Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»»

A Simple Formula to Calculate the ISO Week Number Expand / Collapse
Author
Message
Posted Monday, April 8, 2013 5:23 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Jeff, just FYI...I created the million row table on my slow laptop and tested the code from your article. With a normal SELECT statement it took appx 19-22 secs per run to get a million rows.

Now out of curiosity, I whipped up an iTVF and ran that against the data with a cross apply and it did NOT perform well. Something on the order of 2 minutes or more. That just goes to show that iTVFs are not always the answer. I also re-tested after adding a non-clustered index to the temp table. That dropped about 3 seconds off the ordinary SELECT and about 10 seconds when using the iTVF.

I tried the iTVF both with a WITH SELECT/INSERT INTO and an garden-variety INSERT INTO SELECT and the WITH statement version was the slowest.

Not sure what use anyone might have with that information, but that's my report!

 


 
Post #1440074
Posted Monday, April 8, 2013 6:52 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:36 PM
Points: 20,738, Visits: 32,522
Steven Willis (4/8/2013)
Jeff, just FYI...I created the million row table on my slow laptop and tested the code from your article. With a normal SELECT statement it took appx 19-22 secs per run to get a million rows.

Now out of curiosity, I whipped up an iTVF and ran that against the data with a cross apply and it did NOT perform well. Something on the order of 2 minutes or more. That just goes to show that iTVFs are not always the answer. I also re-tested after adding a non-clustered index to the temp table. That dropped about 3 seconds off the ordinary SELECT and about 10 seconds when using the iTVF.

I tried the iTVF both with a WITH SELECT/INSERT INTO and an garden-variety INSERT INTO SELECT and the WITH statement version was the slowest.

Not sure what use anyone might have with that information, but that's my report!

 


 


Could you post your code, including the itvf you created?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1440090
Posted Monday, April 8, 2013 7:34 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Lynn Pettis (4/8/2013)
Steven Willis (4/8/2013)
Jeff, just FYI...I created the million row table on my slow laptop and tested the code from your article. With a normal SELECT statement it took appx 19-22 secs per run to get a million rows.

Now out of curiosity, I whipped up an iTVF and ran that against the data with a cross apply and it did NOT perform well. Something on the order of 2 minutes or more. That just goes to show that iTVFs are not always the answer. I also re-tested after adding a non-clustered index to the temp table. That dropped about 3 seconds off the ordinary SELECT and about 10 seconds when using the iTVF.

I tried the iTVF both with a WITH SELECT/INSERT INTO and an garden-variety INSERT INTO SELECT and the WITH statement version was the slowest.

Not sure what use anyone might have with that information, but that's my report!
 

Could you post your code, including the itvf you created?


Jeff's test script:


--===== Conditionally drop the test table to make reruns in SSMS easier.
IF OBJECT_ID('tempdb..#TestTable','U') IS NOT NULL
DROP TABLE #TestTable
;
--===== Create and populate a million row test table with random dates/times
-- where (01 Jan 2000) <= Date < (01 Jan 2020)
SELECT TOP 1000000
Date = CAST(RAND(CHECKSUM(NEWID()))*DATEDIFF(dd,'2000','2020')+DATEDIFF(dd,0,'2000') AS DATETIME)
INTO #TestTable
FROM master.sys.all_columns ac1
CROSS JOIN master.sys.all_columns ac2
;
GO



SELECT
tvf.*
FROM
#TestTable tt
CROSS APPLY
dbo.itvfGetWeekNumberFromDate_ISO(tt.Date) tvf



CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO]
(
@dDate DATETIME
)
RETURNS @WeekNumber TABLE
(
[Date] DATE NULL
,[DayOfWeek] VARCHAR(20) NULL
,[ISOWeek] INT NULL
)
WITH SCHEMABINDING
AS
BEGIN

/*
SELECT * FROM dbo.itvfGetWeekNumberFromDate_ISO('2009-12-24')
*/

;WITH cte AS
(
SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date]
,LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek]
)
INSERT INTO @WeekNumber
SELECT
Date
,DayOfWeek
,ISOWeek
FROM
cte

RETURN

END



CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO_v2]
(
@dDate DATETIME
)
RETURNS @WeekNumber TABLE
(
[Date] DATE NULL
,[DayOfWeek] VARCHAR(20) NULL
,[ISOWeek] INT NULL
)
WITH SCHEMABINDING
AS
BEGIN

/*
SELECT * FROM dbo.itvfGetWeekNumberFromDate_ISO_v2('2009-12-24')
*/

INSERT INTO @WeekNumber
SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date]
,LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek]

RETURN

END




 
Post #1440101
Posted Monday, April 8, 2013 8:45 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:36 PM
Points: 20,738, Visits: 32,522
Steven Willis (4/8/2013)
Lynn Pettis (4/8/2013)
Steven Willis (4/8/2013)
Jeff, just FYI...I created the million row table on my slow laptop and tested the code from your article. With a normal SELECT statement it took appx 19-22 secs per run to get a million rows.

Now out of curiosity, I whipped up an iTVF and ran that against the data with a cross apply and it did NOT perform well. Something on the order of 2 minutes or more. That just goes to show that iTVFs are not always the answer. I also re-tested after adding a non-clustered index to the temp table. That dropped about 3 seconds off the ordinary SELECT and about 10 seconds when using the iTVF.

I tried the iTVF both with a WITH SELECT/INSERT INTO and an garden-variety INSERT INTO SELECT and the WITH statement version was the slowest.

Not sure what use anyone might have with that information, but that's my report!
 

Could you post your code, including the itvf you created?


Jeff's test script:


--===== Conditionally drop the test table to make reruns in SSMS easier.
IF OBJECT_ID('tempdb..#TestTable','U') IS NOT NULL
DROP TABLE #TestTable
;
--===== Create and populate a million row test table with random dates/times
-- where (01 Jan 2000) <= Date < (01 Jan 2020)
SELECT TOP 1000000
Date = CAST(RAND(CHECKSUM(NEWID()))*DATEDIFF(dd,'2000','2020')+DATEDIFF(dd,0,'2000') AS DATETIME)
INTO #TestTable
FROM master.sys.all_columns ac1
CROSS JOIN master.sys.all_columns ac2
;
GO



SELECT
tvf.*
FROM
#TestTable tt
CROSS APPLY
dbo.itvfGetWeekNumberFromDate_ISO(tt.Date) tvf



CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO]
(
@dDate DATETIME
)
RETURNS @WeekNumber TABLE
(
[Date] DATE NULL
,[DayOfWeek] VARCHAR(20) NULL
,[ISOWeek] INT NULL
)
WITH SCHEMABINDING
AS
BEGIN

/*
SELECT * FROM dbo.itvfGetWeekNumberFromDate_ISO('2009-12-24')
*/

;WITH cte AS
(
SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date]
,LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek]
)
INSERT INTO @WeekNumber
SELECT
Date
,DayOfWeek
,ISOWeek
FROM
cte

RETURN

END



CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO_v2]
(
@dDate DATETIME
)
RETURNS @WeekNumber TABLE
(
[Date] DATE NULL
,[DayOfWeek] VARCHAR(20) NULL
,[ISOWeek] INT NULL
)
WITH SCHEMABINDING
AS
BEGIN

/*
SELECT * FROM dbo.itvfGetWeekNumberFromDate_ISO_v2('2009-12-24')
*/

INSERT INTO @WeekNumber
SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date]
,LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek]

RETURN

END




 


I hate to be the bearer of bad news but your function is not an inline table valued function, it is a multi-statement table valued function.



The following is your function rewritten as an inline table valued function:


CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO_v2]
(
@dDate DATETIME
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date],
LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek],
(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek];

GO




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1440117
Posted Tuesday, April 9, 2013 10:28 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:38 PM
Points: 35,371, Visits: 31,912
Spot on, Lynn. Thanks for the cover. Busy 20 hour day at work yesterday.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1440461
Posted Tuesday, April 9, 2013 11:22 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Lynn Pettis (4/8/2013)
I hate to be the bearer of bad news but your function is not an inline table valued function, it is a multi-statement table valued function.

The following is your function rewritten as an inline table valued function:


CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO_v2]
(
@dDate DATETIME
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date],
LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek],
(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek];

GO


Lynn, of course you're correct. Too many late hours and not enough sleep.

But I tested the iTVF against the mlTVF and really found no difference. Again, surprising results.


