Optimizing a UNION Query
I have a glossary of unique words extracted from a collection of documents. The glossary table stores the words and cross-references them with the documents using an id stored in a docref table.
I have indexes on all colums, and usually queries are fast - except in one odd case. I want to construct a query that will return documents containing AT LEAST one word FROM each group of words in the query. So, for example, say I have two groups of words: "apple, orange, banana" AND "red, blue, green". The query should return documents which have at least one word from both of these groups.
I constructed the query like this:
(SELECT DISTINCT docid FROM docIndex WHERE docid IN (SELECT docref.docid FROM glossary INNER JOIN docref ON (glossary.termid=docref.termid AND glossary.word IN ('apple','orange','banana'))) AND docid IN (SELECT docref.docid FROM glossary INNER JOIN docref ON (glossary.termid=docref.termid AND glossary.word IN ('red,blue,green'))))
The problem is that this query never uses the INDEX on docid. Rather, an EXPLAIN statement indicates NULL as a possible_key for the Primary SELECT statement. When I use UNION, the query is horrendously slow, and indicates NULL as a possible_key for the Primary SELECT and for the UNION.
Anyone have any idea why this might be happening?
Subject
Views
Written By
Posted
Optimizing a UNION Query
6581
August 23, 2006 08:16PM
2848
August 24, 2006 04:59AM
2864
August 24, 2006 05:28AM
2912
August 24, 2006 12:12PM
5627
August 25, 2006 06:14AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.