
Originally Posted by
weekend00
if you have really big tables, create index on the two columns on both table.
Can you make an index on a temporary table? And would that be worth it, if you could?
I think that is the correct term, these tables are both being created from a master table (millions of records) and only used inside the query.
e.g.
Code:
Select columns
From (SELECT columns FROM mastertable WHERE criteria) AS t2 INNER JOIN (SELECT columns FROM mastertable WHERE criteria GROUP BY fields HAVING criteria) AS t1 ON (t1.column1=t2.column1) AND (t1.column2=t2.column2)
WHERE criteria
GROUP BY fields
HAVING criteria
ORDER BY fields
my question, is if some extra ORDER BY's would help, by sorting the temporary tables before looking for the join
Code:
Select columns
From (SELECT columns FROM mastertable WHERE criteria ORDER BY column1, column2) AS t2 INNER JOIN (SELECT columns FROM mastertable WHERE criteria GROUP BY fields HAVING criteria ORDER BY column1, column2) AS t1 ON (t1.column1=t2.column1) AND (t1.column2=t2.column2)
WHERE criteria
GROUP BY fields
HAVING criteria
ORDER BY fields
I know I can run this query on my own to test, but I was wondering if this sort of logic as able to be performed by the server or not. My case may make it not work because the column1 field is text, or because I'm using multiple columns, etc.
Sorry if this is confusing, I'm struggling to get everything straight.
So the question is, should I bother to sort the temporary tables, or would there be no point? (since the INNER JOIN will always attempt to match each record from t1 against the entire table of t2, so sorting t1 and t2 is a waste)