tsql 查询分析器:如何降低“成本”?

发布于 2024-07-18 20:01:13 字数 1213 浏览 11 评论 0原文

我正在对以下查询运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的那么颓废 2024-07-25 20:01:13

您正在对 tarInvoice 聚集索引进行完整扫描(触摸每个索引条目),看看是否可以删除函数调用 dbo._fnStripLeadZeros(ti.TranNo) 以便它将使用该索引。

可能将前导零添加到 bl.Invoice_Number 并连接到未更改的 ti.TranNo

EDIT

添加不带前导零的计算列并添加索引:

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

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:

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
只怪假的太真实 2024-07-25 20:01:13

尝试将远程服务器添加为链接服务器

Try adding the remote server as a linked server.

情话已封尘 2024-07-25 20:01:13

仅使用一个本地表,因此您可以将更多查询转移到另一台服务器:

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

不确定确切的语法,只是试图指出可能的改进方向。

There's only one local table being used, so you could shift more of the query to the other server:

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

Not sure about the exact syntax, just trying to point in a possible improvement direction.

弄潮 2024-07-25 20:01:13

我还建议,如果您必须在查询中转换数据,例如:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文