记录mysql一些小坑

Posted on 2015-04-26


ORDER BY出现using filesort,读写分离后出现数据查询延迟

根据主键order by却出现using filesort

使用explain分析SQL时常常会遇到extra的其中一值为using filesort,如:

PRIMARY KEY (id), KEY uid (uid)

explain select * from t_talbe where uid=1 order by id;

extra结果为:Using where; Using filesort

手册中对Using filesort解释是“MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the WHERE clause.”。

所有针对以上的例子,把KEY uid (uid)修改成KEY u_id (uid,id)可得以解决

原文链接:http://blog.csdn.net/militala/article/details/6066671

读写分离后出现数据查询查询延迟

原始逻辑:

  1. 在master批量更新一批数据
  2. 从slave查询刚最新一批的数据置入redis

最终置入redis的数据并非最新一批数据,愿意是master更新到slave会有很短暂的延迟,但查询却在延迟的那段时间内.

解决方法:

读写都在master,保证数据的即时性.