当前位置:网站首页>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+1

So 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 .

原网站

版权声明
本文为[Yaochong]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210917190323825u.html