gpt4 book ai didi

mysql - MySQL 中 BINARY LIKE 操作的索引

转载 作者:行者123 更新时间:2023-11-29 02:39:33 26 4
gpt4 key购买 nike

我知道 varchar_pattern_ops 存在于 Postgresql 中,用于在 LIKE 查询中进行快速、基于索引的搜索,但是 MySQL 是否有任何类似的功能?

我目前有一个 Django-MySQL 设置,其中我有一个在非索引字段上运行的查询,并使用 BINARY LIKE 操作,它需要一分钟多的时间才能完成。

我的查询是从文本开头的部分搜索 - text%

这是表结构。该表实际上包含 20 多个字段,但我只包含了主键和我正在搜索的字段

+---------+---------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------------------------+---------------+------+-----+--

| id | varchar(255) | NO | PRI | NULL | |

| mid | varchar(255) | NO | MUL | NULL | |

这是查询-

select count(*) from table where mid binary like 'text%';

这些是索引 -

PRIMARY KEY index has cardinality 102820460
mid index has cardinality 756032

最佳答案

做 MySQL 索引字符串左侧的事实。

如果查询使用通配符右侧,则字符串列可以使用索引:

 SELECT * FROM your_table WHERE field LIKE "text%" # can use an index

但请记住,对于索引,有 767 字节的限制

来自 Mysql 文档

A B-tree index can be used for column comparisons in expressions that use the =, >, >=, <, <=, or BETWEEN operators. The index also can be used for LIKE comparisons if the argument to LIKE is a constant string that does not start with a wildcard character.

https://dev.mysql.com/doc/refman/8.0/en/index-btree-hash.html

关于mysql - MySQL 中 BINARY LIKE 操作的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55685275/

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