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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。