SELECT
CONVERT(VARCHAR(20),tt.Date,120) AS [Date]
,DATENAME(weekday,tt.Date) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,tt.Date) / 7 * 7 + 3) + 6) / 7 AS [ISOWeek]
FROM
#TestTable tt





CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO]
(
@dDate DATETIME
)
RETURNS @WeekNumber TABLE
(
[Date] DATE NULL
,[DayOfWeek] VARCHAR(20) NULL
,[ISOWeek] INT NULL
)
WITH SCHEMABINDING
AS
BEGIN

INSERT INTO @WeekNumber
SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date]
,LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek]

RETURN

END
GO


SELECT
tvf.*
FROM
#TestTable tt
CROSS APPLY
dbo.itvfGetWeekNumberFromDate_ISO(tt.date) tvf






CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO_v2]
(@dDate DATETIME)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date]
,LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,@dDate) / 7 * 7 + 3) + 6) / 7 AS [ISOWeek] ;

GO


SELECT
tvf.*
FROM
#TestTable tt
CROSS APPLY
dbo.itvfGetWeekNumberFromDate_ISO_v2(tt.date) tvf





 


  Post Attachments 
ISO_DATE_STATS3.PNG (139 views, 24.73 KB)
ISO_DATE_STATS2.PNG (140 views, 28.91 KB)
ISO_DATE_STATS1.PNG (135 views, 28.72 KB)
Post #1440478
Posted Tuesday, April 9, 2013 11:49 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:36 PM
Points: 20,738, Visits: 32,522
Create an empty database and run the following:


--===== Conditionally drop the test table to make reruns in SSMS easier.
IF OBJECT_ID('tempdb..#TestTable','U') IS NOT NULL
DROP TABLE #TestTable
;
--===== Create and populate a million row test table with random dates/times
-- where (01 Jan 2000) <= Date < (01 Jan 2020)
SELECT TOP 1000000
Date = CAST(RAND(CHECKSUM(NEWID()))*DATEDIFF(dd,'2000','2020')+DATEDIFF(dd,0,'2000') AS DATETIME)
INTO #TestTable
FROM master.sys.all_columns ac1
CROSS JOIN master.sys.all_columns ac2
;
GO

CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO_v2]
(
@dDate DATETIME
)
RETURNS @WeekNumber TABLE
(
[Date] DATE NULL
,[DayOfWeek] VARCHAR(20) NULL
,[ISOWeek] INT NULL
)
WITH SCHEMABINDING
AS
BEGIN

/*
SELECT * FROM dbo.itvfGetWeekNumberFromDate_ISO_v2('2009-12-24')
*/

INSERT INTO @WeekNumber
SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date]
,LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek]
,(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek]

RETURN

END
GO

