首頁 資料庫 mysql教程 MySQL优化之延迟索引和分页优化

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

Jun 07, 2016 pm 04:45 PM
mysql優化 mysql分頁

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

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

分页是一个很常见功能,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

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何透過MySQL對AVG函數最佳化來提高效能 如何透過MySQL對AVG函數最佳化來提高效能 May 11, 2023 am 08:00 AM

如何透過MySQL對AVG函數最佳化來提高效能

MySQL在電子商務應用中的最佳化與安全專案經驗解析 MySQL在電子商務應用中的最佳化與安全專案經驗解析 Nov 03, 2023 am 10:42 AM

MySQL在電子商務應用中的最佳化與安全專案經驗解析

MySQL常見問題解決方法大全 MySQL常見問題解決方法大全 Jun 15, 2023 am 09:51 AM

MySQL常見問題解決方法大全

mysql分頁怎麼查詢 mysql分頁怎麼查詢 Sep 19, 2023 am 09:55 AM

mysql分頁怎麼查詢

如何合理配置和最佳化MySQL的雙寫緩衝技術 如何合理配置和最佳化MySQL的雙寫緩衝技術 Jul 25, 2023 pm 01:01 PM

如何合理配置和最佳化MySQL的雙寫緩衝技術

如何實現MySQL底層優化:SQL語句優化的常見技巧與原則 如何實現MySQL底層優化:SQL語句優化的常見技巧與原則 Nov 08, 2023 pm 08:19 PM

如何實現MySQL底層優化:SQL語句優化的常見技巧與原則

如何最佳化MySQL連線數管理 如何最佳化MySQL連線數管理 Mar 16, 2024 am 08:12 AM

如何最佳化MySQL連線數管理

MySql和SQL最佳化的差異:MySQL最佳化策略與SQL最佳化策略之間的差異 MySql和SQL最佳化的差異:MySQL最佳化策略與SQL最佳化策略之間的差異 Jun 15, 2023 am 09:52 AM

MySql和SQL最佳化的差異:MySQL最佳化策略與SQL最佳化策略之間的差異

See all articles