# UNION and MySQL Query Cache

**URL:** <https://forums.percona.com/t/union-and-mysql-query-cache/1244>\
**Category:** Other MySQL® Questions\
**Created:** [August 25, 2009, 9:18am UTC](https://forums.percona.com/t/union-and-mysql-query-cache/1244 "2009-08-25T09:18:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![djuntgen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/djuntgen/32/815_2.png) [@djuntgen](https://forums.percona.com/u/djuntgen)\
**Post date:** [August 25, 2009, 9:18am UTC](https://forums.percona.com/t/union-and-mysql-query-cache/1244/1 "2009-08-25T09:18:31Z")

</div>

Hi -

In this post: [http://www.mysqlperformanceblog.com/2006/07/27/mysql-query-c](http://www.mysqlperformanceblog.com/2006/07/27/mysql-query-c) ache/

Peter says that “parts of a UNION” may not be cached.

I don’t understand this, maybe I don’t understand fully how MySQL executes a UNION. So my question is can a UNION be cached and if it can which parts cannot?

Thanks,

–Dave

---

<div class="post-metadata">

**Author:** ![vgatto](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/vgatto/32/748_2.png) [@vgatto](https://forums.percona.com/u/vgatto)\
**Post date:** [August 25, 2009, 10:53am UTC](https://forums.percona.com/t/union-and-mysql-query-cache/1244/2 "2009-08-25T10:53:18Z")

</div>

I think the article is just explaining that the query cache is a very simple mapping between the text of query and its result set. MySQL doesn’t really understand the query beyond it being a collection of bytes for the purposes of query cache lookup.

So if you execute

SELECT id FROM widgets WHERE state = 0  
UNION  
SELECT id FROM widgets WHERE state = 1

you should expect the result set for that exact query to be cached, and not the result set for

SELECT id FROM widgets WHERE state = 0

or the result set for

SELECT id FROM widgets WHERE state = 1

unless the queries are independently executed.

You can verify this by watching SHOW STATUS LIKE ‘Qcache%’ on a quiet server.
