Re: Optimizing a UNION Query
Posted by:
Toa Sty
Date: August 25, 2006 06:14AM
Hi Charles,
So it's still not running well, so let's try to simplify the problem a little.
Start by manually looking-up the termids for the words you're using and run the query without referencing the glossary tables.
So something like:
SELECT DISTINCT d1.docid FROM docref d1, docref d2, docIndex di
WHERE
AND d1.termid IN (1,2,3)
AND
d2.termid IN (4,5,6)
AND
d1.docid = di.docid AND d2.docid = di.docid;
but with your real termids, obviously ;)
(or, if you prefer INNER JOIN to , use something like:
SELECT DISTINCT d1.docid
FROM docref d1 INNER JOIN docIndex di USING (docid) INNER JOIN docref d2 USING (docid)
WHERE
AND d1.termid IN (1,2,3)
AND
d2.termid IN (4,5,6);
)
and see how fast that is.
My suspicion is that it'll execute in about the same amount time as with the glossary tables.
Try adding an multicol index on the docref table on: (termid, docid) This should make a big improvement I think
Other things - you probably know some of these but worth checking:
Run optimize/analyze on your tables to make sure index stats are up to date.
Make sure your key_buffer is large enough to fit your indexes in.
If the query is now running fast enough, add the glossary tables back in, and it should run at about the same (fast) speed.
If not, post explain plans for both and we'll see what to do next :)
HTH,
Toasty