無限分級數量查詢優化

2021-08-29 15:33:55 字數 4479 閱讀 4342

無限分級的資料查詢是個頭痛的問題,遞迴查詢類別,再組合成字串,用 in 來解決子類所有產品的問題,但是這個效率太低,低的讓人無法接受,在此,有乙個sql的方法,可讓我們提高效率。

-----提取子類的所有類別id

create function getchild (@id int)

returns @t table(id int)

asbegin

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)

asbegin

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)

asbegin

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

endreturn

endselect * 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

gocreate 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

asdeclare @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

----------- 13

45*/[zt

無限分級數量查詢優化

無限分級的資料查詢是個頭痛的問題,遞迴查詢類別,再組合成字串,用 in 來解決子類所有產品的問題,但是這個效率太低,低的讓人無法接受,在此,有乙個sql的方法,可讓我們提高效率。提取子類的所有類別id create function getchild id int returns t table i...

推薦 無限分級數量查詢優化

無限分級的資料查詢是個頭痛的問題,遞迴查詢類別,再組合成字串,用 in 來解決子類所有產品的問題,但是這個效率太低,低的讓人無法接受,在此,有乙個sql的方法,可讓我們提高效率。提取子類的所有類別id create function getchild id int returns t table i...

oracle千萬級資料查詢優化

需求 組合查詢,按條件統計某幾個欄位取前100條記錄 問題 沒建索引導致查詢結果耗時5秒多,不能忍受。解決方法 建索引,在哪個欄位建?在這裡先提下oracle的sql語句的執行。oracle在執行sql語句之前會用優化器optimizer對sql語句進行解析,解析出最優的執行計畫再執行,這樣所花費的...