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

dense_rank Expand / Collapse
Author
Message
Posted Tuesday, February 11, 2014 7:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 5:16 AM
Points: 12, Visits: 51
Team,

Using of Dense_rank() and row_number will leads to any performance issue? if yes can please suggest other alternative


John
Post #1540189
Posted Tuesday, February 11, 2014 8:40 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:56 AM
Points: 3,545, Visits: 7,660
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.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1540247
Posted Tuesday, February 11, 2014 10:32 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 8:04 AM
Points: 1,090, Visits: 6,540
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.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1540328
Posted Tuesday, February 11, 2014 4:05 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 7:30 PM
Points: 562, Visits: 2,618
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. 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


-- 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)
My blog
Post #1540483
Posted Wednesday, February 12, 2014 1:09 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 5:16 AM
Points: 12, Visits: 51
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
Post #1540580
Posted Sunday, February 23, 2014 6:22 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:37 AM
Points: 3,627, Visits: 5,275
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1544336
Posted Monday, February 24, 2014 12:12 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 42,829, Visits: 35,959
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 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1544367
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse