# WordPress & multiple queries or 1 query with join?

**URL:** <https://forums.percona.com/t/wordpress-multiple-queries-or-1-query-with-join/1465>\
**Category:** Other MySQL® Questions\
**Created:** [August 4, 2010, 3:11pm UTC](https://forums.percona.com/t/wordpress-multiple-queries-or-1-query-with-join/1465 "2010-08-04T15:11:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikeschinkel](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/mikeschinkel/32/833_2.png) [@mikeschinkel](https://forums.percona.com/u/mikeschinkel)\
**Post date:** [August 4, 2010, 3:11pm UTC](https://forums.percona.com/t/wordpress-multiple-queries-or-1-query-with-join/1465/1 "2010-08-04T15:11:05Z")

</div>

Hi all,

New to the forum, glad to find it.

I work with WordPress and we’re having a debate on the WordPress developers mailing list about which is better: 1 query with joins or multiple queries? This is a statement that was made on the list:

| [B]Quote:[/B] |
| [I]Complex queries are the death of WP sites. Consider that in most of the webhosting in this world, database servers are shared amongst many websites. So you want to rely on the database as little as possible.

Two simpler queries are often preferable to one query which adds load  
to the database server. Why? Because the DB server is generally your  
major bottleneck. Loading any sort of work onto it with JOINs and the  
like causes you nothing but trouble in the long run.

Note, I’m not commenting on the specific code, just making a general  
observation. If I can do two or three queries with no joins, it’s  
usually a heck of a lot faster than one complicated query with two or  
three joins. Less queries is not always better.[/I]

 |

That goes against everything I’d learned in the past, but maybe he is right?

Here’s some psuedo-code mixing MySQL and PHP showing the different choices we were discussing:

1.) $slug = 'actor’SELECT p.post\_title FROM wp\_posts pINNER JOIN wp\_term\_relationships tr ON p.ID=tr.object\_id INNER JOIN wp\_term\_taxonomy tt ON tr.term\_taxonomy\_id=tt.term\_taxonomy\_idINNER JOIN wp\_terms t ON tt.term\_id=t.term\_idWHERE t.slug=$slug2.) $slug = ‘actor’$result = SELECT tr.object\_id FROM wp\_term\_relationships tr INNER JOIN wp\_term\_taxonomy tt ON tr.term\_taxonomy\_id=tt.term\_taxonomy\_idINNER JOIN wp\_terms t ON tt.term\_id=t.term\_idWHERE t.slug=$slugforeach($result as $row) $ids = $row-\>object\_idSELECT post\_title FROM wp\_posts WHERE ID IN (ids)

Thanks in advance for your help in understanding this.

---

<div class="post-metadata">

**Author:** ![gmouse](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@gmouse](https://forums.percona.com/u/gmouse)\
**Post date:** [August 4, 2010, 4:23pm UTC](https://forums.percona.com/t/wordpress-multiple-queries-or-1-query-with-join/1465/2 "2010-08-04T16:23:49Z")

</div>

He is not right.

The main problem of wordpress is that where and order by clauses can not be satisfied by indices because the where’s are on one table and order by on another. That is what makes wp a pita for hosters of large blogs. Optimization will involve major database changes and denormalization.

---

<div class="post-metadata">

**Author:** ![mikeschinkel](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/mikeschinkel/32/833_2.png) [@mikeschinkel](https://forums.percona.com/u/mikeschinkel)\
**Post date:** [August 4, 2010, 4:33pm UTC](https://forums.percona.com/t/wordpress-multiple-queries-or-1-query-with-join/1465/3 "2010-08-04T16:33:18Z")

</div>

Thanks for the reply, but it doesn’t really answer the question. Instead is sounded just like an anti-WordPress rant…

| [B]Quote:[/B] |
| The main problem of wordpress is that where and order by clauses can not be satisfied by indices because the where's are on one table and order by on another. |

Can you be more specific?

---

<div class="post-metadata">

**Author:** ![gmouse](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@gmouse](https://forums.percona.com/u/gmouse)\
**Post date:** [August 5, 2010, 5:11am UTC](https://forums.percona.com/t/wordpress-multiple-queries-or-1-query-with-join/1465/4 "2010-08-05T05:11:37Z")

</div>

The answer to your question is not really interesting. Both queries are fast and you will not notice the difference.

I have looked at wordpress a long time ago, but immediately gave up because it would be faster to start from scratch for what I needed. What I posted is from memory. Maybe you could provide some facts on queries. On [URL][http://core.trac.wordpress.org/ticket/7415[/URL]](http://core.trac.wordpress.org/ticket/7415%5B/URL%5D) you can find instructions on finding slow queries (start reading at SAVEQUERIES). Please do that for one large blog and post some results here so we can take a look at your queries.
