mysql查询优化

参考文档:mysql

http://blog.163.com/zhangjie_0303/blog/static/9908270620146951355834/sql

1.千万不要不一样类型的数据进行比较,好比Date与String。由于like是字符串操做.net

参考文档http://blog.51cto.com/imysqldba/1361152blog

date是datetime索引

推荐使用下面这种接口

2.若是多个字段进行模糊匹配。尤为是手机端,一个搜索框匹配多个字段,进行接口搜索。文档

(这种写法更加好)字符串

select * from tb_base_agy WHEREget

CONCAT(agy_name,short_name,introduction) like '%星梦%';it

(这种写法会致使索引失效)

select * from tb_base_agy WHERE

agy_name LIKE '%星梦%'

or

short_name like '%星梦%'

OR

introduction like '%星梦%';

3.若是条件中须要用到like,因为like是全表扫描,因此须要替换

参考文档:http://blog.csdn.net/luojishan1/article/details/73540118

select * from tb_base_agy

where

LOCATE('星梦',agy_name) > 0;

 

select * from tb_base_agy

where

POSITION('星梦' in agy_name) ;

 

select * from tb_base_agy

where

FIND_IN_SET('星梦',agy_name) ;

4.limit分页的处理 (limit a,b)从a行,查询b条记录。

select * from tb_base_agy

LIMIT 10,10;

(计算出当前分页的第一条记录是多少行,而后查询一条记录。最后经过主键id查询出大于这条记录的数据,而后再分页)

select * from tb_base_agy

where id >=

(select id from tb_base_agy LIMIT 10,1) LIMIT 10;

(经过join处理)

select a.* from tb_base_agy a

JOIN (select id from tb_base_agy LIMIT 10,10 ) b

on a.id = b.id;

相关文章
相关标签/搜索