好的,我已经浏览了几篇关于SQL运行缓慢的帖子,我没有看到任何类似的内容,所以如果我错过了一个,我很抱歉。一个月前我被问到创建一个视图,允许用户报告尚未结算的内容,并且SQL将4个表连接在一起。一个是120万条记录。其余的在80K到250K之间。 (请注意,这应该只返回where语句后的大约100条记录。)
SELECT C.Cltnum + '.' + C.CltEng AS [ClientNum],
C.CPPLname,
w.WSCDesc,
MIN(w.Wdate) AS [FirstTDate],
w.WCodeCat,
w.WCodeSub,
SUM(w.Wbilled) AS [Billed],
SUM(w.Whours * w.Wrate) AS [Billable Wip],
sum(ar.[ARProgress]) AS [Progress],
w.Winvnum,
-- dbo.GetInvoiceAmount(w.Winvnum) AS [InvoiceAmount],
SUM(cb.cinvar) AS [AR Balance]
FROM dbo.WIP AS w
--Never join on a select statement
--join BillingAuditCatagoriesT bac on w.WCodeCat = bac.Catagory and w.WCodeSub = bac.Subcatagory
INNER JOIN dbo.Clients AS C ON w.WCltID = C.ID
JOIN dbo.ClientBuckets AS cb on c.cltnum = cb.cltnum
JOIN dbo.AcctsRec AS ar on ar.arapplyto = w.[Winvnum]
-- WHERE w.wcodecat = '1AUDT'
GROUP BY C.Cltnum, C.CltEng, C.CPPLname, w.WCodeCat, w.Wdate, w.Winvnum, w.WCodeSub, w.WSCDesc
所以,我认为可能存在的问题是,类别是varchar,它是xat,ACT,BID,并且大约有15种不同的类别。这与SubCat相同。你会注意到这里有3个函数,它们是GetJamesProgress,它是=(SELECT sum(Amount)From Progress where inv = w.invnum)和GetInvoiceAmount和GetJamesARBalance相同。我知道这样做不好,但是当我按发票编号加入时,它需要的时间甚至超过它们。
非常感谢你们!