Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Lighter Count Child-Parent?
Message
De
17/02/2009 03:05:26
Walter Meester
HoogkarspelPays-Bas
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01382205
Message ID:
01382287
Vues:
30
>In a one to many relationship between a parent and child table, what is the lightest way to count() the child table and return the parent description in one SQL statement?
>
>Like:
>TableParent = SomeID I, description C(50)
>TableChild = pk I, SomeID I, OtherTablePK I)
>
>My answer is this:
>
>
>Select count(tablechild.someid) as itemcount, tableparent.description ;
>  from tablechild ;
>    left outer join tableparent ;
>    on tablechild.SomeID=tableparent.SomeID ;
>   group by tablechild.someid,tableparent.description
>
>
>But it seems that I'm missing a "lighter" way to do this... am I?

It depends on what you mean with a ligther way. SQL server is a different beast as VFP in many respects. If you have an index on tablechild.someid that SQL server is smart enough to use the index only and never touches the tablechild table physically. Though we can discuss about the order and nature of the join, I don't think it is that relevant in terms of performance. Just make sure you've got indexes on the joined fields and examine the execution plan to see how it is executing the query.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform