views:

4565

answers:

4

Why would someone use a group by versus distinct when there are no aggregations done in the query?

Also, does someone know the group by versus distinct performance considerations in MySQL and SQL Server. I'm guessing that SQL Server has a better optimizer and they might be close to equivalent there, but in MySQL, I expect a significant performance advantage to distinct.

I'm interested in dba answers.

EDIT:

Bill's post is interesting, but not applicable. Let me be more specific...

select a, b, c from table x group by a, b,c

versus

select distinct a,b,c from table x

+2  A: 

Hi,

Both would generate the same query plan in MS SQL Server.... If you have MS SQL Server you could just enable the actual execution plan to see which one is better for your needs ...

Please have a look at those posts:

http://blog.sqlauthority.com/2007/03/29/sql-server-difference-between-distinct-and-group-by-distinct-vs-group-by/

http://www.sqlmag.com/Article/ArticleID/24282/sql_server_24282.html

afgallo
using group by instead of distinct smells like newb. there is a lot more syntax for no gain. also - you don't have any metrics - just hearsay from another's blog.
mson
+8  A: 

GROUP BY maps groups of rows to one row, per distinct value in specific columns, which don't even necessarily have to be in the select-list.

SELECT b, c, d FROM table1 GROUP BY a;

This query is legal SQL (correction: only in MySQL; actually it's not standard SQL and not supported by other brands). MySQL accepts it, and it trusts that you know what you're doing, selecting b, c, and d in an unambiguous way because they're functional dependencies of a.

However, Microsoft SQL Server and other brands don't allow this query, because it can't determine the functional dependencies easily. edit: Instead, standard SQL requires you to follow the Single-Value Rule, i.e. every column in the select-list must either be named in the GROUP BY clause or else be an argument to a set function.

Whereas DISTINCT always looks at all columns in the select-list, and only those columns. It's a common misconception that DISTINCT allows you to specify the columns:

SELECT DISTINCT(a), b, c FROM table1;

Despite the parentheses making DISTINCT look like function call, it is not. It's a query option and a distinct value in any of the three fields of the select-list will lead to a distinct row in the query result. One of the expressions in this select-list has parentheses around it, but this won't affect the result.

Bill Karwin
interesting but irrelevant. you are answering a question about making mistakes in queries. also, i don't think it is ansi standard to accept such a query as you pose. if anything, it's a bug in mysql - the other major vendors do not support this 'feature'
mson
Getting off-topic, but the second query Bill gives (SELECT DISTINCT(a)...) is perfectly valid ANSI SQL-92. The parentheses are actually irrelevant; you can do SELECT a, (b), c FROM table1, and that's valid. It's just that because it's the first param it LOOKS like it the 'a' is a param of DISTINCT.
Cowan
@mson: You're right! I looked it up in my "SQL-99 Complete, Really" and it says enforcement of the Single-Value Rule is standard SQL and an RDBMS that doesn't enforce it (e.g. MySQL) is not conforming to standard SQL.
Bill Karwin
@Cowan: Yes that was my point. It's valid syntax, and it does exactly what it's supposed to, but that's different from what some people believe it does.
Bill Karwin
@Bill - thanks for researching and finding out for sure (i have a terrible memory and was guessing).
mson
FWIW, SQLite also does not enforce the Single-Value Rule. Also, MySQL will enforce the rule if you set the `ONLY_FULL_GROUP_BY` SQL mode.
Bill Karwin
A: 

If you really are looking for distinct values, the distinct makes the source code more readable (like if it's part of a stored procedure) If I'm writing ad-hoc queries I'll usually start with the group by, even if I have no aggregations because I'll often end up putting them on.

Booji Boy
i do the same thing you do, but in the other direction. i start with select distinct and move to group by if there is a reason to. i don't try to optimize everything, but if i can remember a few rules of thumb about what is faster and cleaner, i follow them. group by is a bit messy-2 clauses
mson
+3  A: 

A little (VERY little) empirical data from MS SQL Server, on a couple of random tables from our DB.

For the pattern:

SELECT col1, col2 FROM table GROUP BY col1, col2

and

SELECT DISTINCT col1, col2 FROM table

When there's no covering index for the query, both ways produced the following query plan:

|--Sort(DISTINCT ORDER BY:([table].[col1] ASC, [table].[col2] ASC))
   |--Clustered Index Scan(OBJECT:([db].[dbo].[table].[IX_some_index]))

and when there was a covering index, both produced:

|--Stream Aggregate(GROUP BY:([table].[col1], [table].[col2]))
   |--Index Scan(OBJECT:([db].[dbo].[table].[IX_some_index]), ORDERED FORWARD)

so from that very small sample SQL Server certainly treats both the same.

Cowan
thanks for confirming - i'd guess that the big vendors have optimized this, but i'd also guess that MySQL has not.
mson