# Query is Very SLow

**URL:** <https://forums.percona.com/t/query-is-very-slow/591>\
**Category:** Other MySQL® Questions\
**Created:** [January 8, 2008, 10:08pm UTC](https://forums.percona.com/t/query-is-very-slow/591 "2008-01-08T22:08:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jklanka](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jklanka](https://forums.percona.com/u/jklanka)\
**Post date:** [January 8, 2008, 10:08pm UTC](https://forums.percona.com/t/query-is-very-slow/591/1 "2008-01-08T22:08:25Z")

</div>

I have a table with 3222374 records

when I use “select count(\*) from table” it works within a second.

But When I use

select count(\*) from table where ts \>= ‘2007-01-31 05:21:18’ AND ts \<= ‘2008-01-09 05:21:18’

It takes around 20 seconds to get the result.

I have created a index field on ts ( which is DATETIME type field )

Please help on this.

---

<div class="post-metadata">

**Author:** ![vicklik](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@vicklik](https://forums.percona.com/u/vicklik)\
**Post date:** [January 9, 2008, 2:51am UTC](https://forums.percona.com/t/query-is-very-slow/591/2 "2008-01-09T02:51:21Z")

</div>

try to use only 1 row (if possible the primary key) instead of \* in count statement:

select count(PrimaryKeyRowName) from table where ts \>= ‘2007-01-31 05:21:18’ AND ts \<= ‘2008-01-09 05:21:18’

---

<div class="post-metadata">

**Author:** ![erkules](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@erkules](https://forums.percona.com/u/erkules)\
**Post date:** [January 9, 2008, 5:52am UTC](https://forums.percona.com/t/query-is-very-slow/591/3 "2008-01-09T05:52:10Z")

</div>

| [B]jklanka wrote on Tue, 08 January 2008 22:38[/B] |
| I have a table with 3222374 records

when I use “select count(\*) from table” it works within a second.

But When I use

select count(\*) from table where ts \>= ‘2007-01-31 05:21:18’ AND ts \<= ‘2008-01-09 05:21:18’

It takes around 20 seconds to get the result.

I have created a index field on ts ( which is DATETIME type field )

Please help on this.

 |

show us;  
EXPLAIN select count(\*) from table where ts \>= ‘2007-01-31 05:21:18’ AND ts \<= ‘2008-01-09 05:21:18’;  
and  
show table statul like ‘table’\G

have you realy an table named table?

---

<div class="post-metadata">

**Author:** ![jklanka](https://avatars.discourse-cdn.com/v4/letter/j/91b2a8/32.png) [@jklanka](https://forums.percona.com/u/jklanka)\
**Post date:** [January 9, 2008, 10:26am UTC](https://forums.percona.com/t/query-is-very-slow/591/4 "2008-01-09T10:26:10Z")

</div>

It takes same time during with single field with primary key. Also table name is tbladd, not table.
