日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql延时优化教程_Mysql优化之延迟索引和分页优化_MySQL

發布時間:2023/12/9 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql延时优化教程_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 < 1000000 do

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進行全表掃描!

相關標簽:索引

本文原創發布php中文網,轉載請注明出處,感謝您的尊重!

總結

以上是生活随笔為你收集整理的mysql延时优化教程_Mysql优化之延迟索引和分页优化_MySQL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。