mysql - How do I select rows which IDs not in PHPs LARGE array? -
i need solve following task: have quite large array of ids in php script , need select mysql db rows ids not in array.
there several similar questions (how find records not in array? (mysql)) , favourite answer use not in ()
construction implode(',',$array)
within brackets.
and worked... until array gown 2007 ids , 20 kb (in case) i've got "mysql server has gone away" error. can understand because of lengthy query.
there solutions problem this:
set global max_allowed_packet=1073741824;
(just taken this question).
probably in way, doubt not in (implode)
approach 1 big arrays (i expect in case array can 8000 ids , 100 kb).
is there better solution big arrays?
thanks!
edit 1
as solution recommended insert ids array temporary table , use join solve initial task. clear. never used temporary tables , therefore have additional question (probably worth separate question decided leave here):
if need routine several times during 1 mysql session, approach better:
each time need select id not in php array create new temporary table (all tables deleted after mysql connection termination - after script terminated in fact).
i create temporary table , delete 1 after made needed select
i trncate temporary table afterwards.
which better? or missed else?
in such cases better create temporary table , perform query against instead. it'd along lines of:
create temporary table t1 (a int); insert t1 values (1),(2),(3); select * yourtable left join t1 on (yourtable.id=t1.a) t1.a null;
of course insert
statement should constructed you'd insert values array temporary table.
edit: inserting values in single insert
statement lead same problem faced. hence i'd suggest use prepared statement executed insert data temporary table while iterate through php array.
Comments
Post a Comment