Revenue Source

Welcome to the Revenue Source affiliate marketing forums.

You are viewing our internet marketing and SEO forums as a guest which gives you limited access to most of our discussions.  By joining our free community, you will have access to post affiliate marketing topics, communicate privately with other members (PM), exchange SEO strategies, and access many other special features.  Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems, please don't hesitate to contact us.

Go Back   Revenue Source > Site Design & Development > Databases
Reload this Page Do you always need index on WHERE column ?
Tags: , ,

Reply
 
LinkBack Thread Tools Search this Thread
Old
  (#1 (permalink))
Affiliate Blogs is Offline
Revenue Source Veteran
Affiliate Blogs has a brilliant future here!
 
Affiliate Blogs's Avatar
 
Join Date: Oct 2005
Posts: 9,157
Jack of All Trades
CyberSpace United States
   
Do you always need index on WHERE column ? - 08-28-2007

I believe we wrote about this before, but this topic popups again and again.
Today I've read opinion that if we have clause WHERE has_something=1 we should have index on column `has_something` (the column has two values 0 and 1).
In reality the right answer is not so simple.
Let's look next table
PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]CREATE TABLE `testr` ([/font]
  2. [FONT='Courier New', Courier, monospace] `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,[/font]
  3. [FONT='Courier New', Courier, monospace] `name` varchar(32) NOT NULL,[/font]
  4. [FONT='Courier New', Courier, monospace] `has_something` tinyint(3) UNSIGNED NOT NULL,[/font]
  5. [FONT='Courier New', Courier, monospace] PRIMARY KEY (`id`),[/font]
  6. [FONT='Courier New', Courier, monospace] KEY `has_something` (`has_something`)[/font]
  7. [FONT='Courier New', Courier, monospace]) ENGINE=MyISAM [/font]


with 20.000.000 records.
And in first case has_something=0 for 90% of rows (with random distribution)
PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]mysql> SELECT cnt0/cnt FROM (SELECT count(*) cnt0 FROM testr WHERE has_something=0) t, (SELECT count(*) cnt FROM testr) t1;[/font]
  2. [FONT='Courier New', Courier, monospace]+----------+[/font]
  3. [FONT='Courier New', Courier, monospace]| cnt0/cnt |[/font]
  4. [FONT='Courier New', Courier, monospace]+----------+[/font]
  5. [FONT='Courier New', Courier, monospace]| 0.9001 | [/font]
  6. [FONT='Courier New', Courier, monospace]+----------+[/font]
  7. [FONT='Courier New', Courier, monospace]1 row IN SET (7.56 sec) [/font]



Let's check execution time with and without index

mysql> select count(name) from testr force key (has_something) where has_something=0;
+-------------+
| count(name) |
+-------------+
| 18001245 |
+-------------+
1 row in set (35.96 sec)


mysql> select count(name) from testr ignore key (has_something) where has_something=0;
+-------------+
| count(name) |
+-------------+
| 18001245 |
+-------------+
1 row in set (10.46 sec)

As you see with index the time is by 3.5 times slower.
Good that mysql in this case choose do not use index
PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]mysql> EXPLAIN SELECT count(name) FROM testr WHERE has_something=0; [/font]
  2. [FONT='Courier New', Courier, monospace]+----+-------------+-------+------+---------------+------+---------+------+----------+-------------+[/font]
  3. [FONT='Courier New', Courier, monospace]| id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra |[/font]
  4. [FONT='Courier New', Courier, monospace]+----+-------------+-------+------+---------------+------+---------+------+----------+-------------+[/font]
  5. [FONT='Courier New', Courier, monospace]| 1 | SIMPLE | testr | ALL | has_something | NULL | NULL | NULL | 15000000 | USING WHERE | [/font]
  6. [FONT='Courier New', Courier, monospace]+----+-------------+-------+------+---------------+------+---------+------+----------+-------------+[/font]
  7. [FONT='Courier New', Courier, monospace]1 row IN SET (0.00 sec) [/font]



Let look the case when has_something = 0 for 50% of rows.

mysql> select count(name) from testr force key (has_something) where has_something=0;
1 row in set (20.27 sec)


mysql> select count(name) from testr ignore key (has_something) where has_something=0;
1 row in set (10.62 sec)

query with index is still 2 times slower.
and this time mysql is going to use index in execution plan:
PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]mysql> EXPLAIN SELECT count(name) FROM testr WHERE has_something=0; +----+-------------+-------+------+---------------+---------------+---------+-------+---------+-------+[/font]
  2. [FONT='Courier New', Courier, monospace]| id | select_type | TABLE | type | possible_keys | KEY | key_len | ref | rows | Extra |[/font]
  3. [FONT='Courier New', Courier, monospace]+----+-------------+-------+------+---------------+---------------+---------+-------+---------+-------+[/font]
  4. [FONT='Courier New', Courier, monospace]| 1 | SIMPLE | testr | ref | has_something | has_something | 1 | const | 8890716 | |[/font]
  5. [FONT='Courier New', Courier, monospace]+----+-------------+-------+------+---------------+---------------+---------+-------+---------+-------+[/font]
  6. [FONT='Courier New', Courier, monospace]1 row IN SET (0.00 sec) [/font]



What about 30% rows with has_something=0 ?

mysql> select count(name) from testr force key (has_something) where has_something=0;
1 row in set (12.36 sec)


mysql> select count(name) from testr ignore key (has_something) where has_something=0;
1 row in set (10.51 sec)

Still query without index is faster.
And finally for case with 20% rows with has_someting=0

mysql> select count(name) from testr force key (has_something) where has_something=0;
1 row in set (8.39 sec)


mysql> select count(name) from testr ignore key (has_something) where has_something=0;
1 row in set (10.43 sec)

So only in the last case we really need the index on column `has_something`


Do you always need index on WHERE column ? - Read More...
  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads for: Do you always need index on WHERE column ?
Thread Thread Starter Forum Replies Last Post
Add Column Affiliate Blogs Programming Help 0 04-27-2007 02:31 PM
Don't Dulpicate a Column Affiliate Blogs Programming Help 0 01-10-2007 05:05 PM
Change a Column Name Affiliate Blogs Programming Help 0 11-20-2006 04:31 PM
Add Column Affiliate Blogs Programming Help 0 11-16-2006 09:23 PM
Drop Column in MySQL Affiliate Blogs Programming Help 0 11-15-2006 03:27 PM



© 2004-6 RevenueSource.com.  All rights reserved.  Do not duplicate or redistribute in any form.
This website and its logos/design are property of RevenueSource.com.  All rights reserved. vBSEO 3.2.0 RC7


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34