|
|
Writing Efficient SQL: Set-Based Speed Phreakery
-
02-08-2010, 7:24 AM |
|
|
download is not case sensitive compatible
Why are scripts never written for case sensitive databases? Commence find and replace on the download.....
|
|
-
02-08-2010, 8:39 AM |
-
02-08-2010, 9:05 AM |
|
|
Tony and I debated on whether we should use the term "quirky" or not but went with it in the end. Once you get the data aggregated down to just a handfull of rows, it doesn't make as much difference how you do the last step. The real beauty of the winning entry is the "one pass" pre-aggregation.
|
|
-
02-08-2010, 2:14 PM |
-
02-08-2010, 2:15 PM |
-
02-08-2010, 2:45 PM |
-
02-08-2010, 3:36 PM |
|
|
select * from Registrations
declare @starTime datetime declare @minDate datetime declare @runTotal int select @runTotal = 0 select @starTime = GETDATE()
select @minDate = MIN(DateJoined)from Registrations
SELECT DATEDIFF(MM,@minDate,Datejoined) as s1, COUNT(*) as counts, @runTotal as runningTotal INTO #1 FROM Registrations GROUP BY DATEDIFF(MM,@minDate,Datejoined) order by DATEDIFF(MM,@minDate,Datejoined) asc
select DATEDIFF(MM,@minDate,DateLeft) as s1, COUNT(*) as counts, @runTotal as runningTotal INTO #2 FROM Registrations where DateLeft is not null GROUP BY DATEDIFF(MM,@minDate,DateLeft) order by DATEDIFF(MM,@minDate,DateLeft) asc
update #1 set @runTotal= runningTotal = @runTotal + c.counts - ISNULL(#2.counts,0) from #1 as c left outer join #2 on c.s1 = #2.s1
SELECT DATEADD(MM,#1.s1,@minDate) as Monthly , #1.counts as NewJoins , #2.counts as LeftUs, #1.runningTotal FROM #1 LEFT JOIN #2 on #1.s1 = #2.s1 order by runningTotal Asc
select DATEDIFF(mcs,@starTime,getdate())
|
|
-
02-08-2010, 5:06 PM |
|
|
what if using a CTE for #stage?
Thanks for the article, definitely worth reading.
I would appreciate if somebody explain what happens if we use a CTE instead of the #Stage table in the solution "Peso 4e"? Will it be possible at all and how performance will be affected?
Just curicity from my side, and general perseption that keeping everything in memory (avoiding writing to the tempdb) is beneficial.
Appreciate your thoughts.
|
|
-
02-08-2010, 5:53 PM |
-
puzsol
-
-
-
Joined on 11-01-2007
-
Melbourne Victoria
-
-
-
|
Conditions for Quirky Update to fail
It's not just that the CTE has too few rows to use a parallel operation... in my experience (at least on SQL 2005) table variables (and by extension CTEs?) will never use parallel operations. Spent a bit of time converting all table variables to temporary tables for just that reason!
If my assertion is still correct (and I suspect it is, and will remain so), then all three criteria are met by using a CTE/table variable: - it is ordered - it will not parellelise itself (no matter how big) - it will not partition.
So it won't break in this use.... just because someone could misuse a technique doesn't mean you shouldn't use it. Hey I've seen insert/update triggers written as if they were for only one row... so should we stop using triggers altogether or is that a different discussion?
|
|
-
02-08-2010, 5:57 PM |
|
|
what if using a CTE for #stage?
Thanks for the article, definitely worth reading.
I would appreciate if somebody explain what happens if we use a CTE instead of the #Stage table in the solution "Peso 4e"? Will it be possible at all and how performance will be affected?
Just curicity from my side, and general perseption that keeping everything in memory (avoiding writing to the tempdb) is beneficial.
Appreciate your thoughts.
|
|
-
02-08-2010, 6:13 PM |
-
puzsol
-
-
-
Joined on 11-01-2007
-
Melbourne Victoria
-
-
-
|
Conditions for Quirky Update to fail
It's not just that the CTE has too few rows to use a parallel operation... in my experience (at least on SQL 2005) table variables (and by extension CTEs?) will never use parallel operations. Spent a bit of time converting all table variables to temporary tables for just that reason!
If my assertion is still correct (and I suspect it is, and will remain so), then all three criteria are met by using a CTE/table variable: - it is ordered - it will not parellelise itself (no matter how big) - it will not partition.
So it won't break in this use.... just because someone could misuse a technique doesn't mean you shouldn't use it. Hey I've seen insert/update triggers written as if they were for only one row... so should we stop using triggers altogether or is that a different discussion?
|
|
-
02-08-2010, 7:21 PM |
|
|
It didn't break... it was forced to not work.
>>>There are other things than what Hugo posted that can break it
Hugo posted things that wouldn't allow it to work to begin with. The rest of it, I posted to show that my poor explanation of why it worked and when it won't needed to be changed.
So far as "could break" when a future CU or SP comes out, so what? That could happen with anything and if you don't regression test before such installations, you're begging for trouble. An example of a change that was not on the deprecation list that broke a lot of code was when they changed the very heavily documented sp_MakeWebTask to require "SA" privs in SQL Server 2000 SP4. Although Microsoft is pretty good about following "the plan" in the form of deprecation lists, Microsoft can, has, and will continue to change anything they need to without warning... documented or not.
|
|
-
02-08-2010, 8:52 PM |
|
|
what if using a CTE for #stage?
Thanks for the article, definitely worth reading.
I would appreciate if somebody explain what happens if we use a CTE instead of the #Stage table in the solution "Peso 4e"? Will it be possible at all and how performance will be affected?
Just curiosity from my side, and general perception that keeping everything in memory (avoiding writing to the tempdb) is beneficial.
Appreciate your thoughts.
|
|
-
02-09-2010, 2:21 PM |
|
|
"I would appreciate if somebody explain what happens if we use a CTE instead of the #Stage table in the solution "Peso 4e"? Will it be possible at all and how performance will be affected?"
You can try it, but I don't think you can nest a CTE inside another CTE which is what you would have to do if I understand your proposal.
|
|
-
02-09-2010, 6:58 PM |
|
|
Temp Tables can live in memory, too...
----------------------------------------------- >>> "Just curicity from my side, and general perseption that keeping everything in memory (avoiding writing to the tempdb) is beneficial." ----------------------------------------------- Using a Temp table doesn't mean it's not in memory. The first place a Temp table will try to live is the same place a Table variable or the result set of a CTE may try to live... memory. When a Table Variable or Temp Table get to big for memory, they both do the same thing... start using disk space in TempDB.
|
|
Page 2 of 4 (48 items)
2
|
|