|
|
|
|
|
by jcampbell1
4499 days ago
|
|
> It is already a highly selective query. Is it? The first query is always O(1). The worst case for the latter query is that it must aggregate over 999,910 rows. Consider the case where all values of 'a' are 123, and all values 'b' are 42, except 90. |
|
At least on MSSQL, I would expect a query plan like so:
And the optimizer will choose this over a full table scan so long as the 1+2+3 < full table scan. I don't know the threshold for that, but is is certainly more than 100 rows out 1M+, and the planner will have an estimate of selectivity that will inform plan selection.[1] Important caveat. I interpret this line,
Current situation, selecting about hundred rows out of a million:
....to mean selection of 100 rows from the base table, rather than projection of 100 rows in the result, post-aggregation.
But if he really means a SELECT statement that returns 100 rows, then we have no idea how selective the WHERE clause is, and my answer changes to "The query will be much slower (impact >10%)".