简体   繁体   English

MySQL索引用法

[英]MySQL Index Usage

I am performing a very simple select over a simple table, where the column that I am filtering over has an index. 我正在一个简单的表上执行一个非常简单的选择,我过滤的列有一个索引。

Here is the schema: 这是架构:

 CREATE TABLE IF NOT EXISTS `tmp_inventory_items` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `transmission_id` int(11) unsigned NOT NULL,
  `inventory_item_id` int(11) unsigned DEFAULT NULL,
  `material_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `transmission_id` (`transmission_id`)
  KEY `inventory_item_id` (`inventory_item_id`),
  KEY `material_id` (`material_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;

Here is the SQL: 这是SQL:

SELECT * FROM `tmp_inventory_items` WHERE `transmission_id` = 330

However, when explaining the query, I see that the index is NOT being used, why is that (the table has about 20 rows on my local machine)? 但是,在解释查询时, 我看到索引没有被使用,为什么 (表在本地机器上有大约20行)?

id  select_type table                   type    possible_keys   key     key_len ref     rows    Extra
1   SIMPLE      tmp_inventory_items...  ALL     transmission_id NULL    NULL    NULL    13      Using where

No key is being used even if I hint the mysql with USE INDEX(transmission_id)... this looks very strange to me (MySQL Version 5.5.28) 即使我用USE INDEX(transmission_id)暗示mysql,也没有使用密钥...这对我来说很奇怪(MySQL版本5.5.28)

Because MySQL's algorithms tell it that preparing an index and using it would use more resources than simply performing the query without one. 因为MySQL的算法告诉它准备索引并使用它会比使用没有索引执行查询时使用更多的资源。

When you feed query syntax to a DBMS, one of the things it does is attempts to determine the most efficient way to process the query (usually there are at least tens of ways). 当您将查询语法提供给DBMS时,它所做的一件事就是尝试确定处理查询的最有效方法(通常至少有几十种方法)。

If you want to, you can use FORCE INDEX(transmission_id) ( documented here ) which will inform MySQL that a table scan is assumed to be very expensive, but it's not recommended as to determine for 20 rows, it's just not valuable. 如果你愿意,你可以使用FORCE INDEX(transmission_id)这里记录 ),这将告诉MySQL假定表扫描非常昂贵,但不建议确定20行,它只是没有价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM