由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 谁能帮我看看这个sql query的优化
相关主题
Help on Sql server huge table performanceImport data from excel into sql server 2000
INSERT or UPDATE, which is faster?Truncation error import csv file to SQL table
什么时候不用索引a simple question about T-SQL
Trigger questionsProblem when using SQL " Insert...." to AutoNumber.
MySQL 的一个问题求教how to improve the performance of Oracle Insert operation?
Common Table Expression 问题urgent help! insert value into table
Re: Is there any easy way to truncate the LDF file of SQL SERVER?Set autocommit off
Oracle求助,thanks!!!trigger vs. log ?
相关话题的讨论汇总
话题: folderid话题: temp话题: select话题: proj话题: insert
进入Database版参与讨论
1 (共1页)
s******n
发帖数: 34
1
想要查找一个目录的某个级的子目录, 返回在一个表中.
看看我写的, 挺傻的, 不知道怎么可以优化下
目录关系存在proj_tab里面
create function getSubFolder(@level int, @folderId int)
returns @retab table(folderId int)
as
begin
declare @temp table(folderId int)
insert into @temp select folderId from proj_tab where parentno = @
folderId
select @level = @level -1
while @level <> 0
begin
insert into @retab select folderId from proj_tab where parentno in (
select folderId from @temp)
delete from @temp
insert into @temp
m***0
发帖数: 15
2
1) use truncate instead of del
2) depends on depth and size of ur tree, if it can go up to 5 or event more, and you do reads more than writes, you might want to change the logic completely, add one more column called depth, and change column parent to column ancestor, and each node will have all the info about all of its ancestor, if a node in lvl 5 is to be inserted, then it should also insert 4 more rows for each of its ancestor.
in this way, u will have instant results back when u do select.

【在 s******n 的大作中提到】
: 想要查找一个目录的某个级的子目录, 返回在一个表中.
: 看看我写的, 挺傻的, 不知道怎么可以优化下
: 目录关系存在proj_tab里面
: create function getSubFolder(@level int, @folderId int)
: returns @retab table(folderId int)
: as
: begin
: declare @temp table(folderId int)
: insert into @temp select folderId from proj_tab where parentno = @
: folderId

B*****g
发帖数: 34098
3
顶一个。没有connected by,苦呀。

【在 s******n 的大作中提到】
: 想要查找一个目录的某个级的子目录, 返回在一个表中.
: 看看我写的, 挺傻的, 不知道怎么可以优化下
: 目录关系存在proj_tab里面
: create function getSubFolder(@level int, @folderId int)
: returns @retab table(folderId int)
: as
: begin
: declare @temp table(folderId int)
: insert into @temp select folderId from proj_tab where parentno = @
: folderId

j*****n
发帖数: 1781
4
if MS SQL Server 2005, go books online and look for "Recursive Queries Using
Common Table Expressions", there is an example exactly what you want.
1 (共1页)
进入Database版参与讨论
相关主题
trigger vs. log ?MySQL 的一个问题求教
How to handle inserting value to Identity column in sql server 2005Common Table Expression 问题
MS T-SQL 问题Re: Is there any easy way to truncate the LDF file of SQL SERVER?
[转载] 用perl或者java访问数据库Oracle求助,thanks!!!
Help on Sql server huge table performanceImport data from excel into sql server 2000
INSERT or UPDATE, which is faster?Truncation error import csv file to SQL table
什么时候不用索引a simple question about T-SQL
Trigger questionsProblem when using SQL " Insert...." to AutoNumber.
相关话题的讨论汇总
话题: folderid话题: temp话题: select话题: proj话题: insert