As a more general case this question is , because I think it might be of interest to more people ... What is the best way to perform full-text search on two tables? Suppose there are three tables, one for programs (with submitter_id) and one for tags and descriptions with object_id: foreign keys related to entries in programs. We want to submitter_id programs with specific text in their tags or descriptions. We should use MATCH AGAINST for reasons that I will not be doing here. Do not dwell on this aspect.
programs id submitter_id tags_programs object_id text descriptions_programs object_id text
The following works and runs for 20 ms or so:
SELECT p.submitter_id FROM programs p WHERE p.id IN (SELECT t.object_id FROM titles_programs t WHERE MATCH (t.text) AGAINST ('china') UNION ALL SELECT d.object_id FROM descriptions_programs d WHERE MATCH (d.text) AGAINST ('china'))
but I tried to rewrite this as a JOIN as follows, and it works for a very long time. I have to kill him in 60 seconds.
SELECT p.id FROM descriptions_programs d, tags_programs t, programs p WHERE (d.object_id=p.id AND MATCH (d.text) AGAINST ('china')) OR (t.object_id=p.id AND MATCH (t.text) AGAINST ('china'))
Just out of curiosity, I replaced OR with the AND character. It also works in a few milliseconds, but that is not what I need. What happened to the above second request? I can live with UNION and subselects, but I would like to understand.
source share