【发布时间】:2011-08-27 00:06:12
【问题描述】:
我这里有一个存储过程
ALTER PROCEDURE [dbo].[SortedReport]
(
@ClientID INT,
@RecordLimit,
@FromDate DATETIME,
@ToDate DATETIME,
@OrderBy NVARCHAR(MAX)
)
AS
BEGIN
SELECT TOP (@RecordLimit)
sv.ClientID,
sv.VendorID,
sv.ProductID,
sv.TransactionTime,
sv.ClientName,
sv.VendorName,
sv.ProductName,
sv.ProductCode,
sv.VendorCode,
StockCount = dbo.GetStockCount(sv.ProductID, sv.VendorID, @ClientID, @FromDate, @ToDate),
TransactionCount = dbo.GetTransCount(sv.ProductID, sv.VendorID, @FromDate, @ToDate),
ProductCount = dbo.GetProductCount(sv.ProductID, sv.VendorID),
FROM SortedReportiew AS sv
WHERE (sv.ClientID = @ClientID)
AND (sv.TransactionTime >= @FromDate)
AND (sv.TransactionTime < @Date)
当我有大量数据时,SELECT 部分中的函数调用效率非常低。 100 行的函数调用将意味着大量读取。
我需要从那里删除调用并在表值函数中批量读取并将大多数报告参数传递给这些函数
SELECT
sd.StockCount,
sd.TransactionCount
pd.ProductCount,
TransactionTime,
ClientName,
VendorName,
ProductName,
ProductCode,
VendorCode
FROM
(
SELECT TOP (@RecordLimit)
sv.ClientID,
sv.VendorID,
sv.ProductID,
sv.TransactionTime,
sv.ClientName,
sv.VendorName,
sv.ProductName,
sv.ProductCode,
sv.VendorCode
FROM SortedReportiew AS sv
--Begin Where {
WHERE
(ClientID = @ClientID)
AND TransactionTime >= @FromDate
AND TransactionTime < @ToDate
--End Where }
) AS mainQuery
FULL JOIN GetStockDetails(
@ClientID,
@FromDate,
@ToDate,
) AS sd
ON mainQuery.ClientID = sd.ClientID
LEFT OUTER JOIN GetProductDetails(
@ProductID
@FromDate,
@ToDate,
) AS pd
ON mainQuery.ClientID = pd.ClientID
我之前没有尝试过表值函数。到目前为止我写的是
ALTER FUNCTION [dbo].[GetStockDetails]
(
@ClientID INT
@FromDate DATETIME,
@ToDate DATETIME,
)
RETURNS @tblStockDetails TABLE (
-- Columns returned by the function
StockCount INT,
TransactionCount INT
)
AS
BEGIN
INSERT INTO @tblStockDetails
SELECT
StockCount,
TransactionCount
FROM
(
SELECT
StockCount = (SELECT COUNT(*)... // My Query here
....
RETURN
END
有人可以就此要求的表值函数的格式提出建议吗?
【问题讨论】:
-
您是否也打算在其他SP中使用该功能?否则我会使用临时表或声明的表来获得使用主键的能力。如果数据很多,表值函数连接会很慢。
-
@Johan 我如何使用临时表做到这一点?
标签: sql-server tsql stored-procedures user-defined-functions