说明:
表profit
包含大约20列,没有像text
这样的大列,并且有两个索引:
idx_uid
上的uid
列uid_acc_fId_date
uid,acc,fid and date
醇>
慢查询sql是:
select * from profit where (((uid='1111') and (flag=32)) and (fId='2222')) and (date='20161008') limit 1;
,解释输出为:
id:1
select_type:SIMPLE
table:profit
type:ref
possible_keys:uid_acc_fId_date
key:uid_acc_fId_date
key_len:4
ref:const
rows:267
Extra:Using where
执行带sql_no_cache
的sql将花费大约2s,并且sql_no_cache
执行相同的sql,第二次将花费大约0.1s。
ENV:
uid=1111
的行数仅为 270 PS:
Mysql将在午夜每天备份,innodb_old_blocks_time
为0,我认为备份会将所有热数据刷新到缓冲池之外。但即使如此,它也会花费太多时间来执行上述查询。那么,这是磁盘问题吗?
答案 0 :(得分:1)
扩展上述评论,将索引uid_acc_fId_date更改为(uid,fid,date,acc)并将查询更改为:
SELECT columns
, I
, actually
, want
FROM profit
WHERE uid = 1111
AND fId = 2222
AND date='20161008'
AND flag = 32
ORDER
BY uid
LIMIT 1;
答案 1 :(得分:0)
尝试选择查询时始终使用列名。与SELECT name,table_name中的class_name;
类似答案 2 :(得分:0)
尝试删除where条件中的所有括号。不必要的括号导致mysql无法正确使用索引。
select *
from profit
where
uid='1111'
and flag=32
and fId='2222'
and date='20161008'
limit 1;