If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Datetime Indexing Problem?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-26-12, 17:38
kierheyl kierheyl is offline
Registered User
 
Join Date: Dec 2007
Posts: 23
Datetime Indexing Problem?

Hi guys,

I have a table with over a million rows in it. Lately I've noticed some slowness even on some simple queries. Maybe I'm not understanding something obvious about indexes so I'm hoping some one can enlighten me.

Each record has a datetime field to keep up with when that record was created. If I run the following query:

select * from table where datetime > '2012-01-01'

It won't use the index that I have placed on the datetime field.

If I run this next query, however:

select 1 from table where datetime > '2012-01-01'

It uses the index fine. Does the index only apply to itself? If I want to reference multiple columns, do I need to create an index on multiple fields?

This is where things get weird. If I run this query:

select * from table where datetime > '2012-06-01'

It uses the index fine. Is whether or not it uses the index some how dependent on the number of rows returned? Why does the index work when selecting just the last couple of months, but not since the beginning of the year?

Any help would be appreciated.
Reply With Quote
  #2 (permalink)  
Old 07-26-12, 17:49
kierheyl kierheyl is offline
Registered User
 
Join Date: Dec 2007
Posts: 23
Another bit of useful information is that the table's storage engine is InnoDB.
Reply With Quote
  #3 (permalink)  
Old 07-26-12, 21:53
papadi papadi is offline
Registered User
 
Join Date: Oct 2009
Location: 221B Baker St.
Posts: 483
How do you know when the index is used and when it is not used?
Reply With Quote
  #4 (permalink)  
Old 07-27-12, 04:51
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 10,513
Quote:
Originally Posted by papadi View Post
How do you know when the index is used and when it is not used?
Explain or describe tells you how the SQL engine processes the query
MySQL :: MySQL 5.0 Reference Manual :: 8.2.1 Optimizing Queries with EXPLAIN
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 07-27-12, 09:00
kierheyl kierheyl is offline
Registered User
 
Join Date: Dec 2007
Posts: 23
Quote:
Originally Posted by healdem View Post
Explain or describe tells you how the SQL engine processes the query
MySQL :: MySQL 5.0 Reference Manual :: 8.2.1 Optimizing Queries with EXPLAIN
This is exactly right.
Reply With Quote
  #6 (permalink)  
Old 07-27-12, 09:41
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 10,513
Im wondering if you should be expressly converting the date literal to a datetime

it could be a quirk of the data that is in the db. ie the parser may decide its quicker to do a tablescan than use an index if the where clause is going to return most if not all rows

another thought is that perhaps the indexes need rebuilding
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 07-27-12, 09:46
kierheyl kierheyl is offline
Registered User
 
Join Date: Dec 2007
Posts: 23
Quote:
Originally Posted by healdem View Post
Im wondering if you should be expressly converting the date literal to a datetime

it could be a quirk of the data that is in the db. ie the parser may decide its quicker to do a tablescan than use an index if the where clause is going to return most if not all rows

another thought is that perhaps the indexes need rebuilding
I wondered the same thing about converting and I've tried it and it doesn't make a difference. Furthermore MySQL documentation very plainly states that the way to do datetime comparisons to a static date is to compare to a string in the format of 'YYYY-mm-dd hh:mm:ss'.

Where clause returns as little as 20% of the rows when the index isn't being utilized.

I might try rebuilding indexes.. on a million plus row table though that is no small feat...
Reply With Quote
  #8 (permalink)  
Old 07-27-12, 10:17
kierheyl kierheyl is offline
Registered User
 
Join Date: Dec 2007
Posts: 23
I ran an optimize table query on it, which with innodb tables essentially rebuilds the table. It took about 20 minutes. Looks to be working much better now.
Reply With Quote
  #9 (permalink)  
Old 07-27-12, 14:47
papadi papadi is offline
Registered User
 
Join Date: Oct 2009
Location: 221B Baker St.
Posts: 483
Quote:
Explain or describe tells you how the SQL engine processes the query
Yup. I probably misunderstood the original post My thinking was that it would be tough (impossible?) to determine "after the run".

As the query and the data content and possibly the db definition might have changed along the way, the EXPLAIN might show unrelated results.

Good to hear the time problem has been improved.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On