MySQL中InnoDB引擎對索引的擴充套件
原文連結:
摘要:
InnoDB引擎對索引的擴充套件,自動追加主鍵值及其對執行計劃的影響。
CREATE TABLE t1 (
i1 INT NOT NULL DEFAULT 0,
i2 INT NOT NULL DEFAULT 0,
d DATE DEFAULT NULL,
PRIMARY KEY (i1, i2),
INDEX k_d (d)
) ENGINE = InnoDB;
root@database-one 15:15: [gftest]> CREATE TABLE t1 (
-> i1 INT NOT NULL DEFAULT 0,
-> i2 INT NOT NULL DEFAULT 0,
-> d DATE DEFAULT NULL,
-> PRIMARY KEY (i1, i2),
-> INDEX k_d (d)
-> ) ENGINE = InnoDB;
Query OK, 0 rows affected (0.06 sec)
root@database-one 15:15: [gftest]> INSERT INTO t1 VALUES
-> (1, 1, '1998-01-01'), (1, 2, '1999-01-01'),
-> (1, 3, '2000-01-01'), (1, 4, '2001-01-01'),
-> (1, 5, '2002-01-01'), (2, 1, '1998-01-01'),
-> (2, 2, '1999-01-01'), (2, 3, '2000-01-01'),
-> (2, 4, '2001-01-01'), (2, 5, '2002-01-01'),
-> (3, 1, '1998-01-01'), (3, 2, '1999-01-01'),
-> (3, 3, '2000-01-01'), (3, 4, '2001-01-01'),
-> (3, 5, '2002-01-01'), (4, 1, '1998-01-01'),
-> (4, 2, '1999-01-01'), (4, 3, '2000-01-01'),
-> (4, 4, '2001-01-01'), (4, 5, '2002-01-01'),
-> (5, 1, '1998-01-01'), (5, 2, '1999-01-01'),
-> (5, 3, '2000-01-01'), (5, 4, '2001-01-01'),
-> (5, 5, '2002-01-01');
Query OK, 25 rows affected (0.01 sec)
Records: 25 Duplicates: 0 Warnings: 0
root@database-one 15:21: [gftest]> show index from t1;
+ -------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+ -------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t1 | 0 | PRIMARY | 1 | i1 | A | 5 | NULL | NULL | | BTREE | | |
| t1 | 0 | PRIMARY | 2 | i2 | A | 25 | NULL | NULL | | BTREE | | |
| t1 | 1 | k_d | 1 | d | A | 5 | NULL | NULL | YES | BTREE | | |
+ -------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.01 sec)
SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = ‘2000-01-01’
mysql> EXPLAIN SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ref
possible_keys: PRIMARY,k_d
key: k_d
key_len: 4
ref: const
rows: 5
Extra: Using where; Using index
mysql> EXPLAIN SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: ref
possible_keys: PRIMARY,k_d
key: k_d
key_len: 8
ref: const,const
rows: 1
Extra: Using index
root@database-one 15:35: [gftest]> EXPLAIN SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
partitions: NULL
type: ref
possible_keys: PRIMARY,k_d
key: k_d
key_len: 8
ref: const,const
rows: 1
filtered: 100.00
Extra: Using index
1 row in set, 1 warning (0.01 sec)
-
key_len從4位元組變為8位元組,表明鍵查詢使用列d和i1,而不僅僅是d。
-
ref從const更改為const,const,表明查詢使用兩個鍵值,而不是一個。
-
rows從5減少到1,表明檢索更少的行。
-
Extra從Using where; Using index改為Using index,表示只用索引讀取,不必回表。
root@database-one 16:07: [gftest]> CREATE TABLE t1MyISAM (
-> i1 INT NOT NULL DEFAULT 0,
-> i2 INT NOT NULL DEFAULT 0,
-> d DATE DEFAULT NULL,
-> PRIMARY KEY (i1, i2),
-> INDEX k_d (d)
-> ) ENGINE = MyISAM;
Query OK, 0 rows affected (0.01 sec)
root@database-one 16:07: [gftest]> INSERT INTO t1myisam VALUES
-> (1, 1, '1998-01-01'), (1, 2, '1999-01-01'),
-> (1, 3, '2000-01-01'), (1, 4, '2001-01-01'),
-> (1, 5, '2002-01-01'), (2, 1, '1998-01-01'),
-> (2, 2, '1999-01-01'), (2, 3, '2000-01-01'),
-> (2, 4, '2001-01-01'), (2, 5, '2002-01-01'),
-> (3, 1, '1998-01-01'), (3, 2, '1999-01-01'),
-> (3, 3, '2000-01-01'), (3, 4, '2001-01-01'),
-> (3, 5, '2002-01-01'), (4, 1, '1998-01-01'),
-> (4, 2, '1999-01-01'), (4, 3, '2000-01-01'),
-> (4, 4, '2001-01-01'), (4, 5, '2002-01-01'),
-> (5, 1, '1998-01-01'), (5, 2, '1999-01-01'),
-> (5, 3, '2000-01-01'), (5, 4, '2001-01-01'),
-> (5, 5, '2002-01-01');
Query OK, 25 rows affected (0.02 sec)
Records: 25 Duplicates: 0 Warnings: 0
root@database-one 16:07: [gftest]> EXPLAIN SELECT COUNT(*) FROM t1myisam WHERE i1 = 3 AND d = '2000-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1myisam
partitions: NULL
type: ref
possible_keys: PRIMARY,k_d
key: PRIMARY
key_len: 4
ref: const
rows: 4
filtered: 16.00
Extra: Using where
1 row in set, 1 warning (0.01 sec)
root@database-one 16:12: [gftest]> FLUSH TABLE t1;
Query OK, 0 rows affected (0.00 sec)
root@database-one 16:12: [gftest]> FLUSH STATUS;
Query OK, 0 rows affected (0.14 sec)
root@database-one 16:12: [gftest]> SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01';
+ ----------+
| COUNT(*) |
+ ----------+
| 1 |
+ ----------+
1 row in set (0.03 sec)
root@ database-one 16:12: [gftest]> SHOW STATUS LIKE 'handler_read%';
+ -----------------------+-------+
| Variable_name | Value |
+ -----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 1 |
| Handler_read_last | 0 |
| Handler_read_next | 1 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
+ -----------------------+-------+
7 rows in set (0.01 sec)
root@ database-one 16:13: [gftest]> FLUSH TABLE t1myisam;
Query OK, 0 rows affected (0.01 sec)
root@database-one 16:13: [gftest]> FLUSH STATUS;
Query OK, 0 rows affected (0.00 sec)
root@database-one 16:13: [gftest]> SELECT COUNT(*) FROM t1myisam WHERE i1 = 3 AND d = '2000-01-01';
+ ----------+
| COUNT(*) |
+ ----------+
| 1 |
+ ----------+
1 row in set (0.01 sec)
root@ database-one 16:13: [gftest]> SHOW STATUS LIKE 'handler_read%';
+ -----------------------+-------+
| Variable_name | Value |
+ -----------------------+-------+
| Handler_read_first | 0 |
| Handler_read_key | 1 |
| Handler_read_last | 0 |
| Handler_read_next | 5 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
+ -----------------------+-------+
7 rows in set (0.00 sec)
root@database-one 16:26: [gftest]> SET optimizer_switch = 'use_index_extensions=off';
Query OK, 0 rows affected (0.01 sec)
root@database-one 16:26: [gftest]> EXPLAIN SELECT COUNT(*) FROM t1 WHERE i1 = 3 AND d = '2000-01-01'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
partitions: NULL
type: ref
possible_keys: PRIMARY,k_d
key: PRIMARY
key_len: 4
ref: const
rows: 5
filtered: 20.00
Extra: Using where
1 row in set, 1 warning (0.02 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31556440/viewspace-2682185/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL InnoDB的索引擴充套件MySql索引套件
- MySql 擴充套件儲存引擎MySql套件儲存引擎
- Mysql innodb引擎和myisam引擎使用索引區別MySql索引
- Mysql innodb引擎(一)緩衝和索引MySql索引
- MySQL資料庫對GROUP BY子句的功能擴充套件(2)MySql資料庫套件
- MySQL資料庫對GROUP BY子句的功能擴充套件(1)MySql資料庫套件
- 【Mysql】InnoDB 引擎中的頁目錄MySql
- C 擴充套件庫 – mysql API套件MySqlAPI
- php mysql擴充套件安裝PHPMySql套件
- PHP 擴充套件 Mysql 與 MysqliPHP套件MySql
- 【Mysql】InnoDB 中的 B+ 樹索引MySql索引
- 淺析InnoDB引擎的索引和索引原理索引
- shell中擴充套件命令套件
- Laravel 之搜尋引擎elasticsearch擴充套件ScoutLaravelElasticsearch套件
- kotlin 擴充套件(擴充套件函式和擴充套件屬性)Kotlin套件函式
- sql中的擴充套件學習SQL套件
- Unity3D引擎擴充套件中的編輯器定製方法Unity3D套件
- MySQL InnoDB 索引MySql索引
- MySQL - 擴充套件性 2 擴充套件策略:氪金氪腦任君選MySql套件
- 【Mysql】InnoDB 中的聚簇索引、二級索引、聯合索引MySql索引
- MySQL中MyISAM引擎與InnoDB引擎效能比較MySql
- mysql空間擴充套件 VS PostGISMySql套件
- STL中的棧的擴充套件 (轉)套件
- 一分鐘掌握MySQL的InnoDB引擎B+樹索引MySql索引
- Lynx技術分析-JS引擎擴充套件設計JS套件
- Oracle 11g對AWR的擴充套件Oracle套件
- C++ 對C的擴充套件有哪些C++套件
- WCF擴充套件:行為擴充套件Behavior Extension套件
- 【Mysql】InnoDB 引擎中的資料頁結構MySql
- ?用Chrome擴充套件管理器, 管理你的擴充套件Chrome套件
- mysql innodb索引高度MySql索引
- C#中的擴充套件類的理解C#套件
- 【Kotlin】擴充套件屬性、擴充套件函式Kotlin套件函式
- MySQL MyISAM引擎和InnoDB引擎對於單表大小限制的總結MySql
- 探索MySQL的InnoDB索引失效MySql索引
- Sanic 擴充套件套件
- ORACLE 擴充套件Oracle套件
- 擴充套件工具套件