Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL NOT IN [list of ids] (performance)

Tags:

mysql

I'm just wondering if the amount of id's in a list will influence query performance.

query example:

SELECT * FROM foos WHERE foos.ID NOT IN (2, 4, 5, 6, 7)

Where (2, 4, 5, 6, 7) is an indefinitely long list.

And how many is too many (in context of order)?

UPDATE: The reason why i'm asking it because i have two db. On of it (read-only) is the source of items and another one contain items that is processed by operator. Every time when operator asking for new item from read-only db I want to exclude item that is already processed.

like image 768
Pol Avatar asked Jun 22 '12 19:06

Pol


People also ask

How do you check the performance of a SQL query?

Use the Query Store page in SQL Server Management Studio In Object Explorer, right-click a database, and then select Properties. Requires at least version 16 of Management Studio. In the Database Properties dialog box, select the Query Store page. In the Operation Mode (Requested) box, select Read Write.

What affects SQL query performance?

Table size: If your query hits one or more tables with millions of rows or more, it could affect performance. Joins: If your query joins two tables in a way that substantially increases the row count of the result set, your query is likely to be slow. There's an example of this in the subqueries lesson.


1 Answers

Yes, the amount of IDs in the list will impact performance. A network packet is only so big, for example, and the database has to parse all that noise and turn it into a series of:

WHERE foo.ID <> 2
AND foo.ID <> 4
AND foo.ID <> 5
AND ...

You should consider other ways to let your query know about this set.

like image 152
Aaron Bertrand Avatar answered Oct 01 '22 20:10

Aaron Bertrand