row_examined是否只检查PK索引(索引列)?或者所有行数据都要检查?
我有一张带有PK和UNIQUE KEY的表
show index from test_table;
+-----------------+------------+----------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------------+------------+----------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test_table | 0 | PRIMARY | 1 | test_table_PK | A | 105464 | NULL | NULL | | BTREE | | |
| test_table | 0 | test_table_UNQ | 1 | test_table_UNQ | A | 105464 | NULL | NULL | | BTREE | | |
+-----------------+------------+----------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
通过test_table_PK限制70000,10从test_table组中选择*;
结果:
id: 1
select_type: SIMPLE
table: test_table
type: index
possible_keys: NULL
key: PRIMARY
key_len: 8
ref: NULL
rows: 70010
Extra:
我的问题是:
在上面的查询结果中为什么possible_keys为空?甚至表中都包含两个键。
为什么mysql检查了70010条记录?
如何减少rows_examined的数量?
下面是实际查询,大约需要13秒才能执行。
EXPLAIN EXTENDED select * from test_table
LEFT JOIN rel_test_table_1
ON
test_table.test_table_PK = rel_test_table_1.test_table_PK
LEFT JOIN test_table_2
ON
rel_test_table_1.test_table_2_PK= test_table_2.test_table_2_PK
LEFT JOIN rel_test_table_2
ON
test_table_2.test_table_2_PK = rel_test_table_2.test_table_2_PK
LEFT JOIN test_table_3
ON
rel_test_table_2.test_table_3_PK = test_table_3.test_table_3_PK
limit 70000,10\G
查询结果 *********************** 1.排 ************** *********
id: 1
select_type: SIMPLE
table: test_table
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 105578
filtered: 100.00
Extra:
*********************** 2.排 *********** ************
id: 1
select_type: SIMPLE
table: rel_test_table_1
type: eq_ref
possible_keys: test_table_PK,rel_test_table_1_FK1_IDX
key: test_table_PK
key_len: 8
ref: db2486db.test_table.test_table_PK
rows: 1
filtered: 100.00
Extra:
*********************** 3.排 *********** ************
id: 1
select_type: SIMPLE
table: test_table_2
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: db2486db.rel_test_table_1.test_table_2_PK
rows: 1
filtered: 100.00
Extra:
*********************** 4.排 *********** ************
id: 1
select_type: SIMPLE
table: rel_test_table_2
type: ref
possible_keys: PRIMARY,test_table_PK,rel_test_table_2_FK1_IDX
key: PRIMARY
key_len: 8
ref: db2486db.test_table.test_table_PK
rows: 1
filtered: 100.00
Extra: Using index
*********************** 5.排 *********** ************
id: 1
select_type: SIMPLE
table: test_table_3
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: db2486db.rel_test_table_2.test_table_PK
rows: 1
filtered: 100.00
Extra: