问题
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;
回答1:
BETWEEN should outperform IN in this case (but do measure and check execution plans, too!), especially as n grows and as statistics are still accurate. Let's assume:
mis the size of your tablenis the size of your range
Index can be used (n is tiny compared to m)
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 (n is a significant portion of m)
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.
回答2:
a between b and c is a macro that expands to b <= a and a <= c.
a in (b,c,d) is a macro that expands to a=b or a=c or a=d.
Assuming your n and nk are integer, both should end up meaning the same. The between variant should be much faster because it's only two compares, versus nk - n compares for the in variant.
回答3:
I have done research for this question. I have 11M rows in my table. I have executed two queries on that:
Query 1:SELECT * FROM PLAYERS WHERE SCORE BETWEEN 10 TO 20
Query 2:SELECT * FROM PLAYERS WHERE SCORE IN (10,11,...,20)
While execution time, both queries are translated as Andomar said above.
Among both queries, Query 1 is running faster than Query 2.
To know more follow this link:
Performance of BETWEEN VS IN() in MySQL
Thank you.
来源:https://stackoverflow.com/questions/3308280/is-there-a-performance-difference-between-between-and-in-with-mysql-or-in-sql-in