doujiao7520 2013-05-21 17:12
浏览 24
已采纳

MySQL - 全文搜索 - 丢失索引

So here is the scenario:

  • MySQL
  • have 1 MYISAM Table
  • colum named v.value has a full text index

Basic query works fine, uses the index as expected:

SELECT p.online_identifier
FROM (...)
WHERE  r.area_id = 3 AND s.state_id= 4
AND (snap.area_has_catalogues_attributes_id = 7028
AND MATCH (v.value) AGAINST('+SomeBrand' IN BOOLEAN MODE))

Now when I add an OR, the full text search index (on v.value) is not used. I run Explain to verify it.

The query would look something like this:

(...) WHERE r.area_id = 3 AND s.state_id= 4 AND (snap.area_has_catalogues_attributes_id = 7028 AND MATCH (v.value) AGAINST('+SomeBrand' IN BOOLEAN MODE)) OR (snap.area_has_catalogues_attributes_id = 7045 AND MATCH (v.value) AGAINST('+OtherBrand' IN BOOLEAN MODE))


I dont understand why. Any ideas?

  • 写回答

1条回答 默认 最新

  • dongxia9620 2013-05-21 19:29
    关注

    Here is something interesting: The query you have is actually causing the FULLTEXT indexing to be ignored. Don't worry, it is not your fault. I wrote about this before : Is there a way to hint to query optimizer to MySQL which constraints should be done first?

    After looking over my answer and its subsequent links, now let's look at your original query:

    SELECT p.online_identifier
    FROM (...)
    WHERE  r.area_id = 3 AND s.state_id= 4
    AND (snap.area_has_catalogues_attributes_id = 7028
    AND MATCH (v.value) AGAINST('+SomeBrand' IN BOOLEAN MODE))
    

    You will have to execute the FULLTEXT search alone and retrieve IDs only.

    Use those IDs to join to the other table aliases.


    Let me take a query from one of my other links to demonstrate what to do to your query

    Instead of this query

    select * from seeds WHERE MATCH(text) AGAINST
    ("mount cameroon" IN BOOLEAN MODE) = 4;
    

    you must refactor it into something like this:

    SELECT B.* FROM
    (
        SELECT id,MATCH(text) AGAINST
        ("mount cameroon" IN BOOLEAN MODE) score
        FROM seeds
        WHERE MATCH(text) AGAINST
        ("mount cameroon" IN BOOLEAN MODE)
    ) A
    INNER JOIN seeds B USING (id)
    WHERE A.score = 4;
    

    Give it a Try !!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法