1 union Vs. 14 separate queries


I write a CMS system for a publishing service in php/mysql.

On many of the pages I have several queries, usually more than 10. most of them even return the same result set though not all of them.

I wanted to ask if it is performance-wise to combine all those separate queries into 1 unioned query?

My logic tells me that accessing the database only once is faster than accessing it several times synchronisly, and that the accumulative query time of all queries will be the same as the query time of the union.

Am I right?

Of course there is a downgrade in the code - I have to select dummy nulls in several queries to comply with the entire union result set, but i can live with that if the improvement in response time is significant.


If you have queries which return result sets of different structures you should not probably use union. You will have problems with different data types not to mention with finding which result set corresponds to which query.

If you really worry about latency and queries are independent and you can issue them in a bulk you can use multiple statement execution API which is supported by MySQL 4.1+ - which allows you to get multiple of different results with single request submission.

Note also - 14 queries is not that many. Over fast local network you normally can run tens of thousands of queries per second.