Search code examples
mysqlfull-text-searchfulltext-index

MATCH AGAINST across multiple tables without joins


I'm trying to use fulltext and match against multiple tables but without Join because they are not related. I've tried something like:

SELECT mg_users.username, mg_users.email, mg_uzgajivac.imeuzgajivaca ...
  FROM mg_users,mg_uzgajivac,mg_buy_it_now,mg_golu ...
 WHERE MATCH(mg_users.username) AGAINST ('rasa')
    OR MATCH(mg_users.email) AGAINST ('rasa')
    OR MATCH(mg_uzgajivac.imeuzgajivaca) AGAINST ('rasa')
    OR MATCH(mg_uzgajivac.mjesto) AGAINST ('rasa') ...

But it returns nothing, I'm not sure what I'm doing wrong.


Solution

  • I think your issue is that the indexes are not up to date. You need to rebuild your indexes for MATCH ... AGAINST to work correctly.

    If you are using MyISAM tables then repair the tables will rebuild you the indexes

    REPAIR TABLE table_name;
    

    But if you are using Innodb tables "and you should." You can execute a fake ALTER command which will force the indexes to be rebuild. It will not change anything but it will force the table to rebuild.

    ALTER TABLE table_name ENGINE=innodb;
    

    This may be off-topic but it is something to think about. You can use a multiple column fulltext index to make your query a bit faster

    ALTER TABLE mg_users ADD FULLTEXT INDEX (user_name, email); ALTER TABLE mg_uzgajivac ADD FULLTEXT INDEX (imeuzgajivaca, mjesto);

    Then your query will look like this

    SELECT mg_users.username, mg_users.email, mg_uzgajivac.imeuzgajivaca ...
    FROM mg_users,mg_uzgajivac,mg_buy_it_now,mg_golu ...
    WHERE MATCH(mg_users.username, mg_users.email) AGAINST ('rasa')
        OR MATCH(mg_uzgajivac.imeuzgajivaca, mg_uzgajivac.mjesto) AGAINST ('rasa')
        ...