当前位置:网站首页>MySQL cases SQL causes 100% CPU utilization
MySQL cases SQL causes 100% CPU utilization
2022-06-24 03:56:00 【Yaochong】
background
Here's the thing , Let me tell you ...
A few days ago, the customer asked me a question SQL, Keywords are blocked for customer privacy , Change to your own test environment statement
WITH tabs AS ( SELECT ROW_NUMBER() OVER(PARTITION by O_ORDERPRIORITY ORDER BY O_TOTALPRICE DESC) as my_rowid, o_orderkey, O_CUSTKEY O_TOTALPRICE from orders where O_ORDERDATE >'1998-01-01' and O_ORDERDATE<'1998-12-30' ) update tabs set O_TOTALPRICE = O_TOTALPRICE+1 where my_rowid>1;
See this bar SQL There is still a problem with the writing , I adapted it according to his meaning , Let's first analyze the logic of the sentence he wants
according to O_ORDERPRIORITY Order attribute grouping , Update the value in each group where the price is not the largest , So nice , According to the meaning , I should write it as follows ,o_orderkey It is the order table primary key
update orders
set O_TOTALPRICE = O_TOTALPRICE + 1
where o_orderkey
in
(select o_orderkey
from (
SELECT ROW_NUMBER() OVER (PARTITION by O_ORDERPRIORITY ORDER BY O_TOTALPRICE DESC) as my_rowid,
o_orderkey
from orders
where O_ORDERDATE > '1998-01-01'
and O_ORDERDATE < '1998-12-30'
) x
where x.my_rowid > 1
);I remind MySQL Medium batch updates should be executed in batches
CPU100%
A few days later, the customer , say CPU 100% 了 , Slow query SQL formal , The association was updated a few days ago ...
So this SQL Why is it so slow ... First say Oracle The solution in , It can be rewritten as merge into guide SQL go hash join, Add appropriate parallelism if possible ,MySQL8.0 I won't support it merge into
merge into orders o using
(select o_orderkey
from (
SELECT ROW_NUMBER() OVER (PARTITION by O_ORDERPRIORITY ORDER BY O_TOTALPRICE DESC) as my_rowid,
o_orderkey
from orders
where O_ORDERDATE > '1998-01-01'
and O_ORDERDATE < '1998-12-30'
) x
where x.my_rowid > 1) x
on (x.o_orderkey = o.o_orderkey)
when matched then update set o.O_TOTALPRICE = o.O_TOTALPRICE+1So let's look at the implementation plan , About format=tree You can refer to https://cloud.tencent.com/developer/article/1876791 This article , But it's a pity that SQL I won't support it ...
explain format=tree update tpch.orders set O_TOTALPRICE = O_TOTALPRICE + 1 where o_orderkey
in
(select o_orderkey
from (
SELECT ROW_NUMBER() OVER (PARTITION by O_ORDERPRIORITY ORDER BY O_TOTALPRICE DESC) as my_rowid,
o_orderkey
from tpch.orders
where O_ORDERDATE > '1998-01-01'
and O_ORDERDATE < '1998-12-30'
) x
where x.my_rowid > 1
);
-- Output results :
<not executable by iterator executor>Let's look at the execution plan of the normal version
The driven table has no index , This SQL stay MySQL There must be no way to implement the results ,
There are hundreds of thousands of rows of data in the table , But because of the matching factor , Relevance affects 20 Billion rows , So this case is over
Conclusion :
MySQL Not suitable for OLAP Data analysis type SQL, Because it is in 8.0 Support analysis functions , Be careful when implementing in production , He doesn't Oracle So efficient , There is also a need to improve learning
that , about MySQL Do you have any good suggestions for association update ?
More articles are welcome to follow my official account , search dbachongzi Or scan QR code
author : Yao Chong Oracle OCM、MySQL OCP、Oceanbase OBCA、PingCAP PCTA authentication , Good at based on Oracle、MySQL Performance Turning And a variety of relational NoSQL database .
边栏推荐
- Pine Script脚本常用内容
- Several key tools for cloud native implementation
- Old popup explorer Exe has stopped working due to problems. What should I do?
- Differences between EDI and VMI
- How to choose excellent server hosting or server leasing in Beijing
- Dialogue with Google technical experts: soundstream is expected to be used for general audio coding in the future
- Idea 1 of SQL injection bypassing the security dog
- Flutter series: offstage in flutter
- Recording a summary of frequently asked questions
- 黑帽SEO实战之通用301权重pr劫持
猜你喜欢

On game safety (I)

老弹出explorer.exe遇到问题已停止工作,怎么办?

Ar 3D map technology

Do you understand TLS protocol?

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

Black hat SEO actual combat search engine snapshot hijacking

Old popup explorer Exe has stopped working due to problems. What should I do?

Koom of memory leak

Brief ideas and simple cases of JVM tuning - how to tune

黑帽SEO实战搜索引擎快照劫持
随机推荐
How to choose excellent server hosting or server leasing in Beijing
Garbage collection mechanism
Disk partition extension using graphical interface and PowerShell code
Yuanqi forest pushes "youkuang", and farmers' mountain springs follow the "roll"?
Grpc: how to make grpc provide swagger UI?
Do you understand TLS protocol?
hprofStringCache
Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
ModStartCMS 主题入门开发教程
Spirit breath development log (17)
getLocationInWindow源码
浅谈游戏安全 (一)
Live broadcast Reservation: cloud hosting or cloud function, how can the business do a good job in technology selection?
Rasa 3. X learning series -rasa 3.2.0 new release
In the post epidemic era, "cloud live broadcast" saves "cloud cultural tourism"?
How do websites use CDN? What are the benefits of using it?
Ar 3D map technology
web渗透测试----5、暴力破解漏洞--(1)SSH密码破解
Clickhouse synchronous asynchronous executor
General scheme for improving reading and writing ability of online es cluster