MySQL优化之延迟索引和分页优化

php中文网
发布: 2016-06-07 16:45:45
原创
1184人浏览过

什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!

什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!

分页是一个很常见功能,select  **  from tableName limit  ($page -  1 )  * $n ,$n

通过一个存储过程插入10000条数据进行测试:

create table smth1 (
 id int auto_increment ,
 ver int(11) default null,
 content varchar(1000) not null,
 intro varchar(1000) not null,
 primary key(id),
 key idver(id,ver)
 
)engine = innodb default charset = utf8;

 

create procedure smthTest1()
begin
 declare num int default 100001;
 while num   set num := num +1;
  insert into smth1 values (num ,num,'我是*****','我是谁');
 end while ;

end;

查询:

mysql> show profiles;
+----------+------------+----------------------------------------------+
| Query_ID | Duration  | Query                                        |
+----------+------------+----------------------------------------------+
|        1 |  0.002006 | select id ,content from smth1 limit 1000,10  |
|        2 |  0.030106 | select id ,content from smth1 limit 5000,10  |
|        3 |  0.042428 | select id ,content from smth1 limit 9000,10  |
|        4 | 0.01297225 | select id ,content from smth1 limit 10000,10 |
|        5 | 0.13077625 | select id ,content from smth1 limit 20000,10 |

可见随着查询$page 变大,时间会越来越大!

怎样避免这种情况?

一般我们数据库里面数据都不会直接删除,数据时很宝贵的,不舍得删除,另一方便能提高查询数据

先利用索引查询出来数据,再进行联合查询不就行了

 select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 1000 limit 10
) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 5000 limit 10
) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 9000 limit 10
) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 10000 limit 10
) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 20000 limit 10
) as t on C.id = t.id ;

进行执行计划分析,没有一个大于1s的

11 | 0.04538625 | select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 5000 limit 10
) as t on C.id = t.id  |
|      12 |  0.023278 | select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 9000 limit 10
) as t on C.id = t.id  |
|      13 | 0.02320425 | select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 10000 limit 10
) as t on C.id = t.id |
|      14 |  0.001938 | select C.id,C.content from smth1 C inner join
(
 select id from smth1 where id > 20000 limit 10
) as t on C.id = t.id |

此外,还会想到用in来查询而不是子查询,为什么不用in,,使用in会先查询出来一条id,之后再去和下面进行匹配,会进行smth1进行全表扫描!

--------------------------------------分割线 --------------------------------------

Ubuntu 14.04下安装MySQL

《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主从服务器

Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二进制安装

--------------------------------------分割线 --------------------------------------

本文永久更新链接地址:

linux

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号