MySQL FULLTEXT Search Across >1 Table

后端 未结 4 1667
醉话见心
醉话见心 2020-12-18 10:18

As a more general case of this question because I think it may be of interest to more people...What\'s the best way to perform a fulltext search on two tables? Assume there

相关标签:
4条回答
  • 2020-12-18 10:40

    Just in case you don't know: MySQL has a built in statement called EXPLAIN that can be used to see what's going on under the surface. There's a lot of articles about this, so I won't be going into any detail, but for each table it provides an estimate for the number of rows it will need to process. If you look at the "rows" column in the EXPLAIN result for the second query you'll probably see that the number of rows is quite large, and certainly a lot larger than from the first one.

    The net is full of warnings about using subqueries in MySQL, but it turns out that many times the developer is smarter than the MySQL optimizer. Filtering results in some manner before joining can cause major performance boosts in many cases.

    0 讨论(0)
  • 2020-12-18 10:42

    If you join both tables you end up having lots of records to inspect. Just as an example, if both tables have 100,000 records, fully joining them give you with 10,000,000,000 records (10 billion!).

    If you change the OR by AND, then you allow the engine to filter out all records from table descriptions_programs which doesn't match 'china', and only then joining with titles_programs.

    Anyway, that's not what you need, so I'd recommend sticking to the UNION way.

    0 讨论(0)
  • 2020-12-18 10:43

    Join after the filters (e.g. join the results), don't try to join and then filter.

    The reason is that you lose use of your fulltext index.

    Clarification in response to the comment: I'm using the word join generically here, not as JOIN but as a synonym for merge or combine.

    I'm essentially saying you should use the first (faster) query, or something like it. The reason it's faster is that each of the subqueries is sufficiently uncluttered that the db can use that table's full text index to do the select very quickly. Joining the two (presumably much smaller) result sets (with UNION) is also fast. This means the whole thing is fast.

    The slow version winds up walking through lots of data testing it to see if it's what you want, rather than quickly winnowing the data down and only searching through rows you are likely to actually want.

    0 讨论(0)
  • 2020-12-18 10:46

    The union is the proper way to go. The join will pull in both full text indexes at once and can multiple the number of checks actually preformed.

    0 讨论(0)
提交回复
热议问题