当前位置:网站首页>Why is MySQL query slow?
Why is MySQL query slow?
2022-08-04 21:33:00 【The world is wide】
Pick up a book at ordinary times,How want to roam in In the ocean of knowledge to navigate!(想多了~)
Subconscious always tell myself:你,要看书,要学习,要进步!
大脑:不想看,好累,好无聊啊!
At this time in order to realize“知易行难”The meaning of!
学贵有恒,人贵有志.Faced with the temptation of mobile computer,The bottom of my heart that fragile defence is too easy to break.虽说“金无足赤人无完人”,But at this rate,Gold content is less and less.
得,Ok still more brushSpring,Have a look at the technology stack,Just take a look at todayMySQL,Hope to be able to consider!
说到MySQL查询,Definitely not index the key in the key,当然查询计划也是要看的,Read the query plan to knowMySQL是怎么查询的,How much query the line,The reason why the query speed is not just to yao?
Let's look at the scene of the index fails,Sometimes I feel all know,But all say it out,Seem to have so a DiuDiuDe difficulty?
索引失效有哪些?
1、With no index or uselike “%张三%”这种,不多说,Didn't remember to brush up the MySQL从入门到放弃.
2、 字符串索引,Field coding is different,Everybody under table,Sometimes don't pay attention to the field coding,直接使用默认,If you use the tool,如Navicat,workbench等,这种直接生成DDL语句,如果DDLWith the default encoding,Which do not match the default encoding and the default encoding and the production.那么,恭喜你,If there is even a table query,The index directly abolish the~ ~ ~
当然,The influence of single table query is not,After all your index is established according to the fields in the table,Natural won't have any coding inconsistent problems.If your company project never allowed even table query,那么,恭喜你,This problem directly away from your project.
3、隐式的类型转换. Some elder brothers might be negligence may also be a figure a convenient,The database setup fields,Many fields directly set tovarchar格式,然后,In the code processing,发现,嗯,As involving a diu diu calculation,使用IntegerReceive!!This can receive success?当然能啊!一个隐式转换,框架都帮你做好了,嘛问题没有.
But if index is established in the database field,And you like to use this field to the query again,That is to have fun!Index completely useless!是的,没用!为啥呢? The index tree is a string,You there is a number,Different types of!需要隐式类型转换,The index't walk.If the index is a numeric type(tinyInt,int,bigInt之类的),You are strings coming,This should be go index.具体如何,You can see the execution plan,在这里面,Everything in the sky,哈哈哈.
4、在索引上使用MySQLBuilt-in function or use symbolic operation,比如Date转换,concatSuch as connection or is carried out on the field addition, subtraction, multiplication, and division.mysqlIndex was established,You one by one on index for operation,And to build indexes have what distinction?Not is to scan a full table yao,If you don't understand it,拿一本《MySQL高性能书籍_第3版(中文)》Go by a wall!
5、MySQL查询使用了or ,!= 之类的,比如where name = "张" or age = 18 这种,This even if youname和ageNot all the indexes,mysqlWhen the query selection index can only use an index,Want to check out all the eligible results,Only a full table scan.如果这样写SQL的,Can also go to by a wall.If the two are index,Can consider to separate query,然后使用 union 合并查询结果,This will go index,Should be fast so DiuDiuDe.
6、Index selection error,有时候一个tableThere are four or five indexes,Query conditions and more,mysqlMay use one of the indexes for query,However, the index is not you want to,This query is very slow.这时,Can only see the query plan,Then consider adjustment under the conditions of the query sequence or order of index,努力让SQL变快!再不行,The query is too complicated,Search engine on bai.
7、还有就是联合索引,Joint index mainly is a question of order,三个字段a,b,c建立联合索引,This kind of want to use the index,Must contain the query conditionsa,大家要牢记,MySQL查询对比,From left to right to compare,The index can use,From left to right,In that position,The index can use to which position.再具体点,嗯,Everyone to look at the combined index,Should be what all understand(东西太多,A too long).
8、in的元素太多了,使用in查询的时候,如果in中的条件太多(Assumptions is not the primary key index),MySQLNeed to check the secondary index data in,获取到id,And then again to scan table data,这种情况下,The query efficiency may be it is better to go directly to scan a full table.
使用order by或者group by 不当
order by 就是排序,If this sort of field has index,那皆大欢喜,Directly using the index order,一步搞定!But if no index?Such as index of is youage(年龄),Based on sorting isheight(身高),So what do you want to go to sort,Either the height data is loaded into theMySQLMemory in order,如果数据太大,That is about to establish a temporary table is sorted,慢!那是必须滴!
如果大家使用order byIt is best to use the index to sort or joint index the front part of the sort.Of course if the query result is not much,放到mysqlSorting problems in memory, it should not.If need to establish a temporary table is sorted,Recommend careful,那样的话,真的是,不划算啊.
group by 也就是分组,This actually is trouble than sorting,Even if there is an index,Also must carry on the secondary processing.Grouping is grouped in memory,如果数据太多,Will also create a temporary table,When it comes to build the table,Nature also have fileIO什么的,肯定慢啊,So if the group,As far as possible to a small number of data packet,Rather than a full table or hundreds of thousands of data packet,As the query time in seconds at least.
MySQL锁表
This situation is actually not more,Generally a query or modify can finish soon.If is a deadlock operating affairs,MySQLHave their own processing mechanism(Usually need to roll back the minimum number of rows of data to roll back).If really touching the lock table query,And never let go,That you're going to review your code,当然,Also want to query as soon as possible to lock the thread,然后释放掉锁,不然那MySQL直接宕机,Not too much!
数据库在刷新脏页
数据库执行SQL,Is the equivalent of one transaction,原子性、一致性、隔离性、持久性,It's must be.And eventually to write data to disk.
不过,In order to ascend, speaking, reading and writing speed,MySQL中innerDB引擎,In the database each time to read and write,When the update,Use memory.Updated data generally will be written to the memory,Then the sequential writes a sequential file,When database thread free,Then refreshes data to the database file.If the database has been in a busy,当内存,主要是redo log日志写满了,Must be flushed to the disk to the.
而这个时候,innerDB引擎,Will no longer receive updates data request,这个时候MySQL自然也就慢了.
还有,InnoDB 用缓冲池(buffer pool)管理内存,而当要读入的数据页没有在内存的时候,就必须到缓冲池中申请一个数据页.这时候只能把最久不使用的数据页从内存中淘汰掉:如果要淘汰的是一个干净页,就直接释放出来复用;但如果是脏页呢,就必须将脏页Brush to disk first,变成干净页后才能复用.
单表数据量太大
我们知道,innerDBEngine storage,Create index or,都是根据B+树结构.Need according to the hierarchy to find.innerDBThe storage engine is the smallest storage unit in the page,A page that is16KB.也就是说,每次进行IO取出的数据是16KB.
假设B+The height of the tree for three layers,Not a leaf node storage is the primary key index,Between the two layers of data must be a pointer connection.在innerDBThe size of the pointer is6Byte,If there is there is an index of size8Byte(bigInt类型),那么6+8=14Byte,16KB/14B = 1170个.
The first layer only stores the pointer and index,能存储1170个,The second layer is also stored Pointers and index,Store the number is1170*1170 ,The third layer,Data is stored is,If a data size is2KB,It will be stored in a page8条数据,Three layers of the stored data is1170*1170*8=10951200,Is the data from ten million or so.
What if the only index is notbigInt,而是字符串呢?比如32位的uuid,以utf-8编码为例,The index of the size is3*32Byte+1Byte(字段长度) = 97Byte,97+6=103Byte.
如果B+Trees are three layer,So the first layer of the data stored is16KB/103B = 155,Each line is stored155个索引,第二层也就是155*155=24025,第三层24025*8 = 192200条数据,也就只有19万左右,This certainly cannot store only three layer,IO自然会增加.
当然,UUIDCan transform compression.UUID_TO_BIN()函数将UUID从人类可读格式(VARCHAR)Into a compact format is used to store(BINARY)格式,并且BIN_TO_UUID()函数将UUID从紧凑格式(BINARY)转换为人类可读格式(VARCHAR).
即便如此,存储UUIDValue also need at least16字节,16KB/(16B+6B) = 727,Three layer can store727*727*8=4228K,也就是400多万.相比于bigInt作为主键索引,Store much less obvious.
Now the mainstream of snowflakes algorithm as the primary keyid,其类型就是 64 位比特位的 long 类型的唯一 id,存储到mysql,也就是bigInt类型!你是不是想到了什么?嘿嘿.
对于单表来说,If the data is more than1000万,Advice is to depots table,当然,If the relatively small amount of data in a row,Also can be carried in a period of time,Index used properly,速度也不会很慢,But still want to consider to drop further development,分库分表,As soon as possible the best ha.
关于mysql查询速度变慢,You can think of would be so much,如有疏漏,还请斧正哈!
人贵有志,学贵有恒!与君共勉!
等等,点个赞再走呗~
边栏推荐
猜你喜欢
1319_STM32F103串口BootLoader移植
SPSS-System Clustering Hand Calculation Practice
SPSS-System Clustering Software Practice
MySQL查询为啥慢了?
PowerCLi 导入License到vCenter 7
Re24:读论文 IOT-Match Explainable Legal Case Matching via Inverse Optimal Transport-based Rationale Ext
【SQL之降龙十八掌】01——亢龙有悔:入门10题
数电快速入门(二)(复合逻辑运算和逻辑代数的基本定律的介绍)
27.降维
ROS packages visualization
随机推荐
ES6高级-async的用法
openresty lua-resty-template页面静态化
[Teach you to use the serial port idle interrupt of the STM32HAL library]
模拟对抗之红队免杀开发实践
CPU、内存、显卡等硬件因素也影响着你的深度学习模型性能
ue unreal 虚幻 高分辨率无缩放 编辑器字太小 调整编辑器整体缩放
PowerCLi 导入License到vCenter 7
七夕,当爱神丘比特遇上牛郎和织女
硬件开发定制全流程解析
DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-Polyethylene Glycol-Aldehyde A hydrophobic 18-carbon phospholipid
LayaBox---TypeScript---结构
PowerCLi batch configuration of NTP
Win11如何开启Telnet客户端?
Codeforces Round #811 (Div. 3)
【PCBA program design】Grip dynamometer program
[QT] Implementation of callback function
Altium Designer 19.1.18 - 保护锁定的对象
【QT】回调函数的实现
NFT宝典:你需要知道NFT的术语和定义
拼多多开放平台订单信息查询接口【pdd.order.basic.list.get订单基础信息列表查询接口(根据成交时间)】代码对接教程