Unix Technical Forum

Partial char key not used in conjuction with inequality comparison (MySQL5)

This is a discussion on Partial char key not used in conjuction with inequality comparison (MySQL5) within the MySQL General forum forums, part of the MySQL category; --> Hello list! I've noticed in MySQL 5.0 partial keys on character fields aren't always used. In 4.1 they were. ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:16 AM
Thomas van Gulick
 
Posts: n/a
Default Partial char key not used in conjuction with inequality comparison (MySQL5)

Hello list!

I've noticed in MySQL 5.0 partial keys on character fields aren't always
used. In 4.1 they were.
They seem not to be used when using inequality comparison.
I'm not sure whether this is a bug or intended (in the latter case I have to
work around it to get the speed I got with 4.1 back into 5.0)

You can try for yourselves:

Setup:
CREATE TABLE t (T varchar(16) NOT NULL,KEY (T(1)));
INSERT INTO t SET T="test1";
INSERT INTO t SET T="test2";
INSERT INTO t SET T="test2";

Test:
EXPLAIN SELECT * FROM t WHERE T="x";
Result: key T used

EXPLAIN SELECT * FROM t WHERE T!="x";
Result: key T _unused_

Adding a key on entire field T works, but that's wasting a lot of space
because I only want to test whether a certain field is empty or not.

Important note: MySQL4.1 *did* use key T in the second query!

Can anybody concur whether this happens for them too?
And if so, is this a bug?

grtz,
Thomas
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:17 AM
Joerg Bruehe
 
Posts: n/a
Default Re: Partial char key not used in conjuction with inequality comparison(MySQL5)

Hi Thomas, all!


Thomas van Gulick wrote:
> [[...]]
>
> Test:
> EXPLAIN SELECT * FROM t WHERE T="x";
> Result: key T used
>
> EXPLAIN SELECT * FROM t WHERE T!="x";
> Result: key T _unused_


To be expected:
An "unequal" condition will evaluate to "true" for a very large
proportion of the index entries, so the selectivity will be too low.

The mere existence of an index does not imply that using it to access
the data is a good decision, doing this only pays off if the extra
accesses (to the index) are less work than is saved by not accessing
those data rows which are not returned by the index.

You wouldn't use a catalogue in manual search if a sequential scan would
return 1 out of 4 (or more) items, right ? The same holds here.


Joerg

--
Joerg Bruehe, Senior Production Engineer
MySQL AB, www.mysql.com
Office: (+49 30) 417 01 487 VoIP: 4464@sip.mysql.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

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


All times are GMT. The time now is 05:44 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com