4

有谁知道之间的区别

使用索引

使用哪里;使用索引

在 mysql 的解释输出中(在 Extra 中)?

再生产:

CREATE TABLE `tmp_t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `a` int(11) NOT NULL DEFAULT '0',
  `b` varchar(50) CHARACTER SET latin1 NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `k1` (`a`),
  KEY `k2` (`b`)
) ENGINE=InnoDB AUTO_INCREMENT=5;

insert into tmp_t1 (a,b) values (1,'b1'), (1, 'b2'), (2, 'b3');

mysql> 解释 select count(1) from tmp_t1 where a=1 \G

*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: tmp_t1
         type: ref
possible_keys: k2,kz
          key: kz
      key_len: 4
          ref: const
         rows: 3
        Extra: Using index
1 row in set (0.11 sec)

mysql> 解释 select count(1) from tmp_t1 where b='b1' \G

*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: tmp_t1
         type: ref
possible_keys: k3
          key: k3
      key_len: 52
          ref: const
         rows: 2
        Extra: Using where; Using index
1 row in set (0.00 sec)

有谁知道为什么在第一种情况下额外字段中只有“使用索引”,而在第二种情况下是“使用 where;使用索引”?案例之间的区别在于,第一个案例在整数上运行 WHERE,而第二个案例在 varchar(50) 字段上执行。但是这有什么关系??

谢谢你的帮助!

4

3 回答 3

3

我应该确认差异确实非常令人困惑。在其他情况下Using where; Using index,可能意味着使用了索引,但它是完全扫描的。就像您在 (a, c) 上有一个复合索引但进行类似SELECT a FROM thetable WHERE c = 1. 在这种情况下,MySQL 将使用索引(因为它包含所有必要的数据并且在内存中),但将对索引进行全面扫描。

结果,您应该看到变量

SHOW STATUS LIKE 'Handler_read_next';

将增加到表中的行数。

但这不是查询的情况:

select count(*) from tmp_t1 where b='b1';

它检查索引中的确切行数。我认为这是某种错误或功能,又一个证据表明结果 EXPLAIN 本身并没有什么可依赖的。有趣的是,对于较大表和复合索引的查询,其中 VARCHAR 是索引中的第二个,EXPLAIN有时不会显示Using where. 我很困惑。

于 2012-01-24T17:17:14.573 回答
0

Using Where

A WHERE expression (in additional to the possible key lookup) is used to check if the row should be accepted. If you don't have 'Using where' together with a join type of ALL, you are probably doing something wrong!

Using Index

Only the index is used to retrieve the needed information from the table. There is no need to perform an extra seek to retrieve the actual record.

于 2012-01-24T12:01:25.210 回答
0

"Using index" means that only the index is used for doing the actual query (finding which rows to return), that means mysql does not need to read the actual rows.

The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.

"Using where" means that the index is also used when doing data lookup (for the rows it needs to return, find the corresponding data for the column you selected)

If the Extra column also says Using where, it means the index is being used to perform lookups of key values. Without Using where, the optimizer may be reading the index to avoid reading data rows but not using it for lookups. For example, if the index is a covering index for the query, the optimizer may scan it without using it for lookups.

Source in mysql's documentation (search for 'using index')

于 2012-01-24T12:02:02.027 回答