mysql 添加索引卡死_mysql添加索引,查询反而变慢

mysql 添加索引卡死_mysql添加索引,查询反而变慢依照楼主的数据,我也造了400万数据:mysql>select*fromindex_testlimit5;id1id211111111112222222222111111111122222222221111111111id1创建索引执行确实是id2谓词条件比较快:mysql>select*fromindex_testwhereid1=11111;2097152…

大家好,又见面了,我是你们的朋友全栈君。

依照楼主的数据,我也造了400万数据:

mysql> select * from index_test limit 5;

id1

id2

11111

11111

22222

22222

11111

11111

22222

22222

11111

11111

id1创建索引

执行确实是id2谓词条件比较快:

mysql> select * from index_test where id1=11111;

2097152 rows in set (3.00 sec)

mysql> select * from index_test where id2=11111;

2097152 rows in set (2.32 sec)

id1的profile是这样的:

+———————-+———-+

| Status | Duration |

+———————-+———-+

| starting | 0.000080 |

| checking permissions | 0.000014 |

| Opening tables | 0.000024 |

| init | 0.000033 |

| System lock | 0.000015 |

| optimizing | 0.000018 |

| statistics | 0.035408 |

| preparing | 0.000033 |

| executing | 0.000007 |

| Sending data | 2.963681 |

| end | 0.000021 |

| query end | 0.000015 |

| closing tables | 0.000020 |

| freeing items | 0.003474 |

| logging slow query | 0.000117 |

| cleaning up | 0.000072 |

+———————-+———-+

id2的profile是这样的:

Status

Duration

starting

0.000074

checking permissions

0.000012

Opening tables

0.000025

init

0.000032

System lock

0.000014

optimizing

0.000018

statistics

0.000025

preparing

0.000019

executing

0.000006

Sending data

2.318096

end

0.000020

query end

0.000034

closing tables

0.000022

freeing items

0.004327

logging slow query

0.000093

cleaning up

0.000060

可以看到id1耗时相对显著的是statistics,Sending data,主要集中在Sending data。

Sending data

The thread is reading and processing rows for a SELECT statement, and sending data to the client。

也就是说,id1=11111比id2=11111花费更多的时间在数据读取上。而id1上的是二级索引,用到该索引还有一个回表的花销,在这种数据基数小,索引的选择性就太差,这种情况不应该使用索引。

如果非要使用索引,就要避免回表,创建覆盖索引。

alter table index add index com_idx(id1,id2);

测试结果就可以看到id1=11111比id2=11111快了:

mysql> select * from index_test where id1=11111;

2097152 rows in set (1.71 sec)

mysql> select * from index_test where id2=11111;

2097152 rows in set (2.57 sec)

对应profile如下:

id1=11111

Status

Duration

starting

0.000149

checking permissions

0.000084

Opening tables

0.000032

init

0.000026

System lock

0.000013

optimizing

0.000013

statistics

0.000198

preparing

0.000019

executing

0.000006

Sending data

1.710508

end

0.000024

query end

0.000015

closing tables

0.000019

freeing items

0.003275

logging slow query

0.000068

cleaning up

0.000025

id2=11111

Status

Duration

starting

0.000066

checking permissions

0.000012

Opening tables

0.000020

init

0.000024

System lock

0.000011

optimizing

0.000013

statistics

0.000021

preparing

0.000015

executing

0.000006

Sending data

2.566770

end

0.000025

query end

0.000077

closing tables

0.000034

freeing items

0.004227

logging slow query

0.000093

cleaning up

0.000018

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142994.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JavaScript专题(二)数组去重,会就要会的理直气壮

    JavaScript专题(二)数组去重,会就要会的理直气壮相信当部分开发同学都会遇到这个问题,它的答案有很多种,但是想要真的的回答好这个问题却比较难,我们今天来看看应该怎么回答这个问题。本文篇幅较长,不妨收藏下来慢慢阅读~

    2022年6月16日
    27
  • 深入理解static关键字

    深入理解static关键字提到static关键字,相信大家都不陌生,这是相对比较难以理解的一个关键字,相信各位也都能深深感受的到!本篇文章将好好总结一下static这个关键字。文章目录1、static存在的主要意义2、static的独特之处3、静态变量和实例变量的概念4、静态变量和实例变量【重点常用】5、static静态方法6、static代码块7、static应用场景1、static存在的主要意义static的主要…

    2025年6月5日
    0
  • XML格式化工具类(java)

    XML格式化工具类(java)下面是完整的代码importorg.apache.catalina.tribes.membership.StaticMember;importorg.apache.xml.serialize.OutputFormat;importorg.apache.xml.serialize.OutputFormat;importorg.apache.xml.serialize.XMLSeria…

    2022年7月16日
    23
  • JavaScript高级程序设计(读书笔记)(七)[通俗易懂]

    JavaScript高级程序设计(读书笔记)(七)[通俗易懂]本笔记汇总了作者认为“JavaScript高级程序设计”这本书的前七章知识重点,仅供参考。第七章函数表达式小结:在JavaScript编程中,函数表达式是一种非常有用的技术。使用函数表达式可以无须对函数命名,从而实现动态编程。匿名函数,也称为拉姆达函数,是一种使用JavaScript函数的强大方式。以下总结了函数表达式的特点。函数表达式不同于函数声明。

    2022年8月20日
    4
  • android错误之Unable to resolve target ‘Google Inc.:Google APIs:6’

    在导入一个项目是,出现Unable to resolve target ‘Google Inc.:Google APIs:6’ 按下面方式解决: 修改目录下的project.property文件内容为target=Google Inc.:Google APIs:16(在这里他本来可能是其他版本号,不用管它,只需要改成你所导入的包的版本就行,比如我这里已经导入就是api1

    2022年3月10日
    45
  • Linux 操作系统基础知识总结

    Linux 操作系统基础知识总结1、操作系统总体介绍CPU:就像人的大脑,主要负责相关事情的判断以及实际处理的机制。查询指令:cat/proc/cpuinfo内存:大脑中的记忆区块,将皮肤、眼睛等所收集到的信息记录起来的地方,以供CPU进行判断。查询指令:cat/proc/meminfo1)物理内存物理内存,就是我们将内存条插在主板内存槽上的内存条的容量的大小。看计算机配置的时候,主要看的就是这个物理内存2)虚拟内存Windows中运用了虚拟内存技术,即拿出一部分硬盘空间来充当内存使用,当内存占用完时,电脑就会

    2025年6月26日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号