Mysql的強制索引(Force Index)都爲我們做了哪些優化?

原本只是想驗證一下選擇不同索引對innodb count(*)查詢速度的影響。
各位順道可參考下這篇文章 [InnoDB系列] -- innodb表如何更快得到count(*)結果。

測試過程中沒想到同樣的一條sql語句僅僅是增加了force index後查詢速度幾乎快了一倍。
select count(*) from http_log_3 force index(time) where time >= 000000    //1 row in set (11 min 19.35 sec)
select count(*) from http_log_3 where time >= 000000    //1 row in set (20 min 5.86 sec)


但實際上通過explain分析可知其實這兩條sql語句使用的都是time索引,完全一樣!
在這個特例當中使用force index(time)後影響的並不是索引key的選擇(優化器默認也使用time索引),而是type及rows.


很想知道這是爲什麼,rows是如何被估算出來的,可有公式?

測試環境:
數據庫 mysql 5.1.34,innodb引擎,使用innodb_file_per_table選項。
使用表分區方式創建數據表(按日分區共十個),表中一共有5000萬數據,即每個分區各500萬。

測試輸出:

--------------
explain partitions select count(*) from http_log_3 force index(time) where time >= 000000
--------------

+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+--------------------------+
| id | select_type | table      | partitions                        | type  | possible_keys | key  | key_len | ref  | rows     | Extra                    |
+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+--------------------------+
|  1 | SIMPLE      | http_log_3 | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 | range | time          | time | 3       | NULL | 25000141 | Using where; Using index |
+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+--------------------------+
1 row in set (0.01 sec)

--------------
explain partitions select count(*) from http_log_3 where time >= 000000
--------------

+----+-------------+------------+-----------------------------------+-------+--------------------------+------+---------+------+----------+--------------------------+
| id | select_type | table      | partitions                        | type  | possible_keys            | key  | key_len | ref  | rows     | Extra                    |
+----+-------------+------------+-----------------------------------+-------+--------------------------+------+---------+------+----------+--------------------------+
|  1 | SIMPLE      | http_log_3 | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 | index | time_ip,time_domain,time | time | 3       | NULL | 50000291 | Using where; Using index |
+----+-------------+------------+-----------------------------------+-------+--------------------------+------+---------+------+----------+--------------------------+
1 row in set (0.01 sec)

--------------
explain partitions select count(*) from http_log_3
--------------

+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+-------------+
| id | select_type | table      | partitions                        | type  | possible_keys | key  | key_len | ref  | rows     | Extra       |
+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+-------------+
|  1 | SIMPLE      | http_log_3 | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 | index | NULL          | time | 3       | NULL | 50000291 | Using index |
+----+-------------+------------+-----------------------------------+-------+---------------+------+---------+------+----------+-------------+
1 row in set (0.00 sec)

--------------
select count(*) from http_log_3 force index(time) where time >= 000000
--------------

+----------+
| count(*) |
+----------+
| 50000000 |
+----------+
1 row in set (11 min 19.35 sec)

--------------
select count(*) from http_log_3 where time >= 000000
--------------

+----------+
| count(*) |
+----------+
| 50000000 |
+----------+
1 row in set (20 min 5.86 sec)

--------------
select count(*) from http_log_3
--------------

+----------+
| count(*) |
+----------+
| 50000000 |
+----------+
1 row in set (20 min 6.32 sec)

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章