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;

相關文章
相關標籤/搜索