5

我如何编辑GROUP BY命令有什么不同吗?

我的代码:

SELECT Number, Id 
    FROM Table
    WHERE(....)
    GROUP BY Id, Number

如果我这样编辑它会更快吗:

 SELECT Number, Id 
    FROM Table
    WHERE(....)
    GROUP BY Number , Id
4

2 回答 2

3

DISTINCT如果您不想聚合数据,最好使用它。否则,您提供的两个查询之间没有区别,它将产生相同的查询计划

于 2013-07-15T09:20:15.280 回答
2

这个例子是相同的。

DDL:

CREATE TABLE dbo.[WorkOut]
(
    [WorkOutID] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [TimeSheetDate] [datetime] NOT NULL,
    [DateOut] [datetime] NOT NULL,
    [EmployeeID] [int] NOT NULL,
    [IsMainWorkPlace] [bit] NOT NULL,
    [DepartmentUID] [uniqueidentifier] NOT NULL,
    [WorkPlaceUID] [uniqueidentifier] NULL,
    [TeamUID] [uniqueidentifier] NULL,
    [WorkShiftCD] [nvarchar](10) NULL,
    [WorkHours] [real] NULL,
    [AbsenceCode] [varchar](25) NULL,
    [PaymentType] [char](2) NULL,
    [CategoryID] [int] NULL
)

询问:

SELECT wo.WorkOutID, wo.TimeSheetDate 
FROM dbo.WorkOut wo
GROUP BY wo.WorkOutID, wo.TimeSheetDate

SELECT DISTINCT wo.WorkOutID, wo.TimeSheetDate 
FROM dbo.WorkOut wo

SELECT wo.DateOut, wo.EmployeeID
FROM dbo.WorkOut wo
GROUP BY wo.DateOut, wo.EmployeeID

SELECT DISTINCT wo.DateOut, wo.EmployeeID 
FROM dbo.WorkOut wo

执行计划:

pp

于 2013-07-15T09:24:14.700 回答