# Query with many WHERE clause question

**URL:** <https://forums.percona.com/t/query-with-many-where-clause-question/1084>\
**Category:** Other MySQL® Questions\
**Created:** [February 14, 2009, 1:55pm UTC](https://forums.percona.com/t/query-with-many-where-clause-question/1084 "2009-02-14T13:55:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fwang](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@fwang](https://forums.percona.com/u/fwang)\
**Post date:** [February 14, 2009, 1:55pm UTC](https://forums.percona.com/t/query-with-many-where-clause-question/1084/1 "2009-02-14T13:55:52Z")

</div>

confused: I have a InnoDB table look like this with (5,000,000 rows)

±-----------------------±-----------------±-----±----±--------------------±---------------+| Field | Type | Null | Key | Default | Extra |±-----------------------±-----------------±-----±----±--------------------±---------------+| item\_id | int(10) unsigned | NO | PRI | NULL | auto\_increment | | service\_id | int(10) unsigned | NO | | NULL | | | publish\_date | timestamp | NO | MUL | 0000-00-00 00:00:00 | | | guid | binary(16) | NO | MUL | NULL | | | user\_id | int(10) unsigned | NO | MUL | NULL | | | service\_type\_id | int(10) unsigned | NO | | NULL | | | item\_content\_id | int(10) unsigned | NO | MUL | NULL | | | parent\_item\_content\_id | int(10) unsigned | NO | MUL | 0 | | | source | char(31) | YES | | NULL | | | source\_guid | binary(16) | NO | | NULL | | | service\_item\_guid | binary(16) | NO | | NULL | | ±-----------------------±-----------------±-----±----±--------------------±---------------+

I’m trying to execute this query  
SELECT item\_id,item\_content\_id,service\_id,user\_id,service\_type\_id,H EX(guid) AS guid,publish\_date,source FROM tblitems WHERE parent\_item\_content\_id=0 AND service\_type\_id=101 AND user\_id in (174131135,180679365,192202991,214776398,234247197,314202862 ,366489655,447894579,526731760,556098048,556652039,561040515 ,638419537,651980840,668107789,701655633,706950612,782231381 ,801508556,861750775,883262712,910940633,938219324,996426916 ,1053063122,1086695272,1115537464,1184584418,1233832318,1238 904508,1249089491,1282863051,1312494447,1376282521,138160486 1,1385005897,1398891934,1402842598,1468859471,1529349460,154 3114389,1548353079,1549054617,1571914454,1659070871,16792789 87,1710759448,1730623872,1828437160,1832984639,1854336685,19 05636426,1954649947,1968225540,2104630051,2120640491,2135502 244,2137025828,2237617232,2239168055,2261941941,2320472868,2 419062188,2450748621,2451715591,2513198664,2561373602,261542 5018,2619967012,2625382898,2665924945,2681411117,2730829516, 2758953497,2808142619,2880570142,2886001685,2898360362,29007 59488,2925588951,2987536684,2999878127,3001889291,3019677265 ,3031201704,3135304259,3181534725,3265244011,3272800226,3286 774688,3290666357,3299691500,3327119216,3348536029,341292498 6,3416765413,3436263261,3490927984,3557766570,3561494533,356 2545987,3606791845,3614305201,3739342454,3868617597,38769940 02,3894695880,3904528983,3936052192,3947685092,3955749448,39 61975961,3976397517,4025756315,4117134136,4125308731,4255684 738) ORDER BY publish\_date DESC, item\_id DESC LIMIT 0,20;

I’ve tried the following indexes:

- (parent\_item\_content\_id,publish\_date)
- (user\_id)

The query runs in 200ms.

Is there a ways to speed up the query?

THANKS IN ADVANCE

---

<div class="post-metadata">

**Author:** ![januzi](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@januzi](https://forums.percona.com/u/januzi)\
**Post date:** [February 14, 2009, 2:40pm UTC](https://forums.percona.com/t/query-with-many-where-clause-question/1084/2 "2009-02-14T14:40:58Z")

</div>

Chop query to the simplier form:  
SELECT \* FROM tblitems WHERE parent\_item\_content\_id = 0 AND service\_type\_id = 101 AND user\_id in (174131135)

Check what explain says:  
Explain SELECT \* FROM tblitems WHERE parent\_item\_content\_id = 0 AND service\_type\_id = 101 AND user\_id in (174131135)

What about index (parent\_item\_content\_id, service\_type\_id, user\_id ) ?

Check also how many records there are in Your primary query : select count(\*) from tblitems where [that huge condition].

---

<div class="post-metadata">

**Author:** ![fwang](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@fwang](https://forums.percona.com/u/fwang)\
**Post date:** [February 14, 2009, 3:32pm UTC](https://forums.percona.com/t/query-with-many-where-clause-question/1084/3 "2009-02-14T15:32:24Z")

</div>

januzi, Thank you for you quick reply

I added the index (parent\_item\_content\_id , service\_type\_id , user\_id)

Explain SELECT \* FROM tblitems WHERE parent\_item\_content\_id = 0 AND service\_type\_id = 101 AND user\_id in (174131135)

±—±------------±---------±-----±------------------------------------------------------±-----±--------±------------------±-----±------+| id | select\_type | table | type | possible\_keys | key | key\_len | ref | rows | Extra |±—±------------±---------±-----±------------------------------------------------------±-----±--------±------------------±-----±------+| 1 | SIMPLE | tblitems | ref | tblitems\_user\_id,tblitems\_parent\_item\_content\_id,test | test | 12 | const,const,const | 1 | | ±—±------------±---------±-----±------------------------------------------------------±-----±--------±------------------±-----±------+

The results looks very good. However, if I run the original query, explain gives me:

±—±------------±---------±------±------------------------------------------------------±-----±--------±-----±-----±----------------------------+| id | select\_type | table | type | possible\_keys | key | key\_len | ref | rows | Extra |±—±------------±---------±------±------------------------------------------------------±-----±--------±-----±-----±----------------------------+| 1 | SIMPLE | tblitems | range | tblitems\_user\_id,tblitems\_parent\_item\_content\_id,test | test | 12 | NULL | 6949 | Using where; Using filesort | ±—±------------±---------±------±------------------------------------------------------±-----±--------±-----±-----±----------------------------+

and the query takes 180ms

It’s much better, but is it possible to get under 50ms?

Thanks again

---

<div class="post-metadata">

**Author:** ![januzi](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@januzi](https://forums.percona.com/u/januzi)\
**Post date:** [February 14, 2009, 7:07pm UTC](https://forums.percona.com/t/query-with-many-where-clause-question/1084/4 "2009-02-14T19:07:32Z")

</div>

So, we have got ~7000 matching records. Mysql has to fetch them all, order by specified columns and return only 20 newest.

1. Now mysql needs 180ms to return right data and application/script does something with those records (like showing them).
2. How about: return everything without order and sort data in application/script ?

Could You check the time needed in both cases ?

---

<div class="post-metadata">

**Author:** ![fwang](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@fwang](https://forums.percona.com/u/fwang)\
**Post date:** [February 14, 2009, 10:36pm UTC](https://forums.percona.com/t/query-with-many-where-clause-question/1084/5 "2009-02-14T22:36:39Z")

</div>

Thanks again januzi. I’ll try your solution.

Well, I’m wondering if it is possible to use indexes such as  
(parent\_item\_content\_id, service\_type\_id, user\_id, publish\_date).

What i found out was that when i use the composite index  
(parent\_item\_content\_id, publish\_date), the ‘Using filesort’ won’t be there in Extra.
