November 27, 2014

Increasing slow query performance with the parallel query execution

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.

Conclusion

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).

About 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.

Comments

  1. aftab says:

    Thanks Alex, have you tried to run your split queries sequentially? if so, what was the total time?

  2. 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.

  3. Justin Swanhart says:

    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.

    Thanks

  4. 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.

  5. Justin Swanhart says:

    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.

  6. Justin Swanhart says:

    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

  7. Justin, sure, good point, I will test it out with the same data on the same server and write a follow-up post. Thank you for pointing it out!

  8. Justin Swanhart says:

    Hi,

    Let me know if you need any assistance.

    Thanks!

  9. Rick James says:

    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?

  10. Justin Swanhart says:

    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.

  11. Sergey says:

    Is it possible to hire Alexander for tuning my project ?

  12. Sergey, sure I will be more than happy to help you via our Consulting (http://www.percona.com/products/mysql-consulting/overview) offering. I will send you a separate email as well.

Speak Your Mind

*