- Apr 17, 2003
- 910
- 0
- 0
Does anyone know of a way to exclude rows from a query if they are already in the table? I am using something like this:
SELECT rowID FROM table1 WHERE rowID NOT IN(SELECT row2ID FROM table2)
The amount of IDs returned in the subselect are in the millions, and as far as I can see using IN() or NOT IN() is definitely a recipe for failure.
Apparently, it has been around for a while:
http://bugs.mysql.com/bug.php?id=15872
The thing is, I need to exclude the rows before adding new ones do the DB so is there a way to do it in a single query? Alternately, is there a way to INSERT records while having insert ignore IDs that are already there WITHOUT throwing an error? I dunno...I gotta think of something and it needs to work quickly.
My second choice would be to remove the NOT IN subselect, and do the subselect first as a standalone query. At that point I would put the IDs returned into an array and just that array to exclude the new entries based on their IDs already being in the array. The drawback here is that I would need to loop through every row one-by-one to test if they are already in the array. Not an optimal solution...any ideas are welcome.
SELECT rowID FROM table1 WHERE rowID NOT IN(SELECT row2ID FROM table2)
The amount of IDs returned in the subselect are in the millions, and as far as I can see using IN() or NOT IN() is definitely a recipe for failure.
Apparently, it has been around for a while:
http://bugs.mysql.com/bug.php?id=15872
The thing is, I need to exclude the rows before adding new ones do the DB so is there a way to do it in a single query? Alternately, is there a way to INSERT records while having insert ignore IDs that are already there WITHOUT throwing an error? I dunno...I gotta think of something and it needs to work quickly.
My second choice would be to remove the NOT IN subselect, and do the subselect first as a standalone query. At that point I would put the IDs returned into an array and just that array to exclude the new entries based on their IDs already being in the array. The drawback here is that I would need to loop through every row one-by-one to test if they are already in the array. Not an optimal solution...any ideas are welcome.