MySQL Forums
Forum List  »  Newbie

Re: Index recomendations
Posted by: laptop alias
Date: November 09, 2010 07:18PM

Sometimes an index can actually be counterproductive...

SELECT COUNT(*) FROM tab1;
+----------+
| COUNT(*) |
+----------+
|  2097152 |
+----------+

-- Test 1. No index...

SELECT COUNT(no)
  FROM tab1
 WHERE version='1.1.1.1' 
   AND rel_date BETWEEN ADDDATE(NOW(), INTERVAL -1 HOUR) AND NOW();
+-----------+
| COUNT(no) |
+-----------+
|       397 |
+-----------+
1 row in set (0.68 sec)


-- Test 2. Index on version...

ALTER TABLE tab1 ADD INDEX (version);

SELECT COUNT(no)
  FROM tab1
 WHERE version='1.1.1.2' 
   AND rel_date BETWEEN ADDDATE(NOW(), INTERVAL -1 HOUR) AND NOW();
+-----------+
| COUNT(no) |
+-----------+
|       609 |
+-----------+
1 row in set (2.13 sec) -- 3 times slower than 1!!!!!!!!!!

-- Test 3. Composite index on (version,rel_date)

ALTER TABLE tab1 ADD INDEX version_reldate (version,rel_date);

SELECT COUNT(no)
  FROM tab1
 WHERE version='1.1.1.3' 
   AND rel_date BETWEEN ADDDATE(NOW(), INTERVAL -1 HOUR) AND NOW();
+-----------+
| COUNT(no) |
+-----------+
|       736 |
+-----------+
1 row in set (0.01 sec) -- 68 times faster than 1!!!!!!!!!!!!!

... a more exhaustive test would compare a dense dataset as against a sparse one but I'll leave that for others to explore.

So, for now, try adding a composite index on (version,rel_date).



Edited 1 time(s). Last edit at 11/09/2010 07:20PM by laptop alias.

Options: ReplyQuote


Subject
Written By
Posted
November 09, 2010 07:40AM
Re: Index recomendations
November 09, 2010 07:18PM
November 10, 2010 05:56AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.