Window functions enable calculations across partitions of rows in a result set. This document discusses window function syntax, types of window functions available in MySQL 8.0 like RANK(), DENSE_RANK(), ROW_NUMBER(), and provides examples of queries using window functions to analyze and summarize data in partitions.
1. Window functions in MySQL 8.0
Presented by
Sri Sakthivel M.D.
www.mydbops.com info@mydbops.com
2. About Mydbops
● Founded in 2015, HQ in Bangalore India with 450+ customer base across the globe.
● Mydbops is on Database Consulting with core specialization on MySQL,MongoDB,PostgreSQL
Administration and Support.
● We have expert team with 30+ certified DBA’s providing full time support and currently managing 400+
servers.
● Mydbops was created with a motto of developing a Devops model for Database administration offering
24*7 expert remote DBA support.
● We help organisations to architect and scale systems in MySQL/Mongo by implementing the advanced
technologies in industry which are completely open source.
● We are leading solution provider in the market for all sort of cloud based database deployments and
management.
3. About Me
● MySQL/MariaDB DBA Expert.
● AWS Solution Architect Associate
● Expertise in Gallera / PXC / InnoDB clusters
● Expertise in MySQL load balancers ProxySQL / Maxscale
● Active MySQL Blogger.
● Interested on DB Fine Tuning and SQL Load Balancers.
● Twitter : @hercules7sakthi
● Hobbies : Bike riding and like good Food.
4. Agenda
● What is Window function ?
● Window function vs MySQL
● Window function Syntax
● Types of Window function
● Query Example with Window function
● Conclusion
5. What is Window function ?
Window functions enable users to perform calculations against partitions (i.e. subgroups or sections) of a
result set .
Window functions increase the efficiency and reduce the complexity of queries that analyze partitions
(windows) of a data set by providing an alternative to more complex SQL concepts, e.g. derived queries.
Window functions do not cause rows to become grouped into a single output row, the rows retain their
separate identities and an aggregated value will be added to each row.
6. Window function vs MySQL
● Frequently requested feature for data analysis
● Supports since version MySQL 8.0
● Supports aggregate & most non aggregate functions
● Solving analytical queries & Improves performance
● Flexible & ease to use
8. Window function Syntax
Partition definition ( Syntax ) :
● Helps to create the partition window over the column .
windows_function_name(expression) OVER({PARTITION BY <definition>})
example :
root@localhost>select a.method,a.loanId,a.amt,a.Total from (select method,loanId,amt, sum(amt) over(partition by method) as Total
from yp_payments) a group by a.method limit 3;
+--------+---------+------+--------------------+
| method | loanId | amt | Total |
+--------+---------+------+--------------------+
| 118 | 6768178 | 2233 | 12176312573.809387 |
| 122 | 4953108 | 1655 | 1036464 |
| 167 | 1975693 | 8235 | 634836 |
+--------+---------+------+--------------------+
9. Window function Syntax
Order definition (syntax) :
● Can use the ORDER BY inside the function OVER()
windows_function_name(expression) OVER({ORDER BY ASC | DESC})
example :
root@localhost:>select a.method,a.loanId,a.amt,a.Total from (select method,loanId,amt, sum(amt) over(order by method desc) as Total
from yp_payments) a group by a.method limit 3;
+--------+---------+------+--------------------+
| method | loanId | amt | Total |
+--------+---------+------+--------------------+
| 994 | 7108021 | 2 | 67 |
| 518 | 6040015 | 4118 | 5050588984.439701 |
| 489 | 4703452 | 1784 | 10805334804.439701 |
+--------+---------+------+--------------------+
3 rows in set (26.15 sec)
10. Window function Syntax
Frame definition :
● Can compute running totals for each row.
● Can compute rolling averages.
Syntax :
windows_function_name(expression) OVER(PARTITION BY <expression> ROWS UNBOUNDED PRECEDING)
windows_function_name(expression) OVER(PARTITION BY <expression> ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
11. Window function Syntax
Frame definition :
Example : running incremental totals for each method ,
root@localhost:yp>select a.method,a.loanId,a.amt,a.incremental_Total from (select method,loanId,amt,sum(amt) over(partition by
method rows unbounded preceding) as incremental_Total from yp_payments where method=489) a limit 5;
+--------+---------+------+-------------------+
| method | loanId | amt | incremental_Total |
+--------+---------+------+-------------------+
| 489 | 6758312 | 2233 | 2233 |
| 489 | 6457319 | 2677 | 4910 |
| 489 | 6094261 | 8387 | 13297 |
| 489 | 6240342 | 5148 | 18445 |
| 489 | 6734211 | 3604 | 22049 |
+--------+---------+------+-------------------+
12. Window function Syntax
Frame definition :
Example : rolling averages for each method,
root@localhost:>select a.method,a.loanId,a.amt,a.rolling_Avg from (select method,loanId,amt, avg(amt) over(partition by method rows
between 1 preceding and 1 following) as rolling_Avg from yp_payments where method=489) a limit 5;
+--------+---------+------+-------------------+
| method | loanId | amt | rolling_Avg |
+--------+---------+------+-------------------+
| 489 | 6758312 | 2233 | 2455 |
| 489 | 6457319 | 2677 | 4432.333333333333 |
| 489 | 6094261 | 8387 | 5404 |
| 489 | 6240342 | 5148 | 5713 |
| 489 | 6734211 | 3604 | 4290 |
+--------+---------+------+-------------------+
5 rows in set (12.73 sec)
25. Query example with windows function
● Need top 2 methods ( column ) from yp_payments table, which having huge amount
● The amount should be above 50000 thousands
● Need to know the highest amount from both methods
26. Query example with windows function
Using windows function :
root@localhost> select a.method,a.loanId,a.amt from (select method,loanId,amt,rank() over(partition by method order by amt desc) as
rank_gap from yp_payments where amt > 50000) a where a.rank_gap=1 order by a.amt desc limit 2;
+--------+---------+--------+
| method | loanId | amt |
+--------+---------+--------+
| 118 | 448 | 508698 |
| 518 | 5377057 | 92839 |
+--------+---------+--------+
2 rows in set (3.02 sec)
Execution Time : 3 seconds
+----+-------------+-------------+------------+------+---------------+-------------+---------+-------+---------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------+------------+------+---------------+-------------+---------+-------+---------+----------+-----------------------------+
| 1 | PRIMARY | <derived2> | NULL | ref | <auto_key0> | <auto_key0> | 8 | const | 10 | 100.00 | Using filesort |
| 2 | DERIVED | yp_payments | NULL | ALL | NULL | NULL | NULL | NULL | 5549711 | 33.33 |Using where; Using filesort |
+----+-------------+-------------+------------+------+---------------+-------------+---------+-------+---------+----------+-----------------------------+
2 rows in set, 2 warnings (0.00 sec)
27. Query example with windows function
normal query :
root@localhost>select method,loanId,max(amt) from yp_payments where amt > 50000 group by method order by amt desc limit 2;
+--------+--------+----------+
| method | loanId | max(amt) |
+--------+--------+----------+
| 118 | 448 | 508698 |
| 518 |5377057 | 92839 |
+--------+--------+----------+
2 rows in set (12.00 sec)
Execution Time : 12 seconds
+----+-------------+-------------+------------+-------+-----------------------------------+---------------------------+---------+------+---------+----------+----------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------+------------+-------+-----------------------------------+---------------------------+---------+------+---------+----------+----------------------------------------------+
| 1 | SIMPLE | yp_payments | NULL | index | fk_yp_payments_method_idx,idx_met | fk_yp_payments_method_idx | 4 | NULL | 5549920 | 33.33 | Using where; Using temporary; Using filesort |
+----+-------------+-------------+------------+-------+-----------------------------------+---------------------------+---------+------+---------+----------+----------------------------------------------+
1 row in set, 1 warning (0.00 sec)
28. Query example with windows function
● GROUP BY & ORDER BY required for normal query to sort the results
● Query without windows function is creating temporary table
● Temporary tables are very critical ( disk temporary tables) on huge data set
29. For an enterprise class Database support and
DB managed services reach
Mydbops
Email : info@mydbops.com