首页 > 数据库技术 > 详细

mysql使用的坑

时间:2017-07-31 11:43:31      阅读:166      评论:0      收藏:0      [点我收藏+]

一:

mysql默认是安装记录的物理顺序取数据的,如果不加order by 排序,可能得不到预期的结果。

(1) 获取 两个时间点的 id  (很快)

$sql = ‘select id from apply_info where create_time< {$now} limit 1’;   (要加  order by id desc)

获得 idNow

$sql = ‘select id from apply_info where create_time>= {$yesterday} limit 1’ (要加 order by id asc)

获得 idYt

(2) 每次取 1000

do{

       select user_mobile,user_from from apply_info where  id <= {$idNow} limit 1000;

       idNow -= 1000;

       //toDo

} while (idNow >= idYt)

 二:

当使用limit时,explain可能会造成误导

(1)explain估计行数,不考虑limit,可能会对查询估计过多的检查行数

(2)类似于SELECT ... FROM TBL LIMIT N这样的查询因为用不到索引将要报告为慢查询,(如果N不大,实际很快)

配置文件设置min-examined-row-limit=Num of Rows,检查的行数>=这个量的查询才会被报告为慢查询,避免误判

(3)类似于这样的select .. from tb where key_part1= ? order by key_part2 limit n,explain也要估计出过多的检查行数

mysql使用的坑

原文:http://www.cnblogs.com/hlongch/p/7262285.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!