While Shard-Query can work over multiple nodes, this blog post focuses on using Shard-Query with a single node. Shard-Query can add parallelism to queries which use partitioned tables. Very large tables can often be partitioned fairly easily. Shard-Query can leverage partitioning to add paralellism, because each partition can be queried independently. Because MySQL 5.6 supports the partition hint, Shard-Query can add parallelism to any partitioning method (even subpartioning) on 5.6 but it is limited to RANGE/LIST partitioning methods on early versions.
The output from Shard-Query is from the commandline client, but you can use MySQL proxy to communicate with Shard-Query too.
In the examples I am going to use the schema from the Star Schema Benchmark. I generated data for scale factor 10, which means about 6GB of data in the largest table. I am going to show a few different queries, and explain how Shard-Query executes them in parallel.
Here is the DDL for the lineorder table, which I will use for the demo queries:
CREATE TABLE IF NOT EXISTS lineorder ( LO_OrderKey bigint not null, LO_LineNumber tinyint not null, LO_CustKey int not null, LO_PartKey int not null, LO_SuppKey int not null, LO_OrderDateKey int not null, LO_OrderPriority varchar(15), LO_ShipPriority char(1), LO_Quantity tinyint, LO_ExtendedPrice decimal, LO_OrdTotalPrice decimal, LO_Discount decimal, LO_Revenue decimal, LO_SupplyCost decimal, LO_Tax tinyint, LO_CommitDateKey int not null, LO_ShipMode varchar(10), primary key(LO_OrderDateKey,LO_PartKey,LO_SuppKey,LO_Custkey,LO_OrderKey,LO_LineNumber) ) PARTITION BY HASH(LO_OrderDateKey) PARTITIONS 8;
Notice that the lineorder table is partitioned by HASH(LO_OrderDateKey) into 8 partitions. I used 8 partitions and my test box has 4 cores. It does not hurt to have more partitions than cores. A number of partitions that is two or three times the number of cores generally works best because it keeps each partition small, and smaller partitions are faster to scan. If you have a very large table, a larger number of partitions may be acceptable. Shard-Query will submit a query to Gearman for each partition, and the number of Gearman workers controls the parallelism.
The SQL for the first demo is:
SELECT COUNT(DISTINCT LO_OrderDateKey) FROM lineorder;
Here is the explain from regular MySQL:
mysql> explain select count(distinct LO_OrderDateKey) from lineorder\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: lineorder type: index possible_keys: PRIMARY key: PRIMARY key_len: 25 ref: NULL rows: 58922188 Extra: Using index 1 row in set (0.00 sec)
So it is basically a full table scan. It takes a long time:
mysql> select count(distinct LO_OrderDateKey) from lineorder; +---------------------------------+ | count(distinct LO_OrderDateKey) | +---------------------------------+ | 2406 | +---------------------------------+ 1 row in set (4 min 48.63 sec)
Shard-Query executes this query differently from MySQL. It sends a query to each partition, in parallel like the following queries:
Array ( [0] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p0) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey [1] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p1) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey [2] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p2) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey [3] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p3) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey [4] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p4) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey [5] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p5) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey [6] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p6) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey [7] => SELECT LO_OrderDateKey AS expr_2839651562 FROM lineorder PARTITION(p7) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey )
You will notice that there is one query for each partition. Those queries will be sent to Gearman and executed in parallel by as many Gearman workers as possible (in this case 4.) The output of the queries go into a coordinator table, and then another query does a final aggregation. That query looks like this:
SELECT COUNT(distinct expr_2839651562) AS `count` FROM `aggregation_tmp_73522490`
The Shard-Query time:
select count(distinct LO_OrderDateKey) from lineorder; Array ( [count ] => 2406 ) 1 rows returned Exec time: 0.10923719406128
That isn’t a typo, it really is sub-second compared to minutes in regular MySQL.
This is because Shard-Query uses GROUP BY to answer this query and a loose index scan of the PRIMARY KEY is possible:
mysql> explain partitions SELECT LO_OrderDateKey AS expr_2839651562 -> FROM lineorder PARTITION(p7) AS `lineorder` WHERE 1=1 AND 1=1 GROUP BY LO_OrderDateKey -> \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: lineorder partitions: p7 type: range possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: NULL rows: 80108 Extra: Using index for group-by 1 row in set (0.00 sec)
Next another simple query will be tested, first on regular MySQL:
mysql> select count(*) from lineorder; +----------+ | count(*) | +----------+ | 59986052 | +----------+ 1 row in set (4 min 8.70 sec)
Again, the EXPLAIN shows a full table scan:
mysql> explain select count(*) from lineorder\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: lineorder type: index possible_keys: NULL key: PRIMARY key_len: 25 ref: NULL rows: 58922188 Extra: Using index 1 row in set (0.00 sec)
Now, Shard-Query can’t do anything special to speed up this query, except to execute it in parallel, similar to the first query:
[0] => SELECT COUNT(*) AS expr_3190753946 FROM lineorder PARTITION(p0) AS `lineorder` WHERE 1=1 AND 1=1 [1] => SELECT COUNT(*) AS expr_3190753946 FROM lineorder PARTITION(p1) AS `lineorder` WHERE 1=1 AND 1=1 [2] => SELECT COUNT(*) AS expr_3190753946 FROM lineorder PARTITION(p2) AS `lineorder` WHERE 1=1 AND 1=1 [3] => SELECT COUNT(*) AS expr_3190753946 FROM lineorder PARTITION(p3) AS `lineorder` WHERE 1=1 AND 1=1 ...
The aggregation SQL is similar, but this time the aggregate function is changed to SUM to combine the COUNT from each partition:
SELECT SUM(expr_3190753946) AS ` count ` FROM `aggregation_tmp_51969525`
And the query is quite a bit faster at 140.24 second compared with MySQL’s 248.7 second result:
Array ( [count ] => 59986052 ) 1 rows returned Exec time: 140.24419403076
Finally, I want to look at a more complex query that uses joins and aggregation.
mysql> explain select d_year, c_nation, sum(lo_revenue - lo_supplycost) as profit from lineorder join dim_date on lo_orderdatekey = d_datekey join customer on lo_custkey = c_customerkey join supplier on lo_suppkey = s_suppkey join part on lo_partkey = p_partkey where c_region = 'AMERICA' and s_region = 'AMERICA' and (p_mfgr = 'MFGR#1' or p_mfgr = 'MFGR#2') group by d_year, c_nation order by d_year, c_nation; +----+-------------+-----------+--------+---------------+---------+---------+--------------------------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+--------+---------------+---------+---------+--------------------------+------+---------------------------------+ | 1 | SIMPLE | dim_date | ALL | PRIMARY | NULL | NULL | NULL | 5 | Using temporary; Using filesort | | 1 | SIMPLE | lineorder | ref | PRIMARY | PRIMARY | 4 | ssb.dim_date.D_DateKey | 89 | NULL | | 1 | SIMPLE | supplier | eq_ref | PRIMARY | PRIMARY | 4 | ssb.lineorder.LO_SuppKey | 1 | Using where | | 1 | SIMPLE | customer | eq_ref | PRIMARY | PRIMARY | 4 | ssb.lineorder.LO_CustKey | 1 | Using where | | 1 | SIMPLE | part | eq_ref | PRIMARY | PRIMARY | 4 | ssb.lineorder.LO_PartKey | 1 | Using where | +----+-------------+-----------+--------+---------------+---------+---------+--------------------------+------+---------------------------------+ 5 rows in set (0.01 sec)
Here is the query on regular MySQL:
mysql> select d_year, c_nation, sum(lo_revenue - lo_supplycost) as profit from lineorder join dim_date on lo_orderdatekey = d_datekey join customer on lo_custkey = c_customerkey join supplier on lo_suppkey = s_suppkey join part on lo_partkey = p_partkey where c_region = 'AMERICA' and s_region = 'AMERICA' and (p_mfgr = 'MFGR#1' or p_mfgr = 'MFGR#2') group by d_year, c_nation order by d_year, c_nation; +--------+---------------+--------------+ | d_year | c_nation | profit | +--------+---------------+--------------+ | 1992 | ARGENTINA | 102741829748 | ... | 1998 | UNITED STATES | 61345891337 | +--------+---------------+--------------+ 35 rows in set (11 min 56.79 sec)
Again, Shard-Query splits up the query to run over each partition (I won’t bore you with the details) and it executes the query faster than MySQL, in 343.3 second compared to ~720:
Array ( [d_year] => 1998 [c_nation] => UNITED STATES [profit] => 61345891337 ) 35 rows returned Exec time: 343.29854893684
I hope you see how using Shard-Query can speed up queries without using sharding, on just a single server. All you really need to do is add partitioning.
You can get Shard-Query from GitHub at http://github.com/greenlion/swanhart-tools
Please note: Configure and install Shard-Query as normal, but simply use one node and set the column option (the shard column) to “nocolumn” or false, because you are not required to use a shard column if you are not sharding.
The post Parallel Query for MySQL with Shard-Query appeared first on MySQL Performance Blog.