dbTalk Databases Forums  

Index question...

comp.databases.mysql comp.databases.mysql


Discuss Index question... in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default Index question... - 07-18-2006 , 09:30 AM






Hi,

We always have query in the following form...

e.g.

SELECT * FROM a,b where a.id = b.fid

Should we add index to both a.id and b.fid?

Thanks...


Reply With Quote
  #2  
Old   
Bill Karwin
 
Posts: n/a

Default Re: Index question... - 07-18-2006 , 11:42 AM






howachen (AT) gmail (DOT) com wrote:
Quote:
SELECT * FROM a,b where a.id = b.fid

Should we add index to both a.id and b.fid?
If a.id and b.fid are primary key and foreign key, respectively, they
already have indexes.

If not, then yes, creating indexes would be good. Do you know how to
use the EXPLAIN analyzer to tell you what indexes are being used by a
given query?

See http://dev.mysql.com/doc/refman/5.0/en/explain.html

Regards,
Bill K.


Reply With Quote
  #3  
Old   
AT
 
Posts: n/a

Default Re: Index question... - 07-18-2006 , 12:40 PM




Bill Karwin 寫道:

Quote:
howachen (AT) gmail (DOT) com wrote:
SELECT * FROM a,b where a.id = b.fid

Should we add index to both a.id and b.fid?

If a.id and b.fid are primary key and foreign key, respectively, they
already have indexes.

If not, then yes, creating indexes would be good. Do you know how to
use the EXPLAIN analyzer to tell you what indexes are being used by a
given query?

See http://dev.mysql.com/doc/refman/5.0/en/explain.html

Regards,
Bill K.
1. i am using MyISAM, not InnoDB, so no foreign key.

2. if both indexes are added, under explain statement, i see only one
index is used at a time. the optimizer seems will select the best index
to use?



Reply With Quote
  #4  
Old   
Bill Karwin
 
Posts: n/a

Default Re: Index question... - 07-18-2006 , 01:12 PM



howachen (AT) gmail (DOT) com wrote:
Quote:
1. i am using MyISAM, not InnoDB, so no foreign key.
Aha. Then you should create an index explicitly on b.fid.

Quote:
2. if both indexes are added, under explain statement, i see only one
index is used at a time. the optimizer seems will select the best index
to use?
MySQL may use one index per table in a query. But the optimizer may
judge in some cases, that reading the index into memory is more
expensive than just reading the rows of data.

Regards,
Bill K.


Reply With Quote
  #5  
Old   
AT
 
Posts: n/a

Default Re: Index question... - 07-18-2006 , 09:16 PM




Bill Karwin 寫道:

Quote:
howachen (AT) gmail (DOT) com wrote:
1. i am using MyISAM, not InnoDB, so no foreign key.

Aha. Then you should create an index explicitly on b.fid.
i heard that index on foreign key is needed only if you have foreign
key, otherwise, there is no need to, is it true?

Quote:
2. if both indexes are added, under explain statement, i see only one
index is used at a time. the optimizer seems will select the best index
to use?

MySQL may use one index per table in a query. But the optimizer may
judge in some cases, that reading the index into memory is more
expensive than just reading the rows of data.

what does it mean?

Quote:
Regards,
Bill K.


Reply With Quote
  #6  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Index question... - 07-18-2006 , 09:30 PM



howachen (AT) gmail (DOT) com wrote:
Quote:
Bill Karwin 寫道:


howachen (AT) gmail (DOT) com wrote:

1. i am using MyISAM, not InnoDB, so no foreign key.

Aha. Then you should create an index explicitly on b.fid.


i heard that index on foreign key is needed only if you have foreign
key, otherwise, there is no need to, is it true?

It's only REQUIRED if you have a foreign key. Whether it's NEEDED or
not depends on the database, number of records in the database, queries
made to the database, how much performance hit you can stand and about
50 other variables.

But yes, usually an index on a foreign key can help performance, often
significantly for big tables.


Quote:
2. if both indexes are added, under explain statement, i see only one
index is used at a time. the optimizer seems will select the best index
to use?

MySQL may use one index per table in a query. But the optimizer may
judge in some cases, that reading the index into memory is more
expensive than just reading the rows of data.



what does it mean?

It means the optimizer decided it was faster to read the data directly
from the table than to read in the index then access the table through
the pointers in the index. This most often occurs when you only have a
few rows in a table (but a table scan can also occur for other reason).

Quote:
Regards,
Bill K.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================


Reply With Quote
  #7  
Old   
AT
 
Posts: n/a

Default Re: Index question... - 07-18-2006 , 10:01 PM




Jerry Stuckle 寫道:

