mysql_query("SELECT `id` FROM `tasks` WHERE `id` NOT IN(1,2,3,4,5,6,7,8,9,10,11)"); NOT IN(1,2,3,4,5,6,7,8,9,10,11) - ведь когда будет много значений, выборка начнётся долгой. 

Actually, what are the ways to speed up the sample (indexes are) or can rewrite the exception algorithm? Or is my opinion wrong?

  • And by what algorithm is NOT IN formed? Those. by what criterion do you exclude them? NOT IN can be rewritten as id ! = 1 AND `id! = 2 AND ..., it seems to be faster, although not sure - MySQL can optimize the query with constants and the calls will be the same in speed. - BOPOH
  • NOT IN is the same as AND id ! = 2, so how can I make so many AND'ov. After all, instead of 1,2,3,4 there is a variable $ tasks_done - ModaL
  • So I partly because of this, and asked - by what algorithm is formed? After all, in $ tasks_done, you somehow add values. You can add to the end of SQL instead of adding to the array. Or replace it with another request (perhaps by changing the table a little as advised in the answer). - BOPOH

2 answers 2

faster than NOT IN (1,2,3,4, ...) will not work. Even millions of records will be sampled quickly if your id is either UNIQUE or PRIMARY

    In the tasks table, you can add another field, for example, state, with different task statuses (the script will need to be changed) and do a selection on it.