SQL:對用戶MySQL的LIKE查詢時間太長
SELECT
COUNT(usr.id) as `total_results`
FROM
users as usr
LEFT JOIN profile as prof
ON prof.uid = usr.uid
WHERE
usr.username LIKE '%a%'
OR
prof.name LIKE '%a%'
指標:在個人
index1 - uid
index2 - uid,name
index3 - name
uid - uid
username - username
指標說明:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY usr ALL NULL NULL NULL NULL 18387
1 PRIMARY prof ref index2,index1 index2 8 site.usr.uid 1 Using where
2 DEPENDENT SUBQUERY sub ref i3,index1,index2 i3 16 site.usr.uid,const 1 Using index
以上查詢大約需要0.1221
我該如何讓它運行得更快?
刪除你的'LIKE'上的開始'%' – Lamak 2012-03-29 15:54:00
你想達到什麼目的?您是否真的想要統計所有名稱或用戶名中帶有字母「a」的條目? – liquorvicar 2012-03-29 15:58:24
@liquorvicar是的,我正在嘗試獲取搜索記錄的數量 – fxuser 2012-03-29 16:00:23