Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146934
  • 博文数量: 56
  • 博客积分: 245
  • 博客等级: 二等列兵
  • 技术积分: 520
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-08 14:43
个人简介

慢慢来

文章分类

全部博文(56)

文章存档

2017年(5)

2016年(2)

2015年(6)

2014年(28)

2013年(5)

2012年(10)

我的朋友

分类: 数据库开发技术

2014-12-19 15:41:14

From sqlmag: 

一句话:没区别,但最好还是看一下showplan

Q: 
Should I use DISTINCT or GROUP BY to eliminate duplicates in a result set?

A: A DISTINCT and GROUP BY usually generate the same query plan, so performance should be the same across both query constructs. For example, say you want to generate a unique list of product IDs from the Order Details table in Northwind. The two following queries both give you a correct answer:

SELECT DISTINCT od.productid
FROM \[ORDER details\] OD
SELECT od.productid
FROM \[ORDER details\] OD
GROUP BY od.productid

Which one is more efficient? Checking execution plans is a simple way to determine the relative efficiency of different queries that generate the same result set. Enable Show Execution Plan in Query Analyzer by pressing Ctrl+K or by selecting Show Execution Plan from the Query menu. Then, execute the above queries.  shows that the execution plans of both queries are the same. In most cases, DISTINCT and GROUP BY generate the same plans, and their performance is usually identical.

So, how do you decide which SQL command to use? GROUP BY is required if you're aggregating data, but in many cases, DISTINCT is simpler to write and read if you aren't aggregating data. Pick whichever syntax you prefer for your situation.

阅读(551) | 评论(0) | 转发(0) |
0

上一篇:my vimrc

下一篇:gcc- -O 优化选项

给主人留下些什么吧!~~