我的問題是下面的查詢需要38秒完成, 我需要儘可能減少這個時間。 當我查看執行計劃:Dim_Customers索引掃描的%54花費。 任何建議,將不勝感激。由於需要減少查詢時間
DECLARE @SalesPersonCode NVARCHAR(4)
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @SalesPersonCode = 'AC';
SET @StartDate = '03/01/2012';
SET @endDate = '03/31/2012';
SELECT AA_FactSalesOrderDetails.Salesperson
, Dim_SalesOrganisation.[Salesperson name]
, AA_FactSalesOrderDetails.[Order Date]
, Dim_Customers.[Customer number]
, Dim_Customers.[Customer name]
, Dim_Customers.[Area/state]
, Dim_Customers.country
, Dim_Customers.[Customer stop] AS [Customer Block]
, AA_FactSalesOrderDetails.[Customer order stop] AS [Co Stop]
, AA_FactSalesOrderDetails.[First delivery date Header]
, AA_FactSalesOrderDetails.[Last delivery date Header]
, Dim_Customers.[User-defined field 6 - customer]
, Dim_Customers.[Customer group name]
, AA_FactSalesOrderDetails.[Contact Method]
, AA_FactSalesOrderDetails.[Customer order number]
, AA_FactSalesOrderDetails.[Price Level]
, AA_FactSalesOrderDetails.[Item number]
, Dim_Items.[Product group description] AS [Item name]
, AA_FactSalesOrderDetails.[Ordered quantity - basic U/M] AS [Quantity Ordered]
, AA_FactSalesOrderDetails.[Ordered quantity - basic U/M] * AA_FactSalesOrderDetails.[Net price] AS [Order Line Total ]
FROM AA_FactSalesOrderDetails
LEFT JOIN
Dim_SalesOrganisation
ON
AA_FactSalesOrderDetails.Salesperson = Dim_SalesOrganisation.Salesperson
LEFT JOIN
Dim_Customers
ON
AA_FactSalesOrderDetails.Dim_Customers_dKey = Dim_Customers.Dim_Customers_dKey
LEFT JOIN
Dim_Items
ON
AA_FactSalesOrderDetails.[Item number] = Dim_Items.[Item number]
LEFT JOIN
Dim_CustomerOrderTypes
ON
AA_FactSalesOrderDetails.[Customer order type] = Dim_CustomerOrderTypes.[Customer order type]
WHERE AA_FactSalesOrderDetails.[Order Date]
BETWEEN
dbo.fnc_M3_sql_datetime_to_M3_date(@StartDate) /* !!!Procedural Approach!!! */
AND
dbo.fnc_M3_sql_datetime_to_M3_date(@EndDate) /* !!!Procedural Approach!!! */
AND
AA_FactSalesOrderDetails.Salesperson = @SalesPersonCode
'dbo.fnc_M3_sql_datetime_to_M3_date()'做了什麼?如果代碼不是很討厭,你可以發佈它嗎? – Yuck 2012-04-25 15:14:13
除了@Yuck的問題,表中有多少行?此查詢返回多少行?什麼是[Order Date]的數據類型和函數的返回數據類型?在[訂單日期],銷售人員或加入標準中的任何列上是否有索引?你看過執行計劃,看看時間在哪裏嗎? – 2012-04-25 15:16:37
Dim_Customers.Dim_Customers_dKey是否有一個聚集索引覆蓋它?另外,'AA_FactSalesOrderDetails.Dim_Customers_dKey'是否包含一個覆蓋索引爲'Dim_Customers.Dim_Customers_dKey'的外鍵? – 2012-04-25 15:16:39