【发布时间】:2014-08-10 12:51:31
【问题描述】:
我正在使用Tarazc4 sp计算我的应用程序的资产负债表,但是当我的记录达到50000(超时错误)时非常耗时,请帮忙!我现在想不通了!!!
SELECT *
INTO #levels
FROM dbo.topics_getlevel()
SELECT *
INTO #t1
FROM accounting.documentdetail
WHERE accounting.documentdetail.date BETWEEN @FromDate AND @ToDate
SELECT *
FROM
(SELECT TOP (100) PERCENT
Accounting.Topics.Code, Accounting.Topics.TopicID, Accounting.Topics.ParentID,
Accounting.Topics.Description,
(SELECT Debit
FROM dbo.SubTopics_GetSum(Accounting.Topics.TopicID, @FromDate, @ToDate) AS SubTopics_GetSum_2) AS Debit,
(SELECT Credit
FROM dbo.SubTopics_GetSum(Accounting.Topics.TopicID, @FromDate, @ToDate) AS SubTopics_GetSum_1) AS Credit,
(CASE
WHEN ((SELECT Credit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate)) -
(SELECT Debit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate))) < 0 THEN
((SELECT Debit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate)) -
(SELECT Credit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate))) ELSE 0 END) AS ResDEBIT, (CASE WHEN
((SELECT Credit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate)) -
(SELECT Debit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate))) > 0 THEN
((SELECT Credit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate)) -
(SELECT Debit
FROM dbo.subtopics_getsum(Accounting.Topics.TopicID,@FromDate,@ToDate))) ELSE 0 END) AS ResCREDIT,(select [Level] from #Levels where TopicID=Accounting.Topics.TopicID) as [Level]
FROM Accounting.Topics Left OUTER JOIN
#t1 ON Accounting.Topics.TopicID = #t1.TopicFK
GROUP BY Accounting.Topics.TopicID, Accounting.Topics.ParentID, Accounting.Topics.Code, Accounting.Topics.Description
ORDER BY Accounting.Topics.TopicID
)
t2
--------------------------------END Tarazc4
并在Tarazc4 sp中使用“dbo.subtopics_getsum”函数来获取每个帐户的总和,请检查一下:
ALTER Function [dbo].[SubTopics_GetSum]
(
@TopicID int,@FromDate char(10),@ToDate char(10)
)
RETURNS TABLE
AS
RETURN
(
WITH cte AS (
SELECT T1.TopicID, T1.Code, T1.Description, T1.ParentID,
T1.ParentID AS NewParentID,
CAST(T1.Code AS nvarchar(MAX)) AS TopicCode,
CAST(T1.Description AS nvarchar(MAX)) AS TopicDescription,
isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
isnull((Accounting.DocumentDetail.Credit),0) AS Credit
FROM Accounting.Topics AS T1
LEFT OUTER JOIN
Accounting.DocumentDetail ON T1.TopicID = Accounting.DocumentDetail.TopicFK
where (Accounting.DocumentDetail.date between @FromDate and @ToDate)
and NOT EXISTS(
SELECT T2.TopicID, T2.Code, T2.Description, T2.ParentID,
isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
isnull((Accounting.DocumentDetail.Credit),0) AS Credit
FROM Accounting.Topics AS T2
LEFT OUTER JOIN
Accounting.DocumentDetail
ON T2.TopicID = Accounting.DocumentDetail.TopicFK
WHERE (Accounting.DocumentDetail.date between @FromDate and @ToDate)
and (ParentID = T1.TopicID)
)
UNION ALL
SELECT c.TopicID, c.Code, c.Description, c.ParentID, T1.ParentID AS NewParentID,
CAST(T1.Code AS nvarchar(MAX)) + c.TopicCode AS TopicCode,
CAST(T1.Description AS nvarchar(MAX)) + ' - ' +
c.TopicDescription AS TopicDescription,
c.Debit AS Debit,c.Credit AS Credit
FROM cte AS c
INNER JOIN Accounting.Topics AS T1
ON T1.TopicID = c.NewParentID
)
select
isnull(sum(Debit),0)+
isnull(
(select sum(debit) from accounting.documentdetail
where (Accounting.DocumentDetail.date between @FromDate and @ToDate)
and topicfk=@TopicID)
,0) as Debit,
isnull(Sum(Credit),0)+
isnull(
(select sum(credit) from accounting.documentdetail
where (Accounting.DocumentDetail.date between @FromDate and @ToDate)
and topicfk=@TopicID)
,0) as Credit
from cte as c
WHERE (NewParentID = @TopicID)
)
实际上,我的主题和文档表如下图所示,我只需要从 documentdetail 表中计算我的主题的资产负债表,并将其显示为我的主题(父子)的层次结构!!!!
【问题讨论】:
-
唷 - 巨大的混乱代码!只是一般性建议:在查询中使用执行数据库访问的函数通常对性能非常不利......
-
它真的没什么用——您需要重构代码以不使用该函数,并且还要重构它以不只是在每一列中重复调用该函数。我仍在努力解决这一切。例如,您将文档详细信息转储到 #t1 中,但您不能在函数中使用它。
-
但是我用那个函数计算每个账户的余额,有什么建议吗?? @marc_s
标签: sql-server-2008 query-performance accounting sqlperformance