sql组通过与独特 [英] sql group by versus distinct

查看:73
本文介绍了sql组通过与独特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在查询中没有进行聚合时,有人会使用group而不是distinct?

另外,有人知道group by和不同的性能考虑MySQL和SQL Server。我猜SQL Server有一个更好的优化器,他们可能接近于相当的,但在MySQL中,我期望有显着的性能优势来区分。



m对dba答案感兴趣。

编辑:

Bill的帖子很有趣,但不适用。让我更具体一些...

 从表格x 
中选择a,b,c
由a,b,c



<$从表x
中选择不同的a,b,c

解决方案

一些来自MS SQL Server的经验数据,来自我们数据库的一些随机表。



对于模式:

  SELECT col1,col2 FROM表GROUP BY col1,col2 

  SELECT DISTINCT col1,col2 FROM表

如果查询没有覆盖索引,则两种方法都会生成以下查询计划:

  |  - 排序(DISTINCT ORDER BY:([table]。[col1] ASC,[table]。[col2] ASC)) 
| - 集群索引扫描(OBJECT:([db]。[dbo]。[table]。[IX_some_index]))

,当有覆盖指数时,两者都产生:

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

因此,从这个非常小的样本SQL Server当然对待相同。


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

解决方案

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.

这篇关于sql组通过与独特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