Here is a simple query.
select * where 'id' in [1,2,3,4,5] AND 'sub_id' in [1];
If I switch these two wherein condition to this,
select * where 'sub_id' in [1] AND 'id' in [1,2,3,4,5];
With my first intuition, those two queries seem to have the same time complexity.
However, if I suppose 'id' column has 'N' data(extremely large), then the average time for the first query would take 5n
, assuming 'sub_id' in [1]
part would be O(1), since there are only 5 records to traverse, negligibly small.
On the other hand, second query would take only 1n
, since I am only searching for 1 record among N
data(assuming 'id' in [1,2,3,4,5]
part would be negligibly small too).
Is my assumption hypothetically correct? Assuming 'id' is not pk, thus no index is set.
So, my real question is, when I put wherein (...)
condition in SQL, should I put smaller range first then only larger range in order to make the performance better?
Please fix me if I'm wrong.