Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient sqlite query based on list of primary keys

For querying an sqlite table based on a list of IDs (i.e. distinct primary keys) I am using following statement (example based on the Chinook Database):

SELECT * FROM Customer WHERE CustomerId IN (1,2,3,8,20,35)

However, my actual list of IDs might become rather large (>1000). Thus, I was wondering if this approach using the IN statement is the most efficient or if there is a better/optimized way to query an sqlite table based on a list of primary keys.

like image 676
Johannes Avatar asked Nov 09 '22 12:11

Johannes


1 Answers

If the number of elements in the IN is large enough, SQLite constructs a temporary index for them. This is likely to be more efficient than creating a temporary table manually.

The length of the IN list is limited only be the maximum length of an SQL statement, and by memory.

like image 94
CL. Avatar answered Nov 15 '22 06:11

CL.