David Allan James
David Allan James

Reputation: 13

Group By v Max() or MIN() For Parent Fields

If I have a Parent Child Relationship, and I wish to get an Aggregate from the Child table, count(*), SUM(), doesn't really matter which one.

But I wish to return Multiple fields from the Parent Table too, I can either Group By All the Parent Fields or Group By the Primary Key of the Parent and use MIN() around the Parent Fields Not in the Primary Key.

Question is which is most efficient?

Group By all the Parent Fields Or Pulling them back as the Min(ParentFieldnnn) as ParentFieldnnn ?

Upvotes: 0

Views: 79

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270613

Grouping by a single column should be a tad more efficient. So group by the id and use aggregation functions such as min().

This will actually be even more efficient if you have an index on that key.

When you have a bunch of keys, the database is generally sorting or hashing on all the keys -- so all the data needs to be moved around.

Upvotes: 0

Related Questions