1、
子查詢優化法
先找出第一條資料,然後大於等於這條資料的id就是要獲取的資料
缺點:資料必須是連續的,可以說不能有where條件,where條件會篩選資料,導致資料失去連續性。
實驗下:
**如下
複製**
mysql> set profiling=1;
query ok, 0 rows affected (0.00 sec)
mysql> select count(*) from member;
+----------+
| count(*) |
+----------+
| 169566 |
+----------+
1 row in set (0.00 sec)
mysql> pager grep !~-
pager set to 'grep !~-'
mysql> select * from member limit 10, 100;
100 rows in set (0.00 sec)
mysql> select * from member where memberid >= (select memberid from member limit 10,1) limit 100;
100 rows in set (0.00 sec)
mysql> select * from member limit 1000, 100;
100 rows in set (0.01 sec)
mysql> select * from member where memberid >= (select memberid from member limit 1000,1) limit 100;
100 rows in set (0.00 sec)
mysql> select * from member limit 100000, 100;
100 rows in set (0.10 sec)
mysql> select * from member where memberid >= (select memberid from member limit 100000,1) limit 100;
100 rows in set (0.02 sec)
mysql> nopager
pager set to stdout
mysql> show profilesg
*************************** 1. row ***************************
query_id: 1
duration: 0.00003300
query: select count(*) from member
*************************** 2. row ***************************
query_id: 2
duration: 0.00167000
query: select * from member limit 10, 100
*************************** 3. row ***************************
query_id: 3
duration: 0.00112400
query: select * from member where memberid >= (select memberid from member limit 10,1) limit 100
*************************** 4. row ***************************
query_id: 4
duration: 0.00263200
query: select * from member limit 1000, 100
*************************** 5. row ***************************
query_id: 5
duration: 0.00134000
query: select * from member where memberid >= (select memberid from member limit 1000,1) limit 100
*************************** 6. row ***************************
query_id: 6
duration: 0.09956700
query: select * from member limit 100000, 100
*************************** 7. row ***************************
query_id: 7
duration: 0.02447700
query: select * from member where memberid >= (select memberid from member limit 100000,1) limit 100
從結果中可以得知,當偏移1000以上使用子查詢法可以有效的提高效能。
2、join查詢法(實質轉化為對id查詢)
總資料有500萬左右
以下例子:當使用 select * from wl_tagindex where byname='f' order by id limit 300000,10 執行時間是 3.21s
優化後:
select * from (
select id from wl_tagindex
where byname='f' order by id limit 300000,10
) aleft join wl_tagindex b on a.id=b.id
執行時間為 0.11s 速度明顯提公升
這裡需要說明的是,我這裡用到的字段是 byname ,id 需要把這兩個欄位做復合索引,否則的話效果提公升不明顯。
分頁查詢優化
自己的乙個 由於單錶的資料記錄高達了一百萬條,造成資料訪問很慢,google分析的後台經常報告超時,尤其是頁碼大的頁面更是慢的不行。先讓我們熟悉下基本的sql語句,來檢視下我們將要測試表的基本資訊 use infomation schema select from tables where tabl...
mysql 分頁優化 Mysql 查詢分頁優化
全表掃瞄,速度極慢 limit 語句的查詢時間與起始記錄的位置成正比 mysql 的 limit 語句是很方便,但是對記錄很多的表並不適合直接使用 建立測試表 drop table if exists t user create table test t user id int 10 unsigne...
SQL分頁查詢優化
基於如下基礎分頁方案 select top 頁大小 from table1 where id select max id from select top 頁碼 1 頁大小 id from table1 order by id as t 瓶頸 order by id 隨著分頁數的上公升,儘管只選取了i...