`
justshare
  • 浏览: 103852 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

无限分级数量查询优化

阅读更多
无限分级的数据查询是个头痛的问题,递归查询类别,再组合成字符串,用 in 来解决子类所有产品的问题,但是这个效率太低,低的让人无法接受,在此,有一个SQL的方法,可让我们提高效率。
-----提取子类的所有类别ID
create function GetChild (@id int)
returns @t table(id int)
as
begin
    insert @t select classid from mproclass where parentid = @id
    while @@rowcount > 0
        insert @t select a.classid from mproclass as a inner join @t as b
        on a.parentid = b.id and a.classid not in(select id from @t)
   return
end
-----提取子类以及自己ID的所有类别ID
create function GetChildAndSelf (@id int)
returns @t table(id int)
as
begin
    insert @t values (@id)
    insert @t select classid from mproclass where parentid = @id
    while @@rowcount > 0
        insert @t select a.classid from mproclass as a inner join @t as b
        on a.parentid = b.id and a.classid not in(select id from @t)
   return
end

以上是最佳方案

查询该类别的产品数据的sql为:
sql = "select * from mProduct as a inner join (select [id] from GetChildAndSelf("+classid+")) as b on a.classid=b.id order by psortid desc,pdate desc";

太爽了!

网上还有其他几种方法,贴出来大家一起学习学习

一、
declare   @table   table(id   int,upperid   int)
insert   into   @table
select   1,           2
union   all   select   3,           2
union   all   select   4,           1
union   all   select   5,           3
declare   @upperid   int  
set   @upperid=2;
with   result(id,upperid)
as
(
select   id,upperid   from   @table   where   upperid=@upperid
union   all
select   a.id,a.upperid   from   @table   a   inner   join   result   b   on   a.upperid=b.id
)
select*from   result
/*
id                     upperid
-----------   -----------
1                       2
3                       2
5                       3
4                       1

(4   row(s)   affected)
*/

二、
Create   table   t(id   int,upperid   int)
insert   into   t
select   1,           2
union   all   select   3,           2
union   all   select   4,           1
union   all   select   5,           3
select   *   from   t
create   function   aa(@upperid   int)
returns   @t   table   (id   int,upperid   int,level   int)
as
begin
declare   @i   int
set   @i=1
insert   into   @t
select   *,@i   from   t   where   upperid=@upperid
while   @@rowcount> 0
begin
set   @i=@i+1
insert   into   @t
select   a.*,@i   from   t   a   left   join   @t   b   on   a.upperid=b.id
where   b.level=@i-1
end
return
end

select   *   from   dbo.aa(1)

id                     upperid           level              
-----------   -----------   -----------  
4                       1                       1

(所影响的行数为   1   行)

select   *   from   dbo.aa(2)

id                     upperid           level              
-----------   -----------   -----------  
1                       2                       1
3                       2                       1
4                       1                       2
5                       3                       2


三、
----创建测试数据
if   object_id( 'tbTest ')   is   not   null
drop   table   tbTest
if   object_id( 'spGetChildren ')   is   not   null
drop   proc   spGetChildren
GO
create   table   tbTest(id   int,     upperid   int)
insert   tbTest
select   1,           2   union   all
select   3,           2   union   all
select   4,           1   union   all
select   5,           3
GO
----创建存储过程
create   proc   spGetChildren   @id   int
as
        declare   @t   table(id   int)
        insert   @t   select   id   from   tbTest   where   upperid   =   @id
        while   @@rowcount   >   0
                insert   @t   select   a.id   from   tbTest   as   a   inner   join   @t   as   b
                on   a.upperid   =   b.id   and   a.id   not   in(select   id   from   @t)
        select   *   from   @t
GO

----执行存储过程
declare   @upperid   int
set   @upperid   =   2
EXEC   spGetChildren   @upperid

----清除测试环境
drop   proc   spGetChildren
drop   table   tbTest

/*结果
id                    
-----------  
1
3
4
5
*/
[ZT  http://www.cnblogs.com/skylaugh/archive/2008/06/03/1213133.html]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics