nionios
nionios

Reputation: 189

SQL fastest 'GROUP BY' script

Is there any difference in how I edit the GROUP BY command?

my code:

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

is it faster if i edit it like this:

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

Upvotes: 5

Views: 310

Answers (2)

Devart
Devart

Reputation: 121912

This examples are equal.

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
)

Query:

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

Execution plan:

pp

Upvotes: 2

jazzytomato
jazzytomato

Reputation: 7214

it's better to use DISTINCT if you don't want to aggregate data. Otherwise, there is no difference between the two queries you provided, it'll produce the same query plan

Upvotes: 3

Related Questions