February 11, 2014 at 7:31 am
Team,
Using of Dense_rank() and row_number will leads to any performance issue? if yes can please suggest other alternative
John
February 11, 2014 at 8:40 am
The most accurate response I can give you is: It depends.
Both functions won't cause performance issues on their own, but could be used in an inefficient way and produce performance problems.
The question is, how are you trying to use them? are they part of a performance problem you're experiencing?
If it's for new development, just test and test again.
February 11, 2014 at 10:32 am
Both functions will result in a sort unless a) the data coming into the operator is sorted same as the order by of the function and b) the query processor can determine that this is the case. As Luis said "it depends". Run your queries and check the plans.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
February 11, 2014 at 4:05 pm
Dwain Camps wrote this great article about this recently which was consistent with my experience using windows functions: The Performance of the T-SQL Window Functions[/url]. I suggest giving it a read.
RANK(), DENSE_RANK(), ROW_NUMBER() and NTILE() are examples of Ranking Functions. They seem to perform pretty well.
ROW_NUMBER() is extra special because, when used without any sorting, it is wicked fast. It's not common to use RANK() or DENSE_RANK() without a specific order (why would you) but a it's common to use ROW_NUMBER without any ORDER by for situations where you need a surrogate key or just need a sequence of numbers for any reason without any gaps.
Below is a quick test I put together to demonstrate how ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) is faster than identity for this type of task. ROW_NUMBER() with no sorting will outperform a loop, cursor, or any other iterative method for counting in T-SQL.
-- temp tables for testing
IF OBJECT_ID('tempdb..#sampledata') IS NOT NULL DROP TABLE #sampledata;
IF OBJECT_ID('tempdb..#targetTable1') IS NOT NULL DROP TABLE #targetTable1;
IF OBJECT_ID('tempdb..#targetTable2') IS NOT NULL DROP TABLE #targetTable2;
CREATE TABLE #sampledata(value varchar(36) not null);
CREATE TABLE #targetTable1(s_id int identity, value varchar(36));--with identity
CREATE TABLE #targetTable2(s_id int, value varchar(36));--using row_number()
-- get random sample data
INSERT #sampledata(value)
SELECT TOP(1000000) newid()
FROM sys.all_columns a
CROSS JOIN sys.all_columns b;
-- the performance test
SET NOCOUNT ON;
DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS
DBCC FREEPROCCACHE WITH NO_INFOMSGS
PRINT 'using identity:'
SET STATISTICS TIME ON;
INSERT #targetTable1(value)
SELECT value FROM #sampledata;
SET STATISTICS TIME OFF;
DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS
DBCC FREEPROCCACHE WITH NO_INFOMSGS
PRINT CHAR(13)+CHAR(13)
PRINT 'using ROW_NUMBER() OVER (ORDER BY (SELECT NULL))'
SET STATISTICS TIME ON;
INSERT #targetTable2(s_id,value)
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), value
FROM #sampledata
SET STATISTICS TIME OFF;
GO
Any thoughts, comments, questions are welcome.
Edit: Typos
-- Itzik Ben-Gan 2001
February 12, 2014 at 1:09 am
Thanks for your quick replies, nice article shared by Alan.B, yes obsolutely it depends, now we are developing a new database just wanted to check going forward (if data increases) will be a performance problem. Thanks for your views.
Regards
John
February 23, 2014 at 6:22 pm
I think I said in the article that ROW_NUMBER() and the other ranking functions are all good performing options over trying to code the same thing yourself a different way.
I do want to emphasize that what Luis and Chris said are equally important considerations. Indexing on the table may impact the performance you get out of ROW_NUMBER (improving it assuming your PARTITION/ORDER BY condition matches the CLUSTERED INDEX).
And like anything that is good, they could be misused to make them very, very bad.
Edit: Oh yes and Alan, thanks for the plug.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
February 24, 2014 at 12:12 am
JohnNash (2/12/2014)
just wanted to check going forward (if data increases) will be a performance problem.
Honestly, there's no way to answer that question. Depends on too many factors.
Write the code, test on representative data volumes, if performance is inadequate adjust the code.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply