Buy Percona ServicesBuy Now!

Increasing slow query performance with the parallel query execution

 | January 7, 2014 |  Posted In: MySQL


MySQL and Scaling-up (using more powerful hardware) was always a hot topic. Originally MySQL did not scale well with multiple CPUs; there were times when InnoDB performed poorer with more  CPU cores than with less CPU cores. MySQL 5.6 can scale significantly better; however there is still 1 big limitation: 1 SQL query will eventually use only 1 CPU core (no parallelism). Here is what I mean by that: let’s say we have a complex query which will need to scan million of rows and may need to create a temporary table; in this case MySQL will not be able to scan the table in multiple threads (even with partitioning) so the single query will not be faster on the more powerful server. On the contrary, a server with more slower CPUs will show worse performance than the server with less (but faster) CPUs.

To address this issue we can use a parallel query execution. Vadim wrote about the PHP asynchronous calls for MySQL. Another way to increase the parallelism will be to use “sharding” approach, for  example with Shard Query. I’ve decided to test out the parallel (asynchronous) query execution with relatively large table: I’ve used the US Flights Ontime performance database, which was originally used by Vadim in the old post Analyzing air traffic performance. Let’s see how this can help us increase performance of the complex query reports.

Parallel Query Example

To illustrate the parallel query execution with MySQL I’ve created the following table:

And loaded 26 years of data into it. The table is 56G with ~152M rows.

Software: Percona 5.6.15-63.0. Hardware: Supermicro; X8DTG-D; 48G of RAM; 24xIntel(R) Xeon(R) CPU L5639 @ 2.13GHz, 1xSSD drive (250G)

So we have 24 relatively slow CPUs

Simple query

Now we can run some queries. The first query is very simple: find all flights per year (in the US):

As we have the index on YearD, the query will use the index:

The query is simple, however, it will have to scan 150M rows. Here is the results of the query (cached):

The query took 54 seconds and utilized only 1 CPU core. However, this query is perfect for running in parallel.  We can run 26 parallel queries, each will count its own year. I’ve used the following shell script to run the queries in background:

Here are the results:

So the total execution time is ~5 (10x faster) seconds. Each individual results are here:

Complex Query 

Now we can try more complex query. Lets imagine we want to find out which airlines have maximum delays for the flights inside continental US during the business days from 1988 to 2009 (I was trying to come up with the complex query with multiple conditions in the where clause).

As the query has “group by” and “order by” plus multiple ranges in the where clause it will have to create a temporary table:

(for this query I’ve created the combined index: KEY comb1 (Carrier,YearD,ArrDelayMinutes)  to increase performance)

The query runs in ~15 minutes:


Now we can split this query and run the 31 queries (=31 distinct airlines in this table) in parallel. I have used the following script:

In this case we will also avoid creating temporary table  (as we have an index which starts with carrier).

Results: total time is 5 min 47 seconds (3x faster)

Per query statistics:

As we can see there are large airlines (like AA, UA, US, DL, etc) which took most of the time. In this case the load will not be distributed evenly as in the previous example; however, by running the query in parallel we have got 3x times better response time on this server.

CPU utilization:

Note that in case of “order by” we will need to manually sort the results, however, sorting 10-100 rows will be fast.


Splitting a complex report into multiple queries and running it in parallel (asynchronously) can increase performance (3x to 10x in the above example) and will better utilize modern hardware. It is also possible to split the queries between multiple MySQL servers (i.e. MySQL slave servers) to further increase scalability (will require more coding).

Alexander Rubin

