Author |
Topic |
Masum7
Starting Member
33 Posts |
Posted - 2009-06-04 : 00:14:27
|
I have a table resources like this:id userid1 12 13 14 25 26 37 38 3Here id is pkey.I want to a select query which will select from resources in round robin fashion based on userid.I mean something like this:1 14 26 32 15 27 33 18 3Please note selection of userid 1, 2, 3 then again 1, 2, 3, then 2 is finishes so selection is 1, 3 Masum |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-04 : 00:27:43
|
SELECT ID, UserIDFROM (SELECT ID, UserID, ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY ID) AS recID FROM Table1) AS dORDER BY recID, ID E 12°55'05.63"N 56°04'39.26" |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 00:27:55
|
[code]DECLARE @resource TABLE( id int, userid int)INSERT INTO @resourceSELECT 1, 1 UNION ALLSELECT 2, 1 UNION ALLSELECT 3, 1 UNION ALLSELECT 4, 2 UNION ALLSELECT 5, 2 UNION ALLSELECT 6, 3 UNION ALLSELECT 7, 3 UNION ALLSELECT 8, 3SELECT id, useridFROM( SELECT *, row_no = row_number() OVER (PARTITION BY userid ORDER BY userid, id) FROM @resource) rORDER BY row_no, userid/*id userid ----------- ----------- 1 14 26 32 15 27 33 18 3(8 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 00:29:03
|
took 32 secs to do the formatting  KH[spoiler]Time is always against us[/spoiler] |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-04 : 00:31:00
|
Don't forget you included the sample data too! E 12°55'05.63"N 56°04'39.26" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-04 : 00:32:48
|
I have a question for you khtan. I've often seen you inlude the partition key into the order by key. Is this by habit or is it faster (because of the partition key is the same)? E 12°55'05.63"N 56°04'39.26" |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 00:41:32
|
Never check the performance difference . It is more of a habit or thinking when need to sort the rows / results in the usual ORDER BY, brought over to the over() window function.edit: Just did a quick test, the execution plan are the same KH[spoiler]Time is always against us[/spoiler] |
 |
|
Masum7
Starting Member
33 Posts |
Posted - 2009-06-04 : 00:54:51
|
Many thanks khtan & Peso.Really great. I have also learned about partition.Thank you guys.Masum |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 00:55:24
|
you are welcome KH[spoiler]Time is always against us[/spoiler] |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-04 : 02:51:36
|
It seems my suggestion is about 3-5 percent faster. Hardly measurable that is.And if you think about it, it seems logical that order by two columns is slower than ordering by just one column.There is no need to order by the partitioning key since that value will the same for whole partition related to the order by statement.CREATE TABLE #Robin ( ID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, UserID INT NOT NULL )INSERT #Robin ( UserID )SELECT ABS(CHECKSUM(NEWID())) % 10FROM master..spt_values AS v1INNER JOIN master..spt_values AS v2 ON v2.Type = 'P'WHERE v1.Type = 'P' DELETE fFROM ( SELECT TOP 50 PERCENT ID FROM #Robin ORDER BY NEWID() ) AS fCREATE NONCLUSTERED INDEX IX_Robin ON #Robin (UserID, ID)DBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSGO-- PesoSELECT ID, UserIDFROM ( SELECT ID, UserID, ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY ID) AS recID FROM #Robin ) AS rORDER BY recID, UserIDGO 2DBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSGO-- KhtanSELECT ID, UserIDFROM ( SELECT ID, UserID, ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY UserID, ID) AS row_no FROM #Robin ) AS rORDER BY row_no, UserIDGO 2DROP TABLE #Robin E 12°55'05.63"N 56°04'39.26" |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 03:34:03
|
Yes. you are right. Yours is slightly faster. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|