tsql 查询分析器:如何降低“成本”?
我正在对以下查询运行 sql 分析器
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 服务器以执行联接 该图告诉我
remote query cost : 62%
customered index scan [App].[dbo].tarInvoice.[PK__... Cost : 34%
这个查询需要 2 分钟才能运行。 关于如何让这个运行更有效,我有什么想法吗? 我猜这与连接到同一网络上的另一个 SQL 服务器有关。
提前致谢。
I'm running sql analyzer on the following query
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
The query runs on the [App] server and connects to [Conn] sql server to do a join
and the diagram is telling me
remote query cost : 62%
customered index scan [App].[dbo].tarInvoice.[PK__... Cost : 34%
This query is taking 2 mins to run. Any ideas on how would I go about figuring out how to make this run more efficiently? I"m guessing it has to do with connecting to another sql server on the same network.
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在对 tarInvoice 聚集索引进行完整扫描(触摸每个索引条目),看看是否可以删除函数调用 dbo._fnStripLeadZeros(ti.TranNo) 以便它将使用该索引。
可能将前导零添加到 bl.Invoice_Number 并连接到未更改的 ti.TranNo
EDIT
添加不带前导零的计算列并添加索引:
you are doing a complete scan of the tarInvoice clustered index (touch every index entry), see if you can remove the function call dbo._fnStripLeadZeros(ti.TranNo) so it will use the index.
possibly add leading zeros onto bl.Invoice_Number and join to unaltered ti.TranNo
EDIT
add computed column without leading zeros and add an index:
尝试将远程服务器添加为链接服务器。
Try adding the remote server as a linked server.
仅使用一个本地表,因此您可以将更多查询转移到另一台服务器:
不确定确切的语法,只是试图指出可能的改进方向。
There's only one local table being used, so you could shift more of the query to the other server:
Not sure about the exact syntax, just trying to point in a possible improvement direction.
我还建议,如果您必须在查询中转换数据,例如:
CONVERT(十进制(15,2), bl.Invoice_Amount)
那么您需要考虑重构数据库以使用正确的数据类型。
I would also suggest that if you have to convert data in queries such as:
CONVERT(decimal(15,2), bl.Invoice_Amount)
then you need to consider refactoring your database to use the correct datatype.