是怎么樣的SQL優(yōu)化能做到 900W+數(shù)據(jù),從17s到300ms?
往期熱門文章:
1、RedisJson發(fā)布官方性能報告,性能碾壓ES和Mongo 2、分布式數(shù)據(jù)一致性思考-B端系統(tǒng)一致性 3、Java字符串拼接的五種方法,哪種性能最好? 4、一次線上JVM調(diào)優(yōu)實踐,F(xiàn)ullGC40次/天到10天一次的優(yōu)化過程 5、Chrome 瀏覽器全球大翻車?讓 20 多億用戶無網(wǎng)可上
作者: Muscleape
來源: jianshu.com/p/0768ebc4e28d
操作: 查詢條件放到子查詢中,子查詢只查主鍵ID,然后使用子查詢中確定的主鍵關(guān)聯(lián)查詢其他的屬性字段; 原理:
減少回表操作; 可參考《阿里巴巴Java開發(fā)手冊(泰山版)》第五章-MySQL數(shù)據(jù)庫、(二)索引規(guī)約、第7條: 【推薦】利用延遲關(guān)聯(lián)或者子查詢優(yōu)化超多分頁場景。 說明:MySQL并不是挑過offeset行,而是取offset+N行,然后返回放棄前offset行,返回N行,那當(dāng)offset特別大的時候,效率就非常的底下,要么控制返回的總頁數(shù),要么對超過特定閾值的頁數(shù)進(jìn)行SQL改寫。 正例:先快速定位需要獲取的id段,然后再關(guān)聯(lián): SELECT a.* FROM 表1 a,(select id from 表1 where 條件 LIMIT 100000,20) b where a.id = b.id;
--?優(yōu)化前SQL
SELECT??各種字段
FROM?`table_name`
WHERE?各種條件
LIMIT?0,10;
--?優(yōu)化后SQL
SELECT??各種字段
FROM?`table_name`?main_tale
RIGHT?JOIN?
(
SELECT??子查詢只查主鍵
FROM?`table_name`
WHERE?各種條件
LIMIT?0,10;
)?temp_table?ON?temp_table.主鍵?=?main_table.主鍵
Part1前言
mysql>?select?version();
+-----------+
|?version()?|
+-----------+
|?5.7.17????|
+-----------+
1?row?in?set?(0.00?sec)
mysql>?desc?test;
+--------+---------------------+------+-----+---------+----------------+
|?Field??|?Type????????????????|?Null?|?Key?|?Default?|?Extra??????????|
+--------+---------------------+------+-----+---------+----------------+
|?id?????|?bigint(20)?unsigned?|?NO???|?PRI?|?NULL????|?auto_increment?|
|?val????|?int(10)?unsigned????|?NO???|?MUL?|?0???????|????????????????|
|?source?|?int(10)?unsigned????|?NO???|?????|?0???????|????????????????|
+--------+---------------------+------+-----+---------+----------------+
3?rows?in?set?(0.00?sec)
mysql>?select?count(*)?from?test;
+----------+
|?count(*)?|
+----------+
|??5242882?|
+----------+
1?row?in?set?(4.25?sec)
mysql>?select?*?from?test?where?val=4?limit?300000,5;
+---------+-----+--------+
|?id??????|?val?|?source?|
+---------+-----+--------+
|?3327622?|???4?|??????4?|
|?3327632?|???4?|??????4?|
|?3327642?|???4?|??????4?|
|?3327652?|???4?|??????4?|
|?3327662?|???4?|??????4?|
+---------+-----+--------+
5?rows?in?set?(15.98?sec)
mysql>?select?*?from?test?a?inner?join?(select?id?from?test?where?val=4?limit?300000,5)?b?on?a.id=b.id;
+---------+-----+--------+---------+
|?id??????|?val?|?source?|?id??????|
+---------+-----+--------+---------+
|?3327622?|???4?|??????4?|?3327622?|
|?3327632?|???4?|??????4?|?3327632?|
|?3327642?|???4?|??????4?|?3327642?|
|?3327652?|???4?|??????4?|?3327652?|
|?3327662?|???4?|??????4?|?3327662?|
+---------+-----+--------+---------+
5?rows?in?set?(0.38?sec)


