Pregunta

Estoy tratando de sintonizar una consulta relativamente simple:

select bt.col1,bt.col2
from bigtable bt
join smalltable st on bt.smalltable_id = st.smalltable_id
where st.name = 'some name occuring only once in st'
limit 10

El número de partidos en bigtable es relativamente pequeño en comparación con el tamaño total (<1%)

Aquí está el plan de explicación:

+------+-------------+-------+--------+-----------------+---------+---------+-----------------+----------+-------------+
| id   | select_type | table | type   | possible_keys   | key     | key_len | ref             | rows     | Extra       |
+------+-------------+-------+--------+-----------------+---------+---------+-----------------+----------+-------------+
|    1 | PRIMARY     | bt    | ALL    | ix_smalltable_id| NULL    | NULL    | NULL            | 22709766 | Using where |
|    1 | PRIMARY     | st    | eq_ref | PRIMARY,ix_name | PRIMARY | 2       | bt.smalltable_id|        1 | Using where |
+------+-------------+-------+--------+-----------------+---------+---------+-----------------+----------+-------------+

De alguna manera, aunque sería más fácil recoger el índice, no lo hace.

Luego intenté forzar el índice:

select bt.col1,bt.col2
from bigtable bt force index (ix_smalltable_id)
join smalltable st on bt.smalltable_id = st.smalltable_id
where st.name = 'some name occuring only once in st'
limit 10

Pero el plan de consulta y el tiempo de consulta son los mismos. No quiere usar el índice.

Intenté hacer donde bt.smalltable_id en (seleccionar ...), pero el mismo plan y tiempo de consulta.

Pero si busco el smalltable_id Primero, y luego insertarlo en la selección, es mucho más rápido.

La pregunta

¿Puedo forzar el índice habilitando algunas banderas? ¿Es esto una limitación del motor de consulta?

Estructura de tabla

CREATE TABLE `smalltable` (
  `smalltable_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`smalltable_id`),
  KEY `ix_name` (`name`(10))
) ENGINE=InnoDB AUTO_INCREMENT=5698 DEFAULT CHARSET=utf8

CREATE TABLE `bigtable` (
  `bigtable_id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `col1` varchar(255) DEFAULT NULL,
  `col2` varchar(255) DEFAULT NULL,
  ...
  `smalltable_id` smallint(5) unsigned DEFAULT NULL,
  PRIMARY KEY (`bigtable_id`),
  KEY `ix_smalltable_id` (`smalltable_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23167374 DEFAULT CHARSET=utf8

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a dba.stackexchange
scroll top