我在以下查询中运行sql analyzer
SELECT bl.Invoice_Number, bl.Date_Invoice, ti.TranNo, bt.Description,
CONVERT(decimal(15,2), bl.Invoice_Amount) AS Invoice_Amount, co.Company_ID, co.Account_Nbr,
isnull(bl.Reference,' ') as Reference, bl.Billing_Log_RecID AS BillingKey
FROM [CONN.domain.NET].cwwebapp.dbo.Billing_Log bl
LEFT JOIN [App].dob.tarInvoice ti
ON bl.Invoice_Number = dbo._fnStripLeadZeros(ti.TranNo)
INNER JOIN [CONN.domain.NET].cwwebapp.dbo.Billing_Type bt
ON bl.Billing_Type_ID = bt.Billing_Type_ID
LEFT JOIN [CONN.domain.NET].cwwebapp.dbo.Company co
ON bl.Company_RecID = co.Company_RecID
WHERE bl.Date_Invoice >= '2009-05-05'
AND ti.TranNo IS NULL
AND bl.Invoice_Amount <> 0
AND bl.Billing_Type_ID <> 'D'
AND bl.Billing_Type_ID <> 'P'
-- AND bl.Billing_Type_ID <> 'M'
Order By bl.Invoice_Number
查询在[App]服务器上运行,并连接到[Conn] sql server进行连接 并且图表告诉我
remote query cost : 62%
customered index scan [App].[dbo].tarInvoice.[PK__... Cost : 34%
此查询需要2分钟才能运行。关于如何确定如何提高运行效率的任何想法?我猜它与连接到同一网络上的另一个sql server有关。
提前感谢。
答案 0 :(得分:2)
您正在对tarInvoice聚集索引进行完整扫描(触摸每个索引条目),看看是否可以删除函数调用dbo._fnStripLeadZeros(ti.TranNo),以便它将使用索引。
可能会将前导零添加到bl.Invoice_Number并加入到未改变的ti.TranNo
修改强>
添加不带前导零的计算列并添加索引:
ALTER TABLE dbo.tarInvoice ADD TranNoZeroFree AS Convert(int,TranNo) PERSISTED
GO
CREATE NONCLUSTERED INDEX IX_tarInvoice_TranNoZeroFree ON dbo.tarInvoice (TranNoZeroFree) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
答案 1 :(得分:0)
尝试将远程服务器添加为linked server。
答案 2 :(得分:0)
只使用了一个本地表,因此您可以将更多查询转移到另一台服务器:
select *
from openquery([CONN.domain.NET],'
SELECT bl.Invoice_Number, bl.Date_Invoice, ti.TranNo, bt.Description,
CONVERT(decimal(15,2), bl.Invoice_Amount) AS Invoice_Amount,
co.Company_ID, co.Account_Nbr, isnull(bl.Reference,'' '') as Reference,
bl.Billing_Log_RecID AS BillingKey
FROM cwwebapp.dbo.Billing_Log bl
INNER JOIN cwwebapp.dbo.Billing_Type bt
ON bl.Billing_Type_ID = bt.Billing_Type_ID
LEFT JOIN cwwebapp.dbo.Company co
ON bl.Company_RecID = co.Company_RecID
WHERE bl.Date_Invoice >= ''2009-05-05''
AND bl.Invoice_Amount <> 0
AND bl.Billing_Type_ID <> ''D''
AND bl.Billing_Type_ID <> ''P''
') remote
LEFT JOIN tarInvoice ti
ON remote.Invoice_Number = dbo._fnStripLeadZeros(ti.TranNo)
WHERE ti.TranNo IS NULL
Order By remote.Invoice_Number
不确定确切的语法,只是试图指出可能的改进方向。
答案 3 :(得分:0)
我还建议如果你必须在查询中转换数据,例如: CONVERT(十进制(15,2),bl.Invoice_Amount) 那么你需要考虑重构数据库以使用正确的数据类型。