Part2證實
select * from test where val=4 limit 300000,5是掃描300005個索引節(jié)點和300005個聚簇索引上的數(shù)據(jù)節(jié)點,我們需要知道MySQL有沒有辦法統(tǒng)計在一個sql中通過索引節(jié)點查詢數(shù)據(jù)節(jié)點的次數(shù)。我先試了Handler_read_*系列,很遺憾沒有一個變量能滿足條件。select * from test a inner join (select id from test where val=4 limit 300000,5); 之后,buffer pool中的數(shù)據(jù)頁的數(shù)量遠(yuǎn)遠(yuǎn)少于select * from test where val=4 limit 300000,5;對應(yīng)的數(shù)量,因為前一個sql只訪問5次數(shù)據(jù)頁,而后一個sql訪問300005次數(shù)據(jù)頁。select * from test where val=4 limit 300000,5mysql>?select?index_name,count(*)?from?information_schema.INNODB_BUFFER_PAGE?where?INDEX_NAME?in('val','primary')?and?TABLE_NAME?like?'%test%'?group?by?index_name;Empty?set?(0.04?sec)
mysql>?select?*?from?test?where?val=4?limit?300000,5;
+---------+-----+--------+
|?id??????|?val?|?source?|
+---------+-----+--------+|?
3327622?|???4?|??????4?|
|?3327632?|???4?|??????4?|
|?3327642?|???4?|??????4?|
|?3327652?|???4?|??????4?|
|?3327662?|???4?|??????4?|
+---------+-----+--------+
5?rows?in?set?(26.19?sec)
mysql>?select?index_name,count(*)?from?information_schema.INNODB_BUFFER_PAGE?where?INDEX_NAME?in('val','primary')?and?TABLE_NAME?like?'%test%'?group?by?index_name;
+------------+----------+
|?index_name?|?count(*)?|
+------------+----------+
|?PRIMARY????|?????4098?|
|?val????????|??????208?|
+------------+----------+2?rows?in?set?(0.04?sec)
select * from test a inner join (select id from test where val=4 limit 300000,5) ;為了防止上次試驗的影響,我們需要清空buffer pool,重啟mysql。mysqladmin?shutdown
/usr/local/bin/mysqld_safe?&
mysql>?select?index_name,count(*)?from?information_schema.INNODB_BUFFER_PAGE?where?INDEX_NAME?in('val','primary')?and?TABLE_NAME?like?'%test%'?group?by?index_name;
Empty?set?(0.03?sec)
mysql>?select?*?from?test?a?inner?join?(select?id?from?test?where?val=4?limit?300000,5)?b?on?a.id=b.id;
+---------+-----+--------+---------+
|?id??????|?val?|?source?|?id??????|
+---------+-----+--------+---------+
|?3327622?|???4?|??????4?|?3327622?|
|?3327632?|???4?|??????4?|?3327632?|
|?3327642?|???4?|??????4?|?3327642?|
|?3327652?|???4?|??????4?|?3327652?|
|?3327662?|???4?|??????4?|?3327662?|
+---------+-----+--------+---------+
5?rows?in?set?(0.09?sec)
mysql>?select?index_name,count(*)?from?information_schema.INNODB_BUFFER_PAGE?where?INDEX_NAME?in('val','primary')?and?TABLE_NAME?like?'%test%'?group?by?index_name;
+------------+----------+
|?index_name?|?count(*)?|
+------------+----------+
|?PRIMARY????|????????5?|
|?val????????|??????390?|
+------------+----------+
2?rows?in?set?(0.03?sec)
Part3
最近熱文閱讀:
1、RedisJson發(fā)布官方性能報告,性能碾壓ES和Mongo 2、分布式數(shù)據(jù)一致性思考-B端系統(tǒng)一致性 3、Java字符串拼接的五種方法,哪種性能最好? 4、一次線上JVM調(diào)優(yōu)實踐,F(xiàn)ullGC40次/天到10天一次的優(yōu)化過程 5、Chrome 瀏覽器全球大翻車?讓 20 多億用戶無網(wǎng)可上 6、while(1) 和 for(;;)有什么區(qū)別? 7、使用MySQL,請用好 JSON 這張牌! 8、延時任務(wù)實現(xiàn)方案 9、服務(wù)端如何防止重復(fù)支付 10、五年工作經(jīng)驗總結(jié) 16 條的代碼規(guī)范 關(guān)注公眾號,你想要的Java都在這里
評論
圖片
表情
