# Unexplained higher CPU when using "union all"

**URL:** <https://forums.percona.com/t/unexplained-higher-cpu-when-using-union-all/10828>\
**Category:** Percona Server for MySQL 8.0\
**Created:** [June 10, 2021, 10:05am UTC](https://forums.percona.com/t/unexplained-higher-cpu-when-using-union-all/10828 "2021-06-10T10:05:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vamsi\_P](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/vamsi_p/32/2549_2.png) [@Vamsi\_P](https://forums.percona.com/u/Vamsi_P)\
**Post date:** [June 10, 2021, 10:05am UTC](https://forums.percona.com/t/unexplained-higher-cpu-when-using-union-all/10828/1 "2021-06-10T10:05:41Z")

</div>

Hi Folks,  
We are using MyRocks as key value store. To make it easy to port HBase use cases, our PK has rowkey, column family name, column name and version.

When implementing getMulti(), which retrieves keys from multiple shards (each shard is a separate MyRocks table) on the same host, we used union all, where each select inside union all retrieves keys from one shard.

The above seems like a basic use case of “union all”, but we were really surprised that if we just use N get() calls (where each get() call gets only one key), CPU usage is noticeably lower rather if we use getMulti() calls (where each getMulti() call retrieves N keys).

Wondering if any known major inefficiencies in “union all” implementation?

Thanks,  
Vamsi

---

<div class="post-metadata">

**Author:** ![matthewb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/matthewb/32/34_2.png) [@matthewb](https://forums.percona.com/u/matthewb)\
**Post date:** [July 6, 2021, 5:15pm UTC](https://forums.percona.com/t/unexplained-higher-cpu-when-using-union-all/10828/2 "2021-07-06T17:15:59Z")

</div>

Hi @Vamsi_P,  
MySQL can’t use more than 1 thread per query. So SELECT … UNION ALL … SELECT … UNION ALL… etc etc will only use 1 thread. However, if you ran N concurrent connections each executing a single-simple SELECT, the throughput will be much higher and CPU overall will be more efficiently utilized.
