简体   繁体   English

我对SQL Server 2005中的性能有一个较大的查询

[英]I have one big query for performance related in SQL Server 2005

I have one big query for performance related in SQL Server 2005. 我对SQL Server 2005中的性能有一个很大的疑问。

I have data like this 我有这样的数据

id  parentId
1   null
2   1
3   1
4   2
5   4
6   3

I want the order for the records by downline with parentId and id wise like 我想要下划线的记录顺序与parentId和id明智

id  Order
1   1
2   2
4   3
5   4
3   5
4   6

I don't want to use loop, because loop is creating the problem if high number of rows. 我不想使用循环,因为如果行数过多,循环会造成问题。 Please give me easy way to do it and hurting performance. 请给我一个简单的方法来完成它,并损害性能。

Updated, Please run the script below 更新,请运行下面的脚本

create table [mytable] ( [id] int, [parentId] int ) GO

INSERT INTO [mytable] ([id],[parentId])VALUES(1,NULL) INSERT INTO [mytable] ([id],[parentId])VALUES(2,6) INSERT INTO [mytable] ([id],[parentId])VALUES(4,9) INSERT INTO [mytable] ([id],[parentId])VALUES(5,4) INSERT INTO [mytable] ([id],[parentId])VALUES(6,13) INSERT INTO [mytable] ([id],[parentId])VALUES(7,13) INSERT INTO [mytable] ([id],[parentId])VALUES(8,5) INSERT INTO [mytable] ([id],[parentId])VALUES(9,1) INSERT INTO [mytable] ([id],[parentId])VALUES(13,1) GO

; WITH q AS ( SELECT id, parentId, CAST(id AS VARCHAR(MAX)) + '/' AS path FROM mytable WHERE parentId IS NULL UNION ALL SELECT t.id, t.parentId, q.path + CAST(t.id AS VARCHAR(MAX)) + '/' FROM q JOIN mytable t ON t.parentId = q.id ) SELECT *, ROW_NUMBER() OVER (ORDER BY path) AS rn FROM q ORDER BY path GO

The result of this query ID ParentId Path rn 1 NULL 1/ 1 13 1 1/13/ 2 6 13 1/13/6/ 3 2 6 1/13/6/2/ 4 7 13 1/13/7/ 5 9 1 1/9/ 6 4 9 1/9/4/ 7 5 4 1/9/4/5/ 8 8 5 1/9/4/5/8/ 9

But i want the result for rn from avove result first 1 then 1/9 then1/9/... then 1/13/ then 1/13/... .Please give me the solution for that. 但是我想从结果中得到rn的结果,首先是1,然后是1/9,然后是1/9 / ...,然后是1/13 /,然后是1/13 /...。请给我解决方案。

I want result like 我想要类似的结果



ID    ParentId  Path     rn 
1   NULL    1/       1 
13  1   1/13/       6 
6   13  1/13/6/      7 
2   6   1/13/6/2/    8
7   13  1/13/7/      9
9   1   1/9/         2 
4   9   1/9/4/       3 
5   4   1/9/4/5/     4 
8   5   1/9/4/5/8/   5 




WITH    q AS 
        ( 
        SELECT  id, parentId, CAST(id AS VARCHAR(MAX)) AS path 
        FROM    mytable 
        WHERE   parentId IS NULL 
        UNION ALL 
        SELECT  t.id, t.parentId, q.path + '/' + CAST(t.id AS VARCHAR(MAX)) 
        FROM    q 
        JOIN    mytable t 
        ON      t.parentId = q.id 
        ) 
SELECT  *, ROW_NUMBER() OVER (ORDER BY path) AS rn 
FROM    q 
ORDER BY 
        path 

In above Have one more question. 在上面还有一个问题。 you are using the order by path suppose,in the case of the records like 1/13 and 1/2 , so order by is comming 1/13 and 1/2, but i want the order 1/2 and then 1/13 , because 2 is less then 13. 您正在使用按路径排序,假设记录是1/13和1/2的情况,那么order by是1/13和1/2的结果,但是我想要1/2的顺序然后1/13 ,因为2小于13。

WITH    q AS
        (
        SELECT  id, parentId, CAST(id AS VARCHAR(MAX)) + '/' AS path
        FROM    mytable
        WHERE   parentId IS NULL
        UNION ALL
        SELECT  t.id, t.parentId, q.path + CAST(t.id AS VARCHAR(MAX)) + '/'
        FROM    q
        JOIN    mytable t
        ON      t.parentId = q.id
        )
SELECT  *, ROW_NUMBER() OVER (ORDER BY path) AS rn
FROM    q
ORDER BY
        path

我无法确切说明您的数据库是什么样子,但是这样的工作应该可以

Select id, [Order] From Table1 Order By Order, id
select id, parentid as order from table 
order by coalesce(parentid, 9999), id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM