# Query Optimization - Indexes

**URL:** <https://forums.percona.com/t/query-optimization-indexes/1277>\
**Category:** Other MySQL® Questions\
**Created:** [October 15, 2009, 11:43pm UTC](https://forums.percona.com/t/query-optimization-indexes/1277 "2009-10-15T23:43:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gangof8](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gangof8](https://forums.percona.com/u/gangof8)\
**Post date:** [October 15, 2009, 11:43pm UTC](https://forums.percona.com/t/query-optimization-indexes/1277/1 "2009-10-15T23:43:37Z")

</div>

Hi guys, I’m a new member; however, not new to the community ) BTW, my name is Sam. I’ve a question regarding query optimization and would like to seek your help.

Here’s my query:  
SELECT COUNT(item.itemId) as itemCount, stores.storeId, stores.name FROM bus\_stores stores LEFT JOIN bus\_items item ON item.businessId = stores.businessId and item.status=‘completed’ WHERE stores.status=‘active’ GROUP BY stores.storeId ORDER BY itemCount DESC LIMIT 0, 20

Explain:  
id select\_type table type possible\_keys key key\_len ref rows Extra  
1 SIMPLE stores index PRIMARY 8 47 Using where; Using temporary; Using filesort  
1 SIMPLE item ref i\_businessid\_status i\_businessid\_status 10 aceonline.stores.businessid,const 9 Using where

Index Information:  
bus\_stores: storeId is PK; Table size: 3000 records  
bus\_items: itemId is the PK, KEY i\_businessid\_status(businessid, status); Table size: 1 million records

The above query is taking about 5 sec to execute, which is a real shame.

Could anyone, plese, shed some light on how to optimize this query for better performance?

Thanks

---

<div class="post-metadata">

**Author:** ![sterin71](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@sterin71](https://forums.percona.com/u/sterin71)\
**Post date:** [October 16, 2009, 7:33am UTC](https://forums.percona.com/t/query-optimization-indexes/1277/2 "2009-10-16T07:33:03Z")

</div>

You have done most of the things correct.

The only thing that I can debate (and it is bit slim) is the COUNT(item.itemId).  
Depending on storage engine I think (big disclaimer 😉 ) that a COUNT(item.businessId) would perform better. Since that column is part of the index it doesn’t have to read that information from the table.

So instead we have to look at what your server variables and server looks like.

So what does the output from “SHOW VARIABLES” look like and which storage engine are you using (SHOW CREATE TABLE …)?

---

<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:** [October 16, 2009, 2:03pm UTC](https://forums.percona.com/t/query-optimization-indexes/1277/3 "2009-10-16T14:03:57Z")

</div>

| [B]sterin71 wrote on Fri, 16 October 2009 15:03[/B] |
| 

The only thing that I can debate (and it is bit slim) is the COUNT(item.itemId).  
Depending on storage engine I think (big disclaimer 😉 ) that a COUNT(item.businessId) would perform better.

 |

You are correct; it will work for both InnoDB and MyISAM.

---

<div class="post-metadata">

**Author:** ![gangof8](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gangof8](https://forums.percona.com/u/gangof8)\
**Post date:** [October 17, 2009, 6:01am UTC](https://forums.percona.com/t/query-optimization-indexes/1277/4 "2009-10-17T06:01:24Z")

</div>

Thanks guys! I’ve changed to COUNT(item.businessid) and the response time is 0.17 secongs. Great.

Thanks for you input. Nevertheless, I’m little wondering how did this impact the performance! Is it just because businessid is already indexed?

---

<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:** [October 17, 2009, 9:01am UTC](https://forums.percona.com/t/query-optimization-indexes/1277/5 "2009-10-17T09:01:18Z")

</div>

It now suffices to read the key on (businessid, status). The corresponding row does not need to be accessed for retrieving additional data (such as item.Itemid). Moreover, because businessid is the first part of the key, finding all entries where businessid equals stores.businessid is fast.

---

<div class="post-metadata">

**Author:** ![gangof8](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@gangof8](https://forums.percona.com/u/gangof8)\
**Post date:** [October 18, 2009, 3:14pm UTC](https://forums.percona.com/t/query-optimization-indexes/1277/6 "2009-10-18T15:14:51Z")

</div>

That sounds very sensible. I had a slightly different notion; however, clarifired - thanks to your clarification.

I truly appreciate the response I had on this forum and it seems to be promising in here. I would certainly look forward to take part in the forum actively…  
Cheers
