query optimization help please
am 13.10.2006 22:03:46 von ziggy stardusthello,
i have a database where there is a main table, documents, and then a
relationship table doc_topics with one entry for each topic into which
a document falls. (any document may have an arbitrary number of
topics, but always at least one.) this structure is repeated for other
relationships such as doc_states, doc_types, et cetera. i want a query
that uses one or more of the relationships as search criteria (example,
return only documents with a doc_state entry of "NY"), but i need the
query to return ALL of the states that document pertains to. my
version of mysql does not support subqueries or exist clauses. a
sample query is
select distinct *, t2.topic_id as tid, t2.topic_desc as tdsc from
cap_documents d, cap_doc_topics dt, cap_topics t, cap_doc_states ds
left join cap_doc_topics dt2 on dt2.doc_id = d.doc_id left join
cap_topics t2 on t2.topic_id = dt2.topic_id left join cap_doc_states
ds2 on ds2.doc_id = d.doc_id where d.doc_id = dt.doc_id and dt.topic_id
= t.topic_id and d.doc_id = dt2.doc_id and d.doc_id = ds.doc_id and
d.doc_id = ds2.doc_id and d.display='Y' and match(d.doc_title,
d.summary, t.topic_desc) against('medicare* ' IN BOOLEAN MODE) order by
doc_year desc, d.doc_year desc, d.doc_title, d.author,
d.author_first_name
however, this query takes ten seconds or more to execute. output of
explain is as follows:
+-------+--------+-------------------------+---------+------ ---+--------------+------+---------------------------------- ------------+
| table | type | possible_keys | key | key_len | ref
| rows | Extra |
+-------+--------+-------------------------+---------+------ ---+--------------+------+---------------------------------- ------------+
| d | ref | PRIMARY,display | display | 1 | const
| 51 | Using where; Using temporary; Using filesort |
| ds | ref | PRIMARY,doc_id | PRIMARY | 8 |
d.doc_id | 1 | Using index |
| ds2 | ref | PRIMARY,doc_id | PRIMARY | 8 |
d.doc_id | 1 | Using where; Using index |
| dt | ref | PRIMARY,doc_id,topic_id | PRIMARY | 8 |
d.doc_id | 3 | Using index |
| dt2 | ref | PRIMARY,doc_id | PRIMARY | 8 |
d.doc_id | 3 | Using where; Using index |
| t2 | eq_ref | PRIMARY | PRIMARY | 8 |
dt2.topic_id | 1 | |
| t | eq_ref | PRIMARY | PRIMARY | 8 |
dt.topic_id | 1 | Using where |
+-------+--------+-------------------------+---------+------ ---+--------------+------+---------------------------------- ------------
i have indices in place, and all the join fields are the same type and
size in each table.
any help PLEASE ?
thanks,
-k