Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MySQL parallelize UNION subqueries (or anything at all)?

I use a partitioned table with a large amount of data. According to MySQL docs, it is on the ToDo list that:

Queries involving aggregate functions such as SUM() and COUNT() can easily be parallelized.

... but, can I achieve the same functionality using UNION subqueries? Are they parallelized, or do I have to create a multithreaded client to run concurrent queries with all the possible partition keys?


Edit:

The question is not strictly about UNION or subqueries. I would like to utilize as many cores as possible for my queries. Is there any way to do this (and make sure it's done) without paralellizing my application?

Any good documentation about MySQL's current parallelizing capabilities?

like image 410
vbence Avatar asked Jul 28 '11 15:07

vbence


2 Answers

As far as I know, currently the only way to use more than one thread/core to run queries in your application, is to use more than one connection. This of course makes it impossible to run parallel queries that are part of a single transaction.

like image 160
Mchl Avatar answered Sep 21 '22 00:09

Mchl


The different queries that are UNIONed together in one larger query aren't really subqueries, strictly speaking.

  • The queries are run in order
  • The data type of the columns is determined by the first query
  • By default, identical rows are dropped (UNION defaults to DISTINCT)
  • The result set is not finished building until all queries are run

...there is no way to parallelize the different queries, as they are all really part of the same query.

You may want to try runing the different queries in parallel from your code, and then mashing the results up together in your code once the queries all complete.

The documentation on UNIONs can be found here.

like image 23
TehShrike Avatar answered Sep 21 '22 00:09

TehShrike