MySQL中字符串查询效率大比拼

这篇具有很好参考价值的文章主要介绍了MySQL中字符串查询效率大比拼。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

背景

最近有个同事对字符串加索引,加完后,发现多了个奇奇怪怪的数字
执行的SQL如下:

alter table string_index_test add index `idx_name` (`name`) USING BTREE;

这个奇怪数字就是191,它很是疑惑,也没指定索引的长度
MySQL中字符串查询效率大比拼
通过查看MySQL官方文档

InnoDB has a maximum index length of 767 bytes for tables that use COMPACT or REDUNDANT row format, so for utf8mb3 or utf8mb4 columns, you can index a maximum of 255 or 191 characters, respectively. If you currently have utf8mb3 columns with indexes longer than 191 characters, you must index a smaller number of characters.

In an InnoDB table that uses COMPACT or REDUNDANT row format, these column and index definitions are legal:
col1 VARCHAR(500) CHARACTER SET utf8, INDEX (col1(255))

To use utf8mb4 instead, the index must be smaller:
col1 VARCHAR(500) CHARACTER SET utf8mb4, INDEX (col1(191))

大概意思就是InnoDB最大索引长度为 767 字节数,用的编码是utf8mb4,则可以存储191个字符(767/4 约等于 191),编码字段长度超出最大索引长度后MySQL 默认在普通索引追加了191

思考

1、MySQL中如何提高字符串查询效率?

对字符串加索引?
一般情况下,是不建议在字符串加索引,占空间
如果一定要加,建议可以指定长度,前提是字符串前面部分区分度好的话,此时这类索引就叫前缀索引

2、前缀索引有什么问题?

区分度不好的话,很容易发生碰撞,进而引发一系列问题
我们再通过执行计划来分析一波
MySQL中字符串查询效率大比拼
上面分别演示了前缀索引和普通索引在只有where条件、order by和group by不同执行情况,可以看到Extra的说明,前缀索引只有where条件,无法使用覆盖索引,order by会使用filesort,group by会使用temporary和filesort
总的来说,前缀索引无法使用覆盖索引,进而导致order by和group by要使用文件排序,甚至临时表
前缀索引有这么些问题,不指定长度?怎么处理?

分析

准备了单表100W的数据进行测试
使用性能压力测试工具mysqlslap
性能测试脚本

mysqlslap -uroot -p --concurrency=100,200 --iterations=1 --number-of-queries=1 --create-schema=test --query=C:\xxx\query.sql

–concurrency=100,200 测试并发的线程数/客户端数,第一次100,第二次200
–iterations=1 指定测试重复次数1次
–number-of-queries=1 指定每个线程执行的 SQL 语句数量上限(不精确)
–create-schema=test 指定查询的数据库test

1、不加索引
查询的SQL:SELECT SQL_NO_CACHE * FROM string_index_test WHERE name=‘forlan’;

Benchmark
        Average number of seconds to run all queries: 8.328 seconds
        Minimum number of seconds to run all queries: 8.328 seconds
        Maximum number of seconds to run all queries: 8.328 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0

Benchmark
        Average number of seconds to run all queries: 18.078 seconds
        Minimum number of seconds to run all queries: 18.078 seconds
        Maximum number of seconds to run all queries: 18.078 seconds
        Number of clients running queries: 200
        Average number of queries per client: 0

2、加字符串索引
alter table string_index_test add index idx_name (name) USING BTREE;
查询的SQL:SELECT SQL_NO_CACHE * FROM string_index_test WHERE name=‘forlan’;

Benchmark
        Average number of seconds to run all queries: 0.250 seconds
        Minimum number of seconds to run all queries: 0.250 seconds
        Maximum number of seconds to run all queries: 0.250 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0

Benchmark
        Average number of seconds to run all queries: 1.438 seconds
        Minimum number of seconds to run all queries: 1.438 seconds
        Maximum number of seconds to run all queries: 1.438 seconds
        Number of clients running queries: 200
        Average number of queries per client: 0

3、使用CRC32创建索引

CRC全称为Cyclic Redundancy Check,又叫循环冗余校验。
CRC32是CRC算法的一种,返回值的范围0~2^32-1,使用bigint存储

加一个name_crc32列,创建这个列的所有,索引空间小很多,利用整型加速查询
加索引:alter table string_index_test add index idx_nam_crc32 (name_crc32) USING BTREE;
查询的SQL:SELECT SQL_NO_CACHE * FROM string_index_test WHERE name_crc32=CRC32(‘forlan’) and name=‘forlan’;
因为CRC32存在发生碰撞,所以加上name条件,才能筛选出正确的数据文章来源地址https://www.toymoban.com/news/detail-467093.html

Benchmark
        Average number of seconds to run all queries: 0.266 seconds
        Minimum number of seconds to run all queries: 0.266 seconds
        Maximum number of seconds to run all queries: 0.266 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0

Benchmark
        Average number of seconds to run all queries: 0.390 seconds
        Minimum number of seconds to run all queries: 0.390 seconds
        Maximum number of seconds to run all queries: 0.390 seconds
        Number of clients running queries: 200
        Average number of queries per client: 0