Quote:
howachen (AT) gmail (DOT) com wrote:
Bill Karwin 寫道:


howachen (AT) gmail (DOT) com wrote:

1. i am using MyISAM, not InnoDB, so no foreign key.

Aha. Then you should create an index explicitly on b.fid.


i heard that index on foreign key is needed only if you have foreign
key, otherwise, there is no need to, is it true?


It's only REQUIRED if you have a foreign key. Whether it's NEEDED or
not depends on the database, number of records in the database, queries
made to the database, how much performance hit you can stand and about
50 other variables.

But yes, usually an index on a foreign key can help performance, often
significantly for big tables.

when joining a table, IF ONLY ONE index can be used (i.e. primary key),
then add another index to the FK is useless, no matter the table size
(in fact, it will affect the performance of upadating/inserting)

i don't understand how index on FK will improve performane, IF ONLY ONE
(i.e. PK) index can be used at a time

Quote:

2. if both indexes are added, under explain statement, i see only one
index is used at a time. the optimizer seems will select the best index
to use?

MySQL may use one index per table in a query. But the optimizer may
judge in some cases, that reading the index into memory is more
expensive than just reading the rows of data.



what does it mean?


It means the optimizer decided it was faster to read the data directly
from the table than to read in the index then access the table through
the pointers in the index. This most often occurs when you only have a
few rows in a table (but a table scan can also occur for other reason).

Thanks



Reply With Quote
  #8  
Old   
AT
 
Posts: n/a

Default Re: Index question... - 07-19-2006 , 12:02 AM




Peter H. Coffin 寫道:

Quote:
On 18 Jul 2006 20:01:44 -0700, howachen (AT) gmail (DOT) com wrote:

Jerry Stuckle ???

howachen (AT) gmail (DOT) com wrote:
Bill Karwin ???


howachen (AT) gmail (DOT) com wrote:

1. i am using MyISAM, not InnoDB, so no foreign key.

Aha. Then you should create an index explicitly on b.fid.


i heard that index on foreign key is needed only if you have foreign
key, otherwise, there is no need to, is it true?


It's only REQUIRED if you have a foreign key. Whether it's NEEDED or
not depends on the database, number of records in the database, queries
made to the database, how much performance hit you can stand and about
50 other variables.

But yes, usually an index on a foreign key can help performance, often
significantly for big tables.


when joining a table, IF ONLY ONE index can be used (i.e. primary key),
then add another index to the FK is useless, no matter the table size
(in fact, it will affect the performance of upadating/inserting)

i don't understand how index on FK will improve performane, IF ONLY ONE
(i.e. PK) index can be used at a time

An index is REQUIRED on a foreign key.
this is apply to InnoDB or other DB such as Oracle, not apply to MySQL
ISAM table



Reply With Quote
  #9  
Old   
Bill Karwin
 
Posts: n/a

Default Re: Index question... - 07-19-2006 , 02:20 AM



howachen (AT) gmail (DOT) com wrote:
Quote:
i don't understand how index on FK will improve performane, IF ONLY ONE
(i.e. PK) index can be used at a time
MySQL can use one index per table. Your example named table "a" and
table "b". So it may use the primary key index from table "a" and the
foreign key index from table "b".

That's one index per table. If you have two tables, it may use two indexes.

Quote:
this is apply to InnoDB or other DB such as Oracle, not apply to MySQL
ISAM table
We're not talking about Oracle.

MySQL has a limitation that it uses one index per table. This is true
whether you use InnoDB or MyISAM.

Actually, there are cases in MySQL 5.0 where it can merge indexes, and
so it uses more than one index per table. But only for certain types of
queries. Read the docs about merge indexes:
http://dev.mysql.com/doc/refman/5.0/...imization.html

Regards,
Bill K.


Reply With Quote
  #10  
Old   
AT
 
Posts: n/a

Default Re: Index question... - 07-19-2006 , 03:39 AM




Bill Karwin 寫道:

Quote:
howachen (AT) gmail (DOT) com wrote:
i don't understand how index on FK will improve performane, IF ONLY ONE
(i.e. PK) index can be used at a time

MySQL can use one index per table. Your example named table "a" and
table "b". So it may use the primary key index from table "a" and the
foreign key index from table "b".

That's one index per table. If you have two tables, it may use two indexes.

thanks for your reply first...but the point is:

1. when joining two tables as above, only one index is used in the
whole query execution

my question is : why we need to create additional index on FK if MySQL
only can either use index PK or FK? why not force them to use PK (a.id)
and drop the index on b.fid (so as to save space and improve
insert/update performance)

thanks...



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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.