Home » Performance » MySQL » 1 union Vs. 14 separate queries
1 union Vs. 14 separate queries [message #90] Tue, 22 August 2006 02:42 Go to next message
shokal  is currently offline shokal
Messages: 1
Registered: August 2006
Junior Member
hi.

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.

Thanks,
shokal.
Re: 1 union Vs. 14 separate queries [message #91 is a reply to message #90 ] Tue, 22 August 2006 03:59 Go to previous message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
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.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Previous Topic:Query with inner SELECT is slow
Next Topic:Queries are slowing down as table grows
Goto Forum:
  


Current Time: Fri Jul 10 06:31:32 EDT 2009

Total time taken to generate the page: 0.00446 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software

MySQL is a trademark of Sun Microsystems.
InnoDB is a trademark of Oracle Corp.

Percona Performance Forums are a service of Percona, Inc.
Not affiliated with Sun Microsystems or Oracle Corp.