doufei1988 2011-07-19 16:43
浏览 9
已采纳

有哪些方法可以改善这个长的mysql搜索查询?

Looking to improve this mysql select statement for a search query:

Select * from table WHERE ( user = '$search_query'
OR user LIKE '$search_query %'
OR keyword LIKE '$search_query'
OR tag LIKE '$search_query'
OR tag LIKE '% $search_query'
OR tag LIKE '% $search_query%'
OR tag LIKE '$search_query%'
OR REPLACE(question, ',' ,'') LIKE '$search_query %'
OR REPLACE(question, ',' ,'') LIKE '% $search_query'
OR REPLACE(question, ',' ,'') LIKE '% $search_query %' 
OR REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(question, '\'', ''), ',', ''), '.',''), ':',''), ';',''), '!',''), '?','') LIKE '%$search_query%'
)

The reason it's broke down the way it is, is because say if someone searches for "art", I don't want it showing results for "heart" as well.

I really need to have of these same functions but running fewer resources.

  • 写回答

3条回答 默认 最新

  • dongyaobo9081 2011-07-19 16:50
    关注

    If your table is MyISAM, you can create a FULLTEXT index on it:

    CREATE FULLTEXT INDEX fx_mytable_user_tag_question ON mytable (user, tag, question)
    
    SELECT  *
    FROM    mytable
    WHERE   MATCH(user, tag, question) AGAINST ('+some*' IN BOOLEAN MODE)
    

    This query will return some and something but not awesome.

    Actually, the first step (creating the index) is not required, however, it will speed up the queries and allow more complex searches (not limited to BOOLEAN MODE) and relevance ranging.

    By default, minimal length of a search query is 4 characters, so art mentioned in your example would not be found.

    To work around this, you will have to change parameter ft_min_word_len in server settings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制