您好,登錄后才能下訂單哦!
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)
看完上述內容,你們掌握MySQL中InnoDB引擎如何對索引的擴展的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。