Schedule Meeting

a

MariaDB: WITH TIES syntax

by | Dec 13, 2021 | MariaDB, MariaDB Features

MariaDB implemented the WITH TIES syntax in version 10.6. This feature may not be a ground breaker, but it is useful in some cases and it increases compatibility with some other DBMSs. Let’s see what it does, and what use cases it has.

Some history

MariaDB (just like MySQL) traditionally supported a PostgreSQL-like syntax LIMIT ... OFFSET, which in real cases is usually ORDER BY ... LIMIT ... [OFFSET ...]. See the MariaDB KnowledgeBase page. The use of OFFSET is often an antipattern, but this is a subject for another time.

In MariaDB 10.6, support for an alternative syntax OFFSET ... ROWS FETCH ... ROWS was added. Funnily enough, it was apparently added for compatibility with PostgreSQL, but this syntax is supported by Oracle and other DBMSs, too. And Oracle compatibility seems to be very important for MariaDB,

This work was MDEV-23908, and WITH TIES was added in a different task that I can’t easily find.

What WITH TIES does

Explaining WITH TIES with an example is incredibly easier by using an example, rather than trying to explain its meaning in English.

As mentioned, normally you use FETCH ... ROWS after ORDER BY. For example:

SELECT id, title
    FROM book
    ORDER BY author
    FETCH 10 ROWS;

This query orders books by title, and returns the first 10 of them.

The WITH TIES version is:

SELECT id, title
    FROM book
    ORDER BY author
    FETCH 10 ROWS WITH TIES;

This query could return more than 10 rows. After sorting the rows, it will return the first ten rows, plus the rows having the same author as the 10th row.

WITH TIES doesn’t have any performance drawback (unless the number of additional returned rows is big enough to make a difference, which would be weird).

WITH TIES use cases

Here are some practical use cases for WITH TIES.

Get all rows with the minimum or maximum value

Getting the row containing the maximum value is easy:

SELECT id, email
    FROM user
    ORDER BY registration_date DESC
    LIMIT 1;

But if there are several users that could match this condition, only one of them will be returned. To SELECT them all:

SELECT id, email
    FROM user
    ORDER BY registration_date DESC
    FETCH 1 ROW WITH TIES;

Without WITH TIES, the query would be something like this, and it would be slower that necessary:

SELECT id, email
    FROM user
    WHERE registration_date = (
        SELECT MAX(registration_date) FROM user
    );

Aggregating data in the application

DBMSs are very good at aggregating data (GROUP BY). And they have the aggregate functions that you normally want to use: minimum, maximum, arithmetic mean…

But what is you want to use a function that is not implemented, like the harmonic mean? Well, MariaDB supports stored aggregate functions, which means that you can implement the harmonic mean by yourself, in procedural SQL. Normally that would be the way to go, for example because it can avoid a lot of data roundtrip and lock time. But many teams don’t use procedural SQL for various reasons.

The way to emulate GROUP BY in an application would be something like this (pseudocode):

previousMonth = null
results = run_query(
        'SELECT month, cost FROM purchase WHERE year = 2021;'
    )
series = [ ]
while (row = results.fetch_row()):
    if previousMonth == row.month:
        series.push(previousMonth)
    else:
        print(harmonicMean(series))
        series = [ ]

This is not a sensible use of harmonic means. Please focus on the logic only.

The problem here is that selecting all the rows at once could lead to terrible performance. We need to limit the number of rows we get at once, but the number of rows to get is probably different for every month.

Again, we can use WITH TIES:

SELECT month, cost
    FROM purchase
    WHERE year = 2021 AND month = 1
    ORDER BY year, month
    FETCH 1 ROW WITH TIES;

Loop over non-continuous, non-unique values

In the simplest case, to loop over rows based on one column, you’d do something like this:

loop i in range(1, 100):
    run_query('SELECT id, full_name FROM employee WHERE category_id = ' + str(i))

But how would you do the same kind of loop over a videogame table, with based on the score column? The values would theoretically be anything from zero to a high number, with many holes in the middle, and the same scores may occur many times.

Again, we’ll use WITH TIES:

SELECT player, score FROM game WHERE score > @last_score FETCH 1 ROW WITH TIES;

WITH TIES bugs in MariaDB

MariaDB doesn’t have any relevant open bugs involving WITH TIES. If such bugs are reported in the future, I will try to keep this page up to date.

Last checked: 2021-12-12.

Conclusions

We discussed what the WITH TIES syntax does, and a couple of use cases. If you have more use cases, please drop a comment. See the WITH TIES bugs in MariaDB section if you notice any wrong / weird behaviour.

To master advanced SQL and query optimisation, consider our SQL optimisation training for teams.

Federico Razzoli

Did you like this article?

All content in this blog is distributed under the Creative Commons Attribution 4.0 International license (CC BY 4.0). You can use it for your needs and even modify it, but please refer to Vettabase and the author of the original post. Read more about the terms and conditions: https://creativecommons.org/licenses/by/4.0/

About Federico Razzoli
Federico Razzoli is a database professional, with a preference for open source databases, who has been working with DBMSs since year 2000. In the past 20+ years, he served in a number of companies as a DBA, Database Engineer, Database Consultant and Software Developer. In 2016, Federico summarized his extensive experience with MariaDB in the “Mastering MariaDB” book published by Packt. Being an experienced database events speaker, Federico speaks at professional conferences and meetups and conducts database trainings. He is also a supporter and advocate of open source software. As the Director of Vettabase, Federico does business worldwide but prefers to do it from Scotland where he lives.

Recent Posts

The UUID data type in MariaDB

The UUID data type in MariaDB

MariaDB introduced the UUID data type in version 10.7. The first long-term support (LTS) version to include it is 10.11, which was declared stable in February 2023.

A summary of MariaDB 10.10: INET4 type, RANDOM_BYTES() and more

A summary of MariaDB 10.10: INET4 type, RANDOM_BYTES() and more

In my first post in 2023, I want to give you a summary of MariaDB 10.10. Some may argue that I'm a bit late because it was released last November, and versions 10.11 and 11.0 have been announced since then... All that is correct, but 10.10 is the latest stable...

Services

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *