Reputation: 145
Which would be more cost effective way to create a basic SELECT query. Option one:
SELECT id
FROM table
WHERE COL0 NOT IN (2,3,4,5,6,7,8,9...)
AND COL1 >= 20
AND COL2 <= 10
AND .... ;
Or option two:
SELECT id FROM table WHERE COL0 NOT IN (2,3,4,5,6,7,8,9...);
The COL0 is FK column.
The first thing necessary would be index on the COL0. But from there..
Questions:
I'm using Firebird 2.5 .
Upvotes: 0
Views: 83
Reputation: 48197
The db query optimizer will use the best index to filter the most number of rows.
So you should use first aproach and add either:
so imagine you have 1000 rows but only 10 are > 20
optimizer will use the col1
index to filter out 990 rows making the rest of the query faster.
Also instead of use NOT IN
you could save those value in a separated table tblFilter
SELECT id
FROM table T1
LEFT JOIN tblFilter T2
ON T2.col0 = T2.col0
WHERE T2.col0 IS NULL
Upvotes: 2