Forum Moderators: open
i'm doing some queries that pull all 50 rows of the first table and generates some stats on them based on info from the second table. my basic queries look like this
SELECT t1.*, (SELECT COUNT(t2.column) FROM t2 WHERE t2.column2 = '1') as stat1 FROM t1 ORDER BY stat1
I find that when stat1 is around 50 i get great performance, however when stat1 is much larger (50000) the query takes forever to run. i would have thought that the performance would be the same because in both cases the query loops though 2.6 million rows for each of the 50 rows in t1 and the only thing that gets returned is an integer.
what am i missing?
When dealing with tables with millions of rows, it can take some time to add new a new index. Plus the additional storage requirements make it worth investigating before acting.
Might?
Yes, might. It depends on the query plan that gets generated.
if you're frequently doing a WHERE on a field in a table that has 2.6 million records, you don't need EXPLAIN to tell you that you SHOULD have an index on it.
That depends on what else is in the WHERE and what sorts are involved. The COUNT(t2.column) could also be expensive depending on what database system he's using. EXPLAIN doesn't take much effort; it's a lot faster than creating an index that might not be useful.