在 mysql 中未命中索引
当选择所有列时,索引不起作用,但如果只选择几列,它就会起作用。
mysql> explain SELECT * FROM fatura USE INDEX (datapago_serv_pago) WHERE id_servidor = 10 AND pago = '1' AND data_pago <= '2021-05-05';
+----+-------------+--------+------------+------+--------------------+------+---------+------+----------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+--------------------+------+---------+------+----------+----------+-------------+
| 1 | SIMPLE | fatura | NULL | ALL | datapago_serv_pago | NULL | NULL | NULL | 10199216 | 0.00 | Using where |
+----+-------------+--------+------------+------+--------------------+------+---------+------+----------+----------+-------------+
mysql> explain SELECT uid_ FROM fatura USE INDEX (datapago_serv_pago) WHERE id_servidor = 10 AND pago = '1' AND data_pago <= '2021-05-05';
+----+-------------+--------+------------+-------+--------------------+--------------------+---------+------+---------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+--------------------+--------------------+---------+------+---------+----------+--------------------------+
| 1 | SIMPLE | fatura | NULL | range | datapago_serv_pago | datapago_serv_pago | 5 | NULL | 5099608 | 0.01 | Using where; Using index |
+----+-------------+--------+------------+-------+--------------------+--------------------+---------+------+---------+----------+--------------------------+
CREATE TABLE `fatura` (
`uid_` bigint unsigned NOT NULL,
`uid_cliente` bigint unsigned NOT NULL,
`uid_cliente_servico` bigint unsigned NOT NULL,
`id_servidor` tinyint unsigned NOT NULL,
`id` int unsigned NOT NULL DEFAULT '0',
`data_cadastro` date NOT NULL,
`valor` decimal(12,2) NOT NULL DEFAULT '0.00',
`vencimento` date NOT NULL DEFAULT (0),
`pago` tinyint unsigned NOT NULL DEFAULT '0',
`data_pago` date NOT NULL DEFAULT (0),
`valor_pago` decimal(12,2) unsigned NOT NULL DEFAULT '0.00',
`historico` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`id_cliente` int unsigned NOT NULL DEFAULT (0),
`id_servico` int unsigned NOT NULL DEFAULT (0),
`nome` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`desativada` tinyint unsigned NOT NULL DEFAULT '0',
`operador_inclusao` varchar(50) NOT NULL,
`operador_liquidacao` varchar(50) NOT NULL,
`forma_pago` varchar(50) NOT NULL,
`status_banco` tinyint NOT NULL DEFAULT (0),
PRIMARY KEY (`uid_`),
KEY `vencimento` (`vencimento`) USING BTREE,
KEY `id_cliente` (`id_cliente`),
KEY `id_servico` (`id_servico`),
KEY `pago` (`pago`),
KEY `id_servidor` (`id_servidor`),
KEY `id` (`id`) USING BTREE,
KEY `uid_cliente` (`uid_cliente`),
KEY `data_pago` (`data_pago`),
KEY `data_cadastro` (`data_cadastro`),
KEY `historico` (`historico`),
KEY `uid_cliente_servico` (`uid_cliente_servico`),
KEY `desativada` (`desativada`),
KEY `operador_inclusao` (`operador_inclusao`),
KEY `operador_liquidacao` (`operador_liquidacao`),
KEY `venc_serv_pago` (`vencimento`,`id_servidor`,`pago`),
KEY `forma_pago` (`forma_pago`),
KEY `status_banco` (`status_banco`),
KEY `datapago_serv_pago` (`data_pago`,`id_servidor`,`pago`),
KEY `vencimento_serv_pago` (`data_pago`,`id_servidor`,`pago`)
)
如果我选择日期范围,它可以正常工作:
explain SELECT * FROM fatura USE INDEX (datapago_serv_pago) WHERE id_servidor = 10 AND pago = '1' AND data_pago >= '2021-04-01' AND data_pago <= '2021-05-05';
+----+-------------+--------+------------+-------+--------------------+--------------------+---------+------+--------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+--------------------+--------------------+---------+------+--------+----------+-----------------------+
| 1 | SIMPLE | fatura | NULL | range | datapago_serv_pago | datapago_serv_pago | 5 | NULL | 158342 | 0.01 | Using index condition |
+----+-------------+--------+------------+-------+--------------------+--------------------+---------+------+--------+----------+-----------------------+
在整个文档中搜索了很多,但找不到原因或解决方案。