首页 > 数据库技术 > 详细

mysql模糊查询优化

时间:2015-01-09 20:58:06      阅读:448      评论:0      收藏:0      [点我收藏+]

使用下面的函数来进行模糊查询,如果出现的位置>0,表示包含该字符串。

查询效率比like要高。
如: 

table.field like  ‘%AAA%’ 可以改为 locate (‘AAA’ , table.field) > 0

注:locate(substr,str)


用explain查看结果,rows越少越好!

1、用like查询

   技术分享

2、用locate查询

   技术分享

    可以明显的看到,在此处虽然两者的rows一样,但是filtered的值,用了locate后的值是用like的值的10倍。

    注:The filtered column is described in the MySQL manual:

     

The filtered column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, rows shows the estimated number of rows examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables. This column is displayed if you use EXPLAIN EXTENDED. (New in MySQL 5.1.12)

    也就是说,filtered的越高越好

mysql模糊查询优化

原文:http://blog.csdn.net/class_horse/article/details/42559373

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!