php – MySQL语句需要花费多一点时间来解决
发布时间:2020-09-02 05:55:21 所属栏目:PHP 来源:互联网
导读:我在一个数据库庞大的网站上工作.当时有100万条记录在表中.当我执行查询时,花费太多时间来排除.一个示例查询如下: select * from `ratings` order by id limit 499500, 500 每个查询都需要一分钟以上的时间,但是当我把表放到10万条记录上时,这个查询执行得很
我在一个数据库庞大的网站上工作.当时有100万条记录在表中.当我执行查询时,花费太多时间来排除.一个示例查询如下: select * from `ratings` order by id limit 499500,500 每个查询都需要一分钟以上的时间,但是当我把表放到10万条记录上时,这个查询执行得很快. 正如我已经看到的,表中的100万条记录没有问题,因为在数据库表中,没有大记录的问题. 我在Stack Overflow问题How do I add indices to MySQL tables?的帮助下使用表中的id索引,但是我仍然遇到同样的问题. ***我正在使用CodeIgniter的项目. 请注意,这不是建议使用MyISAM一分钟.我使用这个只能让我的ids,min,max和count排队.所以请忽略引擎.create table ratings ( id int auto_increment primary key,thing int null )engine=MyISAM; insert ratings (thing) values (null),(null),(null); insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; insert ratings (thing) select thing from ratings; 我现在有4.7M行 select count(*),min(id),max(id) from ratings; +----------+---------+---------+ | count(*) | min(id) | max(id) | +----------+---------+---------+ | 4718592 | 1 | 4718592 | +----------+---------+---------+ select * from `ratings` order by id limit 499500,500; -- 1 second on a dumpy laptop . explain select * from `ratings` order by id limit 499500,500; +----+-------------+---------+------+---------------+------+---------+------+---------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+------+---------------+------+---------+------+---------+----------------+ | 1 | SIMPLE | ratings | ALL | NULL | NULL | NULL | NULL | 4718592 | Using filesort | +----+-------------+---------+------+---------------+------+---------+------+---------+----------------+ . explain select * from `ratings` where id>=499501 limit 500; +----+-------------+---------+-------+---------------+---------+---------+------+---------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+-------+---------------+---------+---------+------+---------+-----------------------+ | 1 | SIMPLE | ratings | range | PRIMARY | PRIMARY | 4 | NULL | 4198581 | Using index condition | +----+-------------+---------+-------+---------------+---------+---------+------+---------+-----------------------+ 道德的故事可能是使用where子句. 不能排除僵局的可能性. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- php 反斜杠处理函数addslashes()和stripslashes()实例详解
- php array_pop 删除数组最后一个元素实例
- 解决php 处理 form 表单提交多个 name 属性值相同的 input
- Linux平台php命令行程序处理管道数据的方法
- PHP应用:PHP编程中的Session阻塞问题与解决方法分析
- PHP输出图像imagegif、imagejpeg与imagepng函数用法分析
- Centos 6.5下PHP 5.3安装ffmpeg扩展的步骤详解
- PHP获取IP地址所在地信息的实例(使用纯真IP数据库qqwry.dat
- php使用PDO获取结果集的方法
- PHP生成图片验证码、点击切换实例