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 Covering index and prefix indexes
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: 8,626
Jack of All Trades
CyberSpace United States
   
Covering index and prefix indexes - 11-23-2006

I already wrote in the blog as well mentioned in presentation there is often a choice you have to make between having prefix index - which can be significantly smaller in size and having index being covering index, which means query can be executed using only data from the index without reading the row itself.
Today I had a chance to do couple of experiments to see when exactly it works or does not work:

PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]CREATE TABLE `t` ([/font]
  2. [FONT='Courier New', Courier, monospace] `i` int(11) DEFAULT NULL,[/font]
  3. [FONT='Courier New', Courier, monospace] `j` char(10) DEFAULT NULL,[/font]
  4. [FONT='Courier New', Courier, monospace] `k` int(11) DEFAULT NULL,[/font]
  5. [FONT='Courier New', Courier, monospace] KEY `i` (`i`,`j`(5),`k`)[/font]
  6. [FONT='Courier New', Courier, monospace]) ENGINE=MyISAM [/font]



Now lets see if index can be used as covering index if it has some key parts which are prefixes:
PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]mysql> EXPLAIN SELECT k FROM t WHERE i=5 \G[/font]
  2. [FONT='Courier New', Courier, monospace]*************************** 1. row ***************************[/font]
  3. [FONT='Courier New', Courier, monospace] id: 1[/font]
  4. [FONT='Courier New', Courier, monospace] select_type: SIMPLE[/font]
  5. [FONT='Courier New', Courier, monospace] TABLE: t[/font]
  6. [FONT='Courier New', Courier, monospace] type: ref[/font]
  7. [FONT='Courier New', Courier, monospace]possible_keys: i[/font]
  8. [FONT='Courier New', Courier, monospace] KEY: i[/font]
  9. [FONT='Courier New', Courier, monospace] key_len: 5[/font]
  10. [FONT='Courier New', Courier, monospace] ref: const[/font]
  11. [FONT='Courier New', Courier, monospace] rows: 1[/font]
  12. [FONT='Courier New', Courier, monospace] Extra: USING WHERE; USING INDEX[/font]
  13. [FONT='Courier New', Courier, monospace]1 row IN SET (0.00 sec) [/font]



Great. As you see you actually can use index which has prefix key parts as covering index if you do not touch columns which only have prefixes in the index. Notice "Using Index" in Extra column.
PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]mysql> EXPLAIN SELECT j,k FROM t WHERE i=5 \G[/font]
  2. [FONT='Courier New', Courier, monospace]*************************** 1. row ***************************[/font]
  3. [FONT='Courier New', Courier, monospace] id: 1[/font]
  4. [FONT='Courier New', Courier, monospace] select_type: SIMPLE[/font]
  5. [FONT='Courier New', Courier, monospace] TABLE: t[/font]
  6. [FONT='Courier New', Courier, monospace] type: ref[/font]
  7. [FONT='Courier New', Courier, monospace]possible_keys: i[/font]
  8. [FONT='Courier New', Courier, monospace] KEY: i[/font]
  9. [FONT='Courier New', Courier, monospace] key_len: 5[/font]
  10. [FONT='Courier New', Courier, monospace] ref: const[/font]
  11. [FONT='Courier New', Courier, monospace] rows: 1[/font]
  12. [FONT='Courier New', Courier, monospace] Extra: USING WHERE[/font]
  13. [FONT='Courier New', Courier, monospace]1 row IN SET (0.00 sec) [/font]



In this case index can't be used as covering index as we only have portion of "j" in the index. It is interesting I tested it with single character column values which all do fit in the index but MySQL does not look at the actual data in this case it only looks at definitions.
PLAIN TEXT
SQL:
  1. [FONT='Courier New', Courier, monospace]mysql> EXPLAIN SELECT j FROM t WHERE i=5 AND k LIKE "a%" \G[/font]
  2. [FONT='Courier New', Courier, monospace]*************************** 1. row ***************************[/font]
  3. [FONT='Courier New', Courier, monospace] id: 1[/font]
  4. [FONT='Courier New', Courier, monospace] select_type: SIMPLE[/font]
  5. [FONT='Courier New', Courier, monospace] TABLE: t[/font]
  6. [FONT='Courier New', Courier, monospace] type: ref[/font]
  7. [FONT='Courier New', Courier, monospace]possible_keys: i[/font]
  8. [FONT='Courier New', Courier, monospace] KEY: i[/font]
  9. [FONT='Courier New', Courier, monospace] key_len: 5[/font]
  10. [FONT='Courier New', Courier, monospace] ref: const[/font]
  11. [FONT='Courier New', Courier, monospace] rows: 1[/font]
  12. [FONT='Courier New', Courier, monospace] Extra: USING WHERE[/font]
  13. [FONT='Courier New', Courier, monospace]1 row IN SET (0.00 sec) [/font]



This example is less obvious - one may think why can't we read only from the index as we only select k column ? The reason is - we're using column j in where clause. Even if this particular like check can be done only by using index, MySQL is not smart enough to notice it - it simply checks if column is used in the query and if it does, covering index can't be used.
Note: MySQL is however smart enough to make sure prefix specified is actually prefix, not the full key length. If you would specify key length of 10 in this case it will convert it to the full key instead of prefix key. If you would use length longer than column length, lets say 20, MySQL will refuse to create an index.
So in general handling of prefix keys in MySQL is smart in this respect.


Covering index and prefix indexes - 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: Covering index and prefix indexes
Thread Thread Starter Forum Replies Last Post
Supplemental Index Fuzzier Than Ever SEO Blogs SEO / SEM 0 11-15-2006 10:41 PM
My Next Obsession - Getting a Site Back in the Index SEO Blogs SEO / SEM 0 11-15-2006 10:41 PM
Why I don?t Like Index.html SEO Blogs SEO / SEM 0 11-15-2006 10:41 PM
The Home Business Index Affiliate Blogs Affiliate Marketing 0 11-14-2006 07:12 PM
Search Engine Spider, Index, And Ranking ValiantMarketer Search Engine Optimization / Marketing 0 11-30-2004 04:11 AM



© 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