# Query performs well if it is not optimized

**URL:** <https://forums.percona.com/t/query-performs-well-if-it-is-not-optimized/623>\
**Category:** Other MySQL® Questions\
**Created:** [February 7, 2008, 5:53am UTC](https://forums.percona.com/t/query-performs-well-if-it-is-not-optimized/623 "2008-02-07T05:53:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ch\_roy](https://avatars.discourse-cdn.com/v4/letter/c/858c86/32.png) [@ch\_roy](https://forums.percona.com/u/ch_roy)\
**Post date:** [February 7, 2008, 5:53am UTC](https://forums.percona.com/t/query-performs-well-if-it-is-not-optimized/623/1 "2008-02-07T05:53:04Z")

</div>

Hi,

If I issue this query, the results come very fast.  
SELECT SUM(metric\_diff) FROM snapshot\_master WHERE snapshot\_master.metric\_id = 5 AND snapshot\_master.timestamp\_id in (select timestamp\_id from timestamp\_master where server\_timestamp \>= 1202091795 AND server\_timestamp \<= 1202113395 and strftime(“%Y-%m-%d %H:%M”, datetime(server\_timestamp, ‘unixepoch’,  
‘localtime’))  
= (select strftime(“%Y-%m-%d %H:%M”, datetime(server\_timestamp, ‘unixepoch’,  
‘localtime’)) from timestamp\_master where timestamp\_id = 36000))

If I remove the sub-queries and try and join the tables, then the query takes a lot of time.

The tables are well designed, PK is on the metric\_id and timestamp\_id columns, even I have created indexes on the server\_timestamp column.

Am I missing something?

Chn
