MySQL UNION performance vs separate queries

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

MySQL UNION performance vs separate queries

Post by batfastad »

Hi guys

On our intranet system I'm getting counts of related records from various tables... there's 8 record counts I need to get.
There's already 4 other queries on this particular page which get more specific information.
At the moment I'm using UNION to combine all these into a single query then lopping through those results in PHP and setting them to variable variables.

In theory what should give better performance:
8 individual queries from PHP?
Or just a single query with 8 statements joined by UNION?

I would think UNION should give slightly better performance because there'd be less overheard but I'm no expert... there's probably something with UNION that I'm missing.

Cheers, B
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL UNION performance vs separate queries

Post by Eran »

UNION should give better concurrency (since there's less overhead than 8 separate queries). It might give a small performance boost, but probably nothing significant
Post Reply