gpt4 book ai didi

mysql - 为什么前缀索引比 mysql 中的索引慢?

转载 作者:行者123 更新时间:2023-11-30 21:52:23 26 4
gpt4 key购买 nike

表:(数量:2100W)

CREATE TABLE `prefix` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`number` int(11) NOT NULL,
`string` varchar(750) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `idx_string_prefix10` (`string`(10)),
KEY `idx_string` (`string`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

歧视:

select count(distinct(left(string,10)))/count(*) from prefix;
+-------------------------------------------+
| count(distinct(left(string,10)))/count(*) |
+-------------------------------------------+
| 0.9999 |
+-------------------------------------------+

结果:

select sql_no_cache count(*) from prefix force index(idx_string_prefix10) 
where string <"1505d28b"
243.96s,241.88s

select sql_no_cache count(*) from prefix force index(idx_string)
where string < "1505d28b"
7.96s,7.21s,7.53s

为什么前缀索引比mysql中的索引慢?(原谅我蹩脚的英文)

explain select sql_no_cache count(*) from prefix force index(idx_string_prefix10) 
where string < "1505d28b";

+----+-------------+--------+------------+-------+---------------------+---------------------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+---------------------+---------------------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | prefix | NULL | range | idx_string_prefix10 | idx_string_prefix10 | 42 | NULL | 3489704 | 100.00 | Using where |
+----+-------------+--------+------------+-------+---------------------+---------------------+---------+------+---------+----------+-------------+

最佳答案

当你使用前缀索引时,MySQL 必须从索引中读取,并且在读取索引之后,它还必须读取数据行,以确保该值是由 WHERE 条件选择的。这是两次读取,并扫描更多数据。

当你使用非前缀索引时,MySQL可以从索引中读取整个字符串值,并立即知道该值是否被条件选中,或者是否可以跳过。

关于mysql - 为什么前缀索引比 mysql 中的索引慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com