Featured Posts

Top 5 Debugging and Diagnostic Rules for Programming I remember in 1991 an ad that appeared in the weekly news-magazine, popular at the time, Information Technology (IT) Weekly.  The ad showed some poor schmuck laboring away...

Read more

The 10 Worst Things about World of Warcraft - Mists... I've been playing WoW since vanilla version starting in 2006.  Except for a six-month hiatus in late 2011, I've been a daily player.  I've seen multiple patches come...

Read more

Best Breakfast Burritos, ever! I like eating a good breakfast, usually around lunchtime once I've had my fill of coffee and am awake enough to appreciate a good breakfast. This is my recipe for my ultimate...

Read more

Testing Arrays in PHP - Back to Basics... Sometimes, when you're wallowing through your abstraction class layers, you find yourself using code for simple functions that are normally the focus of an Intro to Programming...

Read more

PHP: Comparing Object Structures I'm working on a project where I am converting an established REST API over to a rabbitMQ service.  Because, you know, dinosaur, I'm continuing to use PHP as my language...

Read more

Subscribe

O’Reilly…and my timestamp index problem…

Category : Announcements, Journal, Technical
No Gravatar

Just a quick jot – I’m headed to the O’Reilly MySQL conference, April 12-14 in Santa Clara for three joyous days filled with database geekiness.

There’s a lot of diversity in my schedule for the conference – I’m focusing mainly on performance tuning for mySQL, cloud deployment, replication and sharding, and a shallow dip into noSQL strategies.

Here’s the current problem I’m working on:

For any given table, (I don’t believe that the ddl matters), say you have a timestamp field which is indexed and referenced in a query.  The table is Innodb so the timestamp field can only be a btree index.  Which means that the index is stored in ascending order.

In your query, your conditional reads something to the effect of (where timestamp(now) > timestamp(now) – 5 minutes)  (it’s psuedo-code, ok?)

What I expect to happen is that as the query is calculating the conditional border value and then doing an index-scan on the btree-index, examining each index value (row) until the current timestamp exceeds the calculated value.

What I’m seeing (in the slow-query log, among other places), is that the query is executing as an index-scan, but is scanning all the rows of the index, essentially doing a full table scan.

The query is calculated and stored as a variable passed to mysql_query() — so the parser is determining the now()-5 minutes value and is passing it off as a string constant…

Anyone have any suggestions, I’d be most happy.  This lil’ kitteh turd has filled my slow-query litter box….

I finally figured out the solution to this, thanks to the book “High Performance SQL” — and I quote from page 106:

“MySQL generally can’t use indexes on columns unless the columns are isolated in the query. “Isolating” the column means is should not be part of an expression or be inside a function in the query.”

Page optimized by WP Minify WordPress Plugin

Weather forecast by WP Wunderground & Denver Snow Plowing