Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > Sql, statment and leftjoins and indexes...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-03, 23:44
jamiguel77 jamiguel77 is offline
Registered User
 
Join Date: Jul 2003
Posts: 2
Sql, statment and leftjoins and indexes...

Select * from tmaster a left join tdetail b on b.idetailmastnum=a.imasterid left join tcatmar c on b.idetailmar=c.icatmarid left join tcatvar d on b.idetailvar=d.icatvarid left join tcatsal e on b.idetailsala=e.icatsalid left join tcatfri f on a.imasterfrig=f.icatfriid left join tcatclie g on a.imasterclie=g.icatclieid where a.dmasterfecha>='2002/01/01' and a.dmasterfecha<='2002/06/01' and a.imasterclie=1 order by idetailmastnum,idetailnreng

in tmaster only have 6 records, in tdetail mmm 30 records in all table so so 5 records why are slow slow? what indexes recommend me for create?


thanks
Reply With Quote
  #2 (permalink)  
Old 07-07-03, 00:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,561
all tables should have a primary key

that will give them an index automatically

then, for related tables, declare an index for each foreign key (which, unless you're using innodb, you don't need to bother declaring as a foreign key) -- the foreign key being the column in the related table that connects to the first table's primary key

thus, every column in all of your ON conditions will have an index

then, consider an index for a column such as dmasterfecha which is used in the WHERE clause and has good selectivity -- imasterclie, on the other hand, may not have good selectivity

by good selectivity, i mean there is a good chance that the optimizer will decide to use the index because it reduces the number of potential rows to be returned

for example, if you had a large table of people, and you had a column for gender, with 1=male and 2=female, then fully half the table will have each value, so that does not provide good selectivity

since the database requires two reads, one for the index and one for the row, if you access half the table that way, there's not much point, so a good optimizer will ignore an index on gender


rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 07-07-03, 05:27
omiossec omiossec is offline
Registered User
 
Join Date: Jan 2003
Location: Paris, France
Posts: 320
You should use explain statement to know how index are unsed in your statement

Code:
explain Select * from tmaster a left join tdetail b on b.idetailmastnum=a.imasterid left join tcatmar c on b.idetailmar=c.icatmarid left join tcatvar d on b.idetailvar=d.icatvarid left join tcatsal e on b.idetailsala=e.icatsalid left join tcatfri f on a.imasterfrig=f.icatfriid left join tcatclie g on a.imasterclie=g.icatclieid where a.dmasterfecha>='2002/01/01' and a.dmasterfecha<='2002/06/01' and a.imasterclie=1 order by idetailmastnum,idetailnreng

You should also use colname instead of *
__________________
Olivier Miossec
--
http://www.lasso-developpeur.net/
--
Reply With Quote
  #4 (permalink)  
Old 07-08-03, 00:02
jamiguel77 jamiguel77 is offline
Registered User
 
Join Date: Jul 2003
Posts: 2
i have solved my problem, but....

i work with mysql + delphi + Zeos Componets when i execute these query in EMS mysql manager work fien fast fastor in sql explorer too, but in my code of delphi slow slow with TZquery of Zeos, but when i change the component by a Native BDE Tquery work fast fast same to Ems mysql explorer, anyone her eknow why?



thanks


regards
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On