当前位置:网站首页>73- find the SQL example during the business peak period (report development class)

73- find the SQL example during the business peak period (report development class)

2022-06-22 21:21:00 Tiger Liu

The following business logic code segment was proposed in the technical discussion group :

The business logic of this code is to find 2019 With the largest annual business volume 61 second ( I think the original intention should be 60 second , The code is not rigorous enough ) Interval and business volume .

This business question mainly uses plsql complete , It is a typical programmer's thinking , The execution efficiency is obviously very low , My advice is to press to_char(date_created,'yyyymmdd hh24mi') do group by, Then find the maximum ,sql The writing method is relatively simple , I believe that most developers should be able to simply write .

But the subject added other requirements , It may be necessary to make statistics at different time intervals , such as 15 Second interval ,30 Second interval, etc .

In response to this demand , I give SQL It is written as follows ( With 15 Second interval as an example ):

The title owner made a performance comparison between the two writing methods ,1000 More than a million records , The writing method I provided is time-consuming 125 second , primary plsql It takes time to write 1371 second ( This speed is unacceptable ), Efficiency difference 10 Several times , If the amount of data is large , The difference in efficiency will be even greater , And the optimized sql You can also use parallelism , It can speed up .

The query above , The starting point of the interval is fixed , This kind of writing is not too complicated . If we extend this problem further , For example, check which is continuous in a year 10 The business volume is the highest in days ,sql It is relatively complicated , Here's how I put it :

Here are some uses Analysis function Two small cases of , You can refer to it . meanwhile , Use with Of CTE Writing will make sql The level of is relatively clear , Improve the readability of the code .

If you have a better way of writing , You might as well write it in your message , Let's learn together .

原网站

版权声明
本文为[Tiger Liu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221910054744.html