Skip to content
Advertisement

MariaDB Pivot Table Performace

I have a table containing data with dynamic categories:

I need a pivoted version of this table which I use with statement:

This creates results in the following format:

This query by itself is performing well also for many categories and table entries (e.g., for 8 categories and 240k entries, it takes around 20ms), but if i wrap this exact query in a select * from <query>, performance drops significantly (to 650ms).

Also, ordering by user_id does not influence the performance significantly, whereas ordering by any other field also causes a performance drop, even if an index of the respective field and user_id exists. I’m guessing that this approach in itself is not feasible for larger tables? However, I am curious what causes the extra execution time when adding the select * from <query part.

Background: I try to use this query to store dynamic user data, and I’d like to prevent changes to the table structure at runtime (i.e., adding a column). Any alternative would be welcome. I’m using MariaDB 10.5.5, and I need the solution to also work with MySQL 5.7 and SQL Server 2019.

Execution Plans:

Without surrounding select * from:

With surrounding select * from:

Advertisement

Answer

Here is my speculation on what is happening.

You have an index on the underlying table which MariaDB uses for the aggregation. That means that no sorting is done . . . and just by reading the index, it can start returning rows.

This is a very nice feature. But when you just run the query, you are seeing the time to first row.

When you use a derived table, MariaDB must generate all the rows before returning any of them. So, the select * with a subquery is doing much more work.

That is why the second version is slower than the first. I would expect a query that returns many tens of thousands of rows to take more than 20 milliseconds on most machines.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement