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