总结

  • 通过对字符串加索引,可以提高查询效率,但需要注意指定长度,无法使用覆盖索引
  • 通过使用CRC32,需要额外存一个字段,将字符串转为整数存储,节省空间,效率提升并不是很大,但存在碰撞问题,可以加多字符串筛选条件
  • -对于CRC32存在碰撞问题,可以使用CRC64减少碰撞,但需要安装 common_schema database函数库

到了这里,关于MySQL中字符串查询效率大比拼的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Python_字符串(字符串拼接:join() 与 +拼接符两种方法效率对比)

    运算结果如下图所示: 显然,在循环1000000次的前提下,字符的join()运算效率远远高于使用+拼接符的运算。 其根本逻辑在于: 对于简单的+运算符,字符串每次相加都会创建一个新的对象。较为费时。 而对于join(),其用于列表内字符串的相加,列表中字符串的相加并不会创建

    2023年04月24日
    浏览(45)
  • 字符串查询--Python

    str1 = \\\'hello python\\\'  index 需求:查找p所在的索引位置  格式: 字符串.index(self(不用传值), sub(子字符串), start(起始位置), end(结束位置)) print(str1.index(\\\'p\\\'))  # 6 如果字符串中含有多个子字符串,则会返回指定范围内的从左至右的第一个查找到的子字符串位置索引 print(str1.index(\\\'o\\\'))  #

    2024年02月09日
    浏览(32)
  • Elasticsearch-单字符串多字段查询

    单字符串查询实例  bool查询should的算分过程 Disjunction Max Query查询 通过Tie Breaker参数调整 MultiMatch Query查询 三种场景  MultiMatch Query语法  使用多数字段匹配解决 跨字段搜索(一个地址信息映射到不同的字段上:街道,城市,国家......) PUT /blogs/_bulk {\\\"index\\\":{\\\"_id\\\":1}} {\\\"title\\\":\\\"Qucik brown

    2024年02月06日
    浏览(32)
  • Hive SQL判断一个字符串中是否包含字串的N种方式及其效率

    这是个常见需求,某个表tab中,需要判断某个string类型的字段中,哪些数据含有一个子串。以下给出6种方案,并给出效率对比。 可以使用regexp_extract(subject, pattern, index)函数来提取字符串中匹配指定正则表达式的字串。要判断一个字符串中是否包含字串\\\"ABCD;\\\",可以使用如下代

    2024年01月24日
    浏览(53)
  • fastApi笔记04-查询参数和字符串校验

    使用Query可以对查询参数添加校验 default:默认值 max_length:最大长度 min_length:最小长度 pattern:正则表达式 当使用Query声明一个查询参数为必须参数时,只用不声明默认值就行,也就是default不传 声明None是个有效类型,这样将强制客户端传一个值。即使这个值是None 使用Query显

    2024年02月21日
    浏览(25)
  • mysql 解析json字符串、数组字符串、json数组字符串

    笔者使用mysql 5.7进行了一次json字符串的解析,因为一直在搞大数据相关的数据库、olap等,太久没有用mysql5.x的版本,一些函数已经不知道支不支持,我的同事建议我使用like、rlike模糊匹配的方式,身为数据人我不太喜欢用这种手段,因为他们比较低效。于是我想这里总结一下

    2024年02月16日
    浏览(32)
  • mysql 拼接字符串,截取字符串 常用方式

    1,拼接字符串常用:CONCAT(‘需要拼接的字符串’,列名) CONCAT(\\\'+\\\',b.Telephone)查询出号码,前边加上+号 2,分割字符串 substring_index(b.Telephone,\\\'+\\\',1) 用从左往右的第一个+分割电话号码,获取分割后的从左往右的第一个字符串 比如 86+133333333 执行完后 为 86 substring_index(b.Telephone,\\\'+\\\',

    2024年02月14日
    浏览(41)
  • 联表查询的时候外键id是字符串

    2024年02月09日
    浏览(26)
  • Java 新手如何使用Spring MVC 中的查询字符串和查询参数

    目录 前言   什么是查询字符串和查询参数? Spring MVC中的查询参数  处理可选参数 处理多个值 处理查询参数的默认值 处理查询字符串 示例:创建一个RESTful服务  总结 作者简介:  懒大王敲代码,计算机专业应届生 今天给大家聊聊Java 新手如何使用Spring MVC 中的查询字符串

    2024年02月03日
    浏览(35)
  • MYSQL字符串函数详解和实战(字符串函数大全,内含示例)

    MySQL提供了许多字符串函数,用于处理和操作字符串数据。以下是一些常用的MYSQL字符串函数。 建议收藏以备后续用到查阅参考 。 目录 一、CONCAT 拼接字符串 二、CONCAT_WS 拼接字符串 三、SUBSTR 取子字符串 四、SUBSTRING 取子字符串 五、SUBSTRING_INDEX 取子字符串 六、REPLACE 替换子

    2024年02月05日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包