I have a set of consecutive rows I want to get based upon their primary key, which is an auto-incrementing integer. Assuming that there are no holes, is there any performance between between:
SELECT * FROM `theTable` WHERE `id` IN (n, ... nk);
and:
SELECT * FROM `theTable` WHERE `id` BETWEEN n AND nk;
BETWEENshould outperformINin this case (but do measure and check execution plans, too!), especially asngrows and as statistics are still accurate. Let’s assume:mis the size of your tablenis the size of your rangeIndex can be used (
nis tiny compared tom)In theory,
BETWEENcan be implemented with a single “range scan” (Oracle speak) on the primary key index, and then traverse at mostnindex leaf nodes. The complexity will beO(n + log m)INis usually implemented as a series (loop) ofn“range scans” on the primary key index. Withmbeing the size of the table, the complexity will always beO(n * log m)… which is always worse (neglibile for very small tablesmor very small rangesn)Index cannot be used (
nis a significant portion ofm)In any case, you’ll get a full table scan and evaluate the predicate on each row:
BETWEENneeds to evaluate two predicates: One for the lower and one for the upper bound. The complexity isO(m)INneeds to evaluate at mostnpredicates. The complexity isO(m * n)… which is again always worse, or perhapsO(m)if the database can optimise theINlist to be a hashmap, rather than a list of predicates.