CREATE FUNCTION [dbo].[itvfGetWeekNumberFromDate_ISO_v3]
(
@dDate DATETIME
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT
CONVERT(VARCHAR(20),@dDate,120) AS [Date],
LEFT(DATENAME(dw,@dDate),9) AS [DayOfWeek],
(DATEPART(dy,DATEDIFF(dd,0,@dDate)/7*7+3)+6)/7 AS [ISOWeek];

GO

set nocount on;
--===== Create the timer and bit-bucket variables and start the timer.
DECLARE @BitBucket1 sql_variant,
@BitBucket2 sql_variant,
@BitBucket3 sql_variant; --< change or add variables needed here to eliminate display distortion

Declare @cpu_ int,
@lreads_ bigint,
@eMsec_ bigint,
@Reads_ bigint,
@Writes_ bigint;
declare @CpuMs int,
@LogRds bigint,
@Elapsed bigint,
@Reads bigint,
@Writes bigint;

dbcc freeproccache with no_infomsgs;
dbcc freesystemcache('ALL') with no_infomsgs;
dbcc dropcleanbuffers with no_infomsgs;


Select
@cpu_ = cpu_time
, @lreads_ = logical_reads
, @eMsec_ = total_elapsed_time
, @Reads_ = reads
, @Writes_ = writes
From
sys.dm_exec_requests
Where
session_id = @@spid;

---
SELECT
@BitBucket1 = tvf.Date,
@BitBucket2 = tvf.DayOfWeek,
@BitBucket3 = tvf.ISOWeek
FROM
#TestTable tt
CROSS APPLY
itvfGetWeekNumberFromDate_ISO_v2(tt.Date) tvf
---

Select
@CpuMs = cpu_time - @cpu_
, @LogRds = logical_reads - @lreads_
, @Elapsed = total_elapsed_time - @eMsec_
, @Reads = reads - @Reads_
, @Writes = writes - @Writes_
From
sys.dm_exec_requests
Where
session_id = @@spid;
--===== Display the duration
PRINT 'CPU(ms): ' + right(' ' + cast(@CpuMs as varchar(10)),10) +
' Logical Reads: ' + right(' ' + cast(@LogRds as varchar(10)),10) +
' Elapsed(ms): ' + right(' ' + cast(@Elapsed as varchar(10)),10) +
' Reads: ' + right(' ' + cast(@Reads as varchar(10)),10) +
' Writes: ' + right(' ' + cast(@Writes as varchar(10)),10);

set nocount off;
GO

set nocount on;
--===== Create the timer and bit-bucket variables and start the timer.
DECLARE @BitBucket1 sql_variant,
@BitBucket2 sql_variant,
@BitBucket3 sql_variant; --< change or add variables needed here to eliminate display distortion

Declare @cpu_ int,
@lreads_ bigint,
@eMsec_ bigint,
@Reads_ bigint,
@Writes_ bigint;
declare @CpuMs int,
@LogRds bigint,
@Elapsed bigint,
@Reads bigint,
@Writes bigint;

dbcc freeproccache with no_infomsgs;
dbcc freesystemcache('ALL') with no_infomsgs;
dbcc dropcleanbuffers with no_infomsgs;


Select
@cpu_ = cpu_time
, @lreads_ = logical_reads
, @eMsec_ = total_elapsed_time
, @Reads_ = reads
, @Writes_ = writes
From
sys.dm_exec_requests
Where
session_id = @@spid;

---
SELECT
@BitBucket1 = tvf.Date,
@BitBucket2 = tvf.DayOfWeek,
@BitBucket3 = tvf.ISOWeek
FROM
#TestTable tt
CROSS APPLY
itvfGetWeekNumberFromDate_ISO_v3(tt.Date) tvf
---

Select
@CpuMs = cpu_time - @cpu_
, @LogRds = logical_reads - @lreads_
, @Elapsed = total_elapsed_time - @eMsec_
, @Reads = reads - @Reads_
, @Writes = writes - @Writes_
From
sys.dm_exec_requests
Where
session_id = @@spid;
--===== Display the duration
PRINT 'CPU(ms): ' + right(' ' + cast(@CpuMs as varchar(10)),10) +
' Logical Reads: ' + right(' ' + cast(@LogRds as varchar(10)),10) +
' Elapsed(ms): ' + right(' ' + cast(@Elapsed as varchar(10)),10) +
' Reads: ' + right(' ' + cast(@Reads as varchar(10)),10) +
' Writes: ' + right(' ' + cast(@Writes as varchar(10)),10);

set nocount off;
GO

set nocount on;
--===== Create the timer and bit-bucket variables and start the timer.
DECLARE @BitBucket1 sql_variant,
@BitBucket2 sql_variant,
@BitBucket3 sql_variant; --< change or add variables needed here to eliminate display distortion

Declare @cpu_ int,
@lreads_ bigint,
@eMsec_ bigint,
@Reads_ bigint,
@Writes_ bigint;
declare @CpuMs int,
@LogRds bigint,
@Elapsed bigint,
@Reads bigint,
@Writes bigint;

dbcc freeproccache with no_infomsgs;
dbcc freesystemcache('ALL') with no_infomsgs;
dbcc dropcleanbuffers with no_infomsgs;


Select
@cpu_ = cpu_time
, @lreads_ = logical_reads
, @eMsec_ = total_elapsed_time
, @Reads_ = reads
, @Writes_ = writes
From
sys.dm_exec_requests
Where
session_id = @@spid;

---
SELECT
BitBucket1 = tvf.Date,
BitBucket2 = tvf.DayOfWeek,
BitBucket3 = tvf.ISOWeek
FROM
#TestTable tt
CROSS APPLY
itvfGetWeekNumberFromDate_ISO_v2(tt.Date) tvf
---

Select
@CpuMs = cpu_time - @cpu_
, @LogRds = logical_reads - @lreads_
, @Elapsed = total_elapsed_time - @eMsec_
, @Reads = reads - @Reads_
, @Writes = writes - @Writes_
From
sys.dm_exec_requests
Where
session_id = @@spid;
--===== Display the duration
PRINT 'CPU(ms): ' + right(' ' + cast(@CpuMs as varchar(10)),10) +
' Logical Reads: ' + right(' ' + cast(@LogRds as varchar(10)),10) +
' Elapsed(ms): ' + right(' ' + cast(@Elapsed as varchar(10)),10) +
' Reads: ' + right(' ' + cast(@Reads as varchar(10)),10) +
' Writes: ' + right(' ' + cast(@Writes as varchar(10)),10);

set nocount off;
GO

set nocount on;
--===== Create the timer and bit-bucket variables and start the timer.
DECLARE @BitBucket1 sql_variant,
@BitBucket2 sql_variant,
@BitBucket3 sql_variant; --< change or add variables needed here to eliminate display distortion

Declare @cpu_ int,
@lreads_ bigint,
@eMsec_ bigint,
@Reads_ bigint,
@Writes_ bigint;
declare @CpuMs int,
@LogRds bigint,
@Elapsed bigint,
@Reads bigint,
@Writes bigint;

dbcc freeproccache with no_infomsgs;
dbcc freesystemcache('ALL') with no_infomsgs;
dbcc dropcleanbuffers with no_infomsgs;


Select
@cpu_ = cpu_time
, @lreads_ = logical_reads
, @eMsec_ = total_elapsed_time
, @Reads_ = reads
, @Writes_ = writes
From
sys.dm_exec_requests
Where
session_id = @@spid;

---
SELECT
BitBucket1 = tvf.Date,
BitBucket2 = tvf.DayOfWeek,
BitBucket3 = tvf.ISOWeek
FROM
#TestTable tt
CROSS APPLY
itvfGetWeekNumberFromDate_ISO_v3(tt.Date) tvf
---

Select
@CpuMs = cpu_time - @cpu_
, @LogRds = logical_reads - @lreads_
, @Elapsed = total_elapsed_time - @eMsec_
, @Reads = reads - @Reads_
, @Writes = writes - @Writes_
From
sys.dm_exec_requests
Where
session_id = @@spid;
--===== Display the duration
PRINT 'CPU(ms): ' + right(' ' + cast(@CpuMs as varchar(10)),10) +
' Logical Reads: ' + right(' ' + cast(@LogRds as varchar(10)),10) +
' Elapsed(ms): ' + right(' ' + cast(@Elapsed as varchar(10)),10) +
' Reads: ' + right(' ' + cast(@Reads as varchar(10)),10) +
' Writes: ' + right(' ' + cast(@Writes as varchar(10)),10);

set nocount off;
GO





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1440485
Posted Tuesday, April 9, 2013 8:18 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 9:51 PM
Points: 612, Visits: 2,858
Wow.

Amazing work Jeff. Great article too. 5 stars.



-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001

My blog
Post #1440629
Posted Tuesday, April 9, 2013 10:34 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:38 PM
Points: 35,371, Visits: 31,912
Alan.B (4/9/2013)
Wow.

Amazing work Jeff. Great article too. 5 stars.



Thanks, Alan. I really appreciate that.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1440640
Posted Tuesday, April 9, 2013 11:50 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:35 AM
Points: 6,733, Visits: 8,485
Lynn Pettis (4/9/2013)
Create an empty database and run the following:
...


Tested your script at my slow box:

CPU(ms):     102009 Logical Reads:   10002259 Elapsed(ms):     104478 Reads:       2044 Writes:          0
CPU(ms): 2745 Logical Reads: 2175 Elapsed(ms): 2867 Reads: 2016 Writes: 0
CPU(ms): 100028 Logical Reads: 10002257 Elapsed(ms): 101861 Reads: 2043 Writes: 0
CPU(ms): 2090 Logical Reads: 2175 Elapsed(ms): 11723 Reads: 2016 Writes: 0
[

Clear case with regards to the conclusion for the test

Lovely script on how easy one can just measure such consumption.


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #1440660
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse