dtmjl4427 2015-09-21 16:17
浏览 43
已采纳

在mysql数据库中最有效地搜索关键字

I am working on a project where I have a database, that contains a summary field, which is filled in by a web form that visitors to the site enter on.

When the user completes entering the summary field, I want to perform a lookup using the words that were entered by the the user on the page for similar records in the database that contain the same keywords that they've filled in on the page.

I was thinking I could split the summary string that is submitted and then loop through the array and build up a query so the query would end up something like:

SELECT * 
FROM my_table 
WHERE summary LIKE '%keyword1%' 
OR summary LIKE '%keyword2' 
OR summary LIKE '%keyword3%';

However, this seems massively inefficient, and as the database could grow quite big, could potentially become quite a slow query to run.

I then found the MySQL IN clause, but this only seems to work with multiple values where a field can only contain 1 of these values in a row.

Is there a way I can use the IN function, or is there a better MySQL function that I can use to do what I want, or is my first idea the only way round it?

An example of what I am trying to achieve is a bit like on Stack Overflow. When you lose focus of the title field, it pops up similar questions based on the title you've provided.

  • 写回答

1条回答 默认 最新

  • duan5801 2015-09-21 21:31
    关注

    I would recommend reading this manual page InnoDB FULLTEXT Indexes and the one on Full-Text Restrictions. New functionality of full text has been incorporated in recent releases of mysql, augmenting the use of it with INNODB tables.

    Concerning the inability to upgrade a mysql version, there is no reason why one cannot mix and match MyISAM and INNODB tables in the same db. As such, one would keep textual information in MyISAM (where historically FTS index power was available), and doing joins to INNODB tables when needed. This avoids the "must upgrade to version 5.6" argument.

    Legend: FTS=Full Text Search

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

报告相同问题?

悬赏问题

  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置