使用复合索引时,MySQL中的基数是什么意思?
我是mysql新手,对基数的含义有点困惑,我读到它表示数字或唯一行,但我想知道它在本例中的含义,这是我的表定义使用复合索引时,MySQL中的基数是什么意思?,mysql,sql,indexing,database-indexes,Mysql,Sql,Indexing,Database Indexes,我是mysql新手,对基数的含义有点困惑,我读到它表示数字或唯一行,但我想知道它在本例中的含义,这是我的表定义 +-------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+--------------+----
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| revisado | varchar(10) | YES | MUL | NULL | |
| total | int(11) | NO | MUL | NULL | |
| busqueda | varchar(300) | NO | MUL | NULL | |
| clave | bigint(15) | NO | | NULL | |
| producto_servicio | varchar(300) | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
目前的记录总数是13621
我有这个疑问
从buscador\u claves2中选择clave、producto\u servicio,其中busqueda='Ferretia'和total=2,revisado='APROBADO'
这是表的索引定义
+------------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| buscador_claves2 | 0 | PRIMARY | 1 | id | A | 14309 | NULL | NULL | | BTREE | |
| buscador_claves2 | 1 | idx_busqueda | 1 | busqueda | A | 14309 | 255 | NULL | | BTREE | |
| buscador_claves2 | 1 | idx_total | 1 | total | A | 3 | NULL | NULL | | BTREE | |
| buscador_claves2 | 1 | idx_revisado | 1 | revisado | A | 1 | NULL | NULL | YES | BTREE | |
| buscador_claves2 | 1 | idx_compuesto1 | 1 | revisado | A | 1 | NULL | NULL | YES | BTREE | |
| buscador_claves2 | 1 | idx_compuesto1 | 2 | total | A | 105 | NULL | NULL | | BTREE | |
| buscador_claves2 | 1 | idx_compuesto1 | 3 | busqueda | A | 14309 | 255 | NULL | | BTREE | |
| buscador_claves2 | 1 | idx_compuesto2 | 1 | busqueda | A | 14309 | 255 | NULL | | BTREE | |
| buscador_claves2 | 1 | idx_compuesto2 | 2 | total | A | 14309 | NULL | NULL | | BTREE | |
| buscador_claves2 | 1 | idx_compuesto2 | 3 | revisado | A | 14309 | NULL | NULL | YES | BTREE | |
+------------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
查询将idx_computes1
作为索引来查找数据,在这种情况下,revisado
、total
和busqueda
列作为索引idx_computes1
的一部分的基数是什么意思?为什么需要idx\u computesto1
而不是idx\u computesto2
,我可以看出这两个索引的基数是不同的
这是查询解释的输出
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: buscador_claves2
type: ref
possible_keys: idx_busqueda,idx_total,idx_revisado,idx_compuesto1,idx_compuesto2
key: idx_compuesto1
key_len: 804
ref: const,const,const
rows: 1
Extra: Using where
我希望您能帮助我更好地理解此信息,谢谢。在MySQL中,索引基数列的值是存储引擎对该索引中唯一值数量的估计值。它用于确定在联接期间此索引的使用情况。通常MySQL优化器更喜欢基数较高的索引,因为这通常意味着它能够过滤到更少的行。理想情况是基数值始终等于
SELECT COUNT(不同于_键)
,但实际上它通常会以相对较小的幅度关闭,这是因为在正常数据库操作过程中很难以不破坏数据库性能的有效方式准确计算基数。分析表之后,该值将立即变得更准确。当优化器可以为一个特定连接选择多个键时,关闭基数开始起作用,这会对选择的键的性能产生巨大的影响,并且这些键的基数估计值足够关闭,从而导致优化器选择错误的键。这种情况相对少见,但确实会发生。在这种情况下,可以使用ANALYZE TABLE
解决问题,或者-如果您总是100%确定哪一个键更适合联接-明确地让优化器在查询中使用FORCE key
。可以从开始。基数是指列中数据的唯一性。低基数,例如在一个位列中,意味着索引不能很好地区分不同的行,反之亦然。我发现“基数”在优化查询中几乎没有任何用处。如果你的值有明显的偏差,“基数”可能会非常远。