Alexander joined Percona in 2013. Alexander worked with MySQL since 2000 as DBA and Application Developer. Before joining Percona he was doing MySQL consulting as a principal consultant for over 7 years (started with MySQL AB in 2006, then Sun Microsystems and then Oracle). He helped many customers design large, scalable and highly available MySQL systems and optimize MySQL performance. Alexander also helped customers design Big Data stores with Apache Hadoop and related technologies.


  • Aftab,

    Yes, I’ve modified the script for the complex query example and run it without placing “mysql” cli in the background:
    total time: 16m42.318s (compared to 5 min)

    Per query statistics:

    seq_sql_complex/9E.log:1 row in set (10.80 sec)
    seq_sql_complex/AA.log:1 row in set (1 min 59.09 sec)
    seq_sql_complex/AL.log:1 row in set (2.17 sec)
    seq_sql_complex/AQ.log:1 row in set (0.80 sec)
    seq_sql_complex/AS.log:1 row in set (23.96 sec)
    seq_sql_complex/B6.log:1 row in set (10.56 sec)
    seq_sql_complex/CO.log:1 row in set (1 min 1.11 sec)
    seq_sql_complex/DH.log:1 row in set (3.97 sec)
    seq_sql_complex/DL.log:1 row in set (2 min 10.92 sec)
    seq_sql_complex/EA.log:1 row in set (4.72 sec)
    seq_sql_complex/EV.log:1 row in set (23.61 sec)
    seq_sql_complex/F9.log:1 row in set (4.35 sec)
    seq_sql_complex/FL.log:1 row in set (13.78 sec)
    seq_sql_complex/HA.log:1 row in set (3.26 sec)
    seq_sql_complex/HP.log:1 row in set (25.43 sec)
    seq_sql_complex/ML.log:1 row in set (0.46 sec)
    seq_sql_complex/MQ.log:1 row in set (35.88 sec)
    seq_sql_complex/NW.log:1 row in set (1 min 10.91 sec)
    seq_sql_complex/OH.log:1 row in set (10.42 sec)
    seq_sql_complex/OO.log:1 row in set (35.20 sec)
    seq_sql_complex/PA.log:1 row in set (1.79 sec)
    seq_sql_complex/PI.log:1 row in set (4.66 sec)
    seq_sql_complex/PS.log:1 row in set (0.22 sec)
    seq_sql_complex/RU.log:1 row in set (7.70 sec)
    seq_sql_complex/TW.log:1 row in set (27.48 sec)
    seq_sql_complex/TZ.log:1 row in set (1.13 sec)
    seq_sql_complex/UA.log:1 row in set (1 min 42.88 sec)
    seq_sql_complex/US.log:1 row in set (1 min 44.81 sec)
    seq_sql_complex/WN.log:1 row in set (2 min 18.02 sec)
    seq_sql_complex/XE.log:1 row in set (12.55 sec)
    seq_sql_complex/YV.log:1 row in set (9.47 sec)

    As we can see the maximum query execution times decreased (2 min vs 5 min), however, by running it in parallel we have significant faster response.

  • Last time I checked we already had a parallel query tool called Shard-Query.

    It is much more full featured than such a simple script (though it started out basically like one).

    Please take the time to look into it before reinventing the wheel.


  • Justin, yes, I’ve mention the ShardQuery in the beginning of the post. The idea is to show how MySQL 5.6 will handle the parallel query execution load, I’m not trying to re-invent the wheel of cause.

  • My point is that you spent time writing and testing a script, etc, when you could have actually tested and blogged about shard-query. It has a SQL explain feature and you can explain exactly what it is doing just as easily as this. I can’t be the only one to blog about it.

  • Just a minor correction to your post then : Shard-Query doesn’t need sharding. It works with a single table. You could use an IN clause to get the same results with shard-query, or just partition the table and it would “just work”.

    step 1)
    create partitioned table
    step 2)
    load data into partitioned table
    step 3)
    set up shard query to use one server
    step 4)
    access server with shard-query and voila instant parallel queries
    step 5)
    access shard-query with mysql-proxy and voila parallel transparent mysql server for OLAP queries

  • Really big queries end up being I/O bound. I suspect (without proof) that parallelizing I/O bound queries will show a smaller improvement — because the I/O subsystem is limited. RAID striping (-1/5/10) may or may not help; it depends on how well the original, non-parallelized, query could make use of read-ahead.

    Do either of you have good benchmarks of such?

  • Hi Rick,

    The idea here is to get as close to sequential reads as possible over many partitions. In order for that to work best you have to either use SSD or RAID with many spindles, ideally using MySQL 5.6 to place partitions on different devices. For example, in EC2 you could use a large instance with four EBS devices, each RAID0 and place a subset of partitions on each of the EBS devices. Or you could stripe all four together – it depends on your use case. For SSD, parallel query is ideal, because a single thread can’t read more than 50MB/sec or so in MySQL. Multiple threads can sustain much higher IO rates on the SSD devices.

    Eventually though, you will hit an IO bottleneck on the server.

    Here you typically look at more options and they can be combined:
    a) column store – this reduces IO by reducing the amount of data that has to be examined for large queries, especially on wide fact tables
    b) sharding – queries are distributed over multiple nodes, where each node has a subset of the total data set. Most sharding solutions can’t distribute OLAP queries, but Shard-Query can
    c) materialized views – insert-only data sets are easy to materialize, and Flexviews supports incrementally refreshing materialized views over data sets with updates. OLAP tools like Mondrian can automatically use the materialized views (summary tables) to answer queries, reducing IO dramatically.

    The optimal solution is usually combining a column store with sharding or materialized views with sharding. A combination of all three would work, but MySQL column stores don’t record binlogs or support triggers, so Flexviews can’t be used with them.

    I will do some benchmarks and post the results.

  • I’ve used this technique but it seems like the threads in mysql are getting stalled. prstat reports cpu usage low. iostat reports low io usage. Sometimes the same query (same criteria) is 10 secs and other times it’s 2 minutes.

    I’ve set transaction isolation globally to READ UNCOMMITTED (perfect for us, we are ok with dirty reads). Table is partitioned to match the sharding, and WHERE includes the partition hash value. Our app is primarily single-user.

    I’m still on 5.5.37, perhaps I should upgrade to 5.6. Are there issues where these threads might have lock contention with each other? I am using read-uncommitted so it shouldnt lock anyway.

    Thanks in advance for any suggestions you might have!

  • @Justin Swanhart doesn’t Shard-Query require partitioning, yet this solution does not? I am not ready to give up my foreign-keys any time soon which you cannot have on partitioned tables. Is there really no RDBMS or storage engine that will automatically parallelize queries?

    • I ran multiple queries in parallel on the same MySQL instance in a way very similar to the one described in this article using MySQL 5.6.23 with SSD. I easily got order of magnitude performance improvement. I was able to run up to 100 queries in parallel without significant I/O bottleneck. Once I exceeded this approximate 100 threshold the queue of queries started to get significantly longer. It is transparent and reliable solution.

      I tried to install Justin Swanhart’s Shard-Query solution, but got nasty installation errors and gave up after spending some time.

      The case when you have to run multiple almost identical queries with difference in only one or two field values is very common.

      It is essentially queries that look like
      SELECT a,b,c,d, …
      WHERE ID IN (1,2,3,…N);

      or something like that. You can split it on N queries and run them in parallel. MySQL is great in multi-threading.

      This problem is very low hanging fruit waiting to be picked up.


  • can you tell me how to split queries between multiple MySQL servers (slave servers) ?. I have set up one master and one slave but i don´t know how to split the query in both

  • So, i should write a program to split mysql queries in the master and in the slave?

    i mean, something like this but openning a connection to the slave too to send queries of some years

    for y in {1988..2013}
    sql=”select yeard, count(*) from ontime where yeard=$y”
    mysql -vvv ontime -e “$sql” &>par_sql1/$y.log &

    • Correct. For the reporting purposes you can potentially use Apache Spark ( So you will need:
      1. Virtual IP across multiple servers (i.e. HAproxy or ProxySQL)
      2. Apache spark + JDBC to connect to MySQL.

Comments are closed