Use golf analogy when explaining to executives.
Use a car analogy for all others.  — Confucius.

The purpose of window functions is to translate the business reporting requirements declaratively and effectively to SQL so query performance and developer/business-analyst efficiency improve dramatically. I’ve seen real-world reports and dashboards go from hours to minutes, minutes to seconds after using window functions.  Query size decreases from 40-pages to a few pages. Back in the ‘90s, Redbrick database really understood the business use case and created a new layer of functionality to do business reporting that included ranking, running totals, calculating commissions & inventory based on subgroups, positions, etc. These have been in SQL standard in 2003.  Every BI layer (like Tableau, Looker, Cognos) exploits this functionality.

Introduction to Window Functions

Imagine you have scores of six golfers through two rounds. Now, you need to create the leaderboard and rank them. Rank them using SQL.

PlayerRound1Round2
Marco7573
Johan7268
Chang6776
Isha7471
Sitaram6872
Bingjie7167

Insert the data into Couchbase.

WITHOUT window functions (current state – Couchbase 6.0)

To write the query without the use of window functions, you need a subquery to calculate the rank for each player.  This subquery has to scan through all of the data resulting in the worst algorithmic complexity of O(N^2), which dramatically increases the execution time and throughput.

With window functions in Mad-Hatter (upcoming release)

This query returns player, total after two rounds (T), how of the score is over/under par (ToPar) and then ranks them based on the scores of first two rounds.  This is the NEW functionality in Mad-Hatter. The time complexity of this is O(N), meaning execution time will only increase linearly.  

Observations:

  1. The query expresses the requirements simply and clearly.
  2. Performance of this query in a real-world scenario is much better.  We plan to measure.
  3. When the ranking requirements depend on multiple documents,  the query becomes quite complex — to write, optimize and run.
  4. All this affects the TCO overall.

Now, let’s create an expanded dashboard.

Show add dense rank, row number, who’s ahead, and the number of strokes behind the leader.  All very common things in a reporting resituation. You’re seeing the new window function whenever you see the OVER() clause.  The query below has six window functions.

As you saw earlier, doing this query with six window functions using subquery method will be a larger effort, expensive, error-prone query.

In addition to making the built-in aggregates (COUNT, SUM, AVG, etc) as window functions, Sitaram has added the following window functions.  The syntax and semantics of each of them are well defined in the standard and well described in the articles of the reference section below.

RANK()
DENSE_RANK()
PERCENT_RANK()
CUME_DIST()
NTILE()
RATIO_TO_REPORT()
ROW_NUMBER()
LAG()
FIRST_VALUE()
LAST_VALUE()
NTH_VALUE()
LEAD()

References:

  1. Probably the Coolest SQL Feature: Window Functions. https://blog.jooq.org/2013/11/03/probably-the-coolest-sql-feature-window-functions/
  2. A Window into the World of Analytic Functions. https://blogs.oracle.com/oraclemagazine/a-window-into-the-world-of-analytic-functions
  3. Oracle Reference: https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174

Posted by Keshav Murthy

Keshav Murthy is a Senior Director at Couchbase of N1QL R&D. Previously, he was a Senior Director at MapR, Senior Architect for IBM, with more than 20 years experience in database design & development. He lead the SQL and NoSQL R&D team at IBM Informix. He has received two President's Club awards at Couchbase, two Outstanding Technical Achievement Awards at IBM. Keshav has a bachelors degree in Computer Science and Engineering from the University of Mysore, India, holds eight US patents and invented databases for systems of engagement.

Leave a reply