简体   繁体   English

SQL子查询性能问题

[英]SQL Sub-Query performance issue

I'm having performance issues with my SQL sub query. 我的SQL子查询遇到性能问题。

As a hard-coded query, it takes about 1 second to run: 作为硬编码查询,运行大约需要1秒钟:

SELECT   ColumnA
        ,ColumnB
        ,ColumnC
FROM    [LinkedServer].[Database].[Schema].[View]
WHERE   ColumnA IN 
    (
        'ABC',
        'DEF',
        'HIJ',
        'KLM'
    )

However, the following code takes over a minute to run: 但是,以下代码需要一分钟的时间才能运行:

SELECT   ColumnA
        ,ColumnB
        ,ColumnC
FROM    [LinkedServer].[Database].[Schema].[View]
WHERE   ColumnA IN 
    (
        SELECT ColumnA FROM #TempTable
    )

The temp table contains the same 4 rows as the hard-coded example. 临时表包含与硬编码示例相同的4行。 The view on the linked server contains approx. 链接服务器上的视图包含大约。 700,000 rows (and, unfortunately, is outside of my control). 700,000行(不幸的是,这超出了我的控制范围)。 The ColumnA data types are the same and both tables are indexed. ColumnA数据类型相同,并且两个表都已建立索引。

Any ideas on how to improve the performance of this query? 关于如何改善此查询性能的任何想法?

Many thanks. 非常感谢。

Try a JOIN instead: 尝试JOIN

SELECT   V.ColumnA
        ,V.ColumnB
        ,V.ColumnC
FROM    [LinkedServer].[Database].[Schema].[View] V
INNER JOIN #TempTable T ON V.ColumnA = T.ColumnA

Its probably related to how the query plan is created. 它可能与查询计划的创建方式有关。 In one case SQL server knows what values it will be using in the comparison, in the second it is estimating. 在一种情况下,SQL Server知道在比较中将使用什么值,在第二种情况下,它正在估计。 Run each query in a seperate Management Studio window after clicking the "Include Actual Excecution Plan." 单击“包括实际执行计划”后,在单独的Management Studio窗口中运行每个查询。 You will probably see different plans. 您可能会看到不同的计划。 The first thing I would check is to hover over the arrows linking the actions (start with the fat ones) and compare the Estimated number of Rows to the Actual number of rows. 我要检查的第一件事是将鼠标悬停在链接动作的箭头上(从粗线开始),然后将“估计的行数”与“实际的行数”进行比较。 A large disparity (factor of 10?) in these values can cause SQL server to make the wrong decision (table scan vs index etc.) If you see this you might consider a hint to get SQL to change its plan - if you need to use the slow query! 这些值之间的较大差异(系数为10?)可能导致SQL Server做出错误的决定(表扫描与索引等)。如果看到此错误,则可以考虑让SQL更改其计划的提示-如果需要使用慢查询! The big problem with hints is that as the data volumes change the hint can easily become a hindrance rather that a benefit, so they are considered to be a last resort. 提示的最大问题是,随着数据量的变化,提示很容易成为障碍而不是好处,因此被认为是不得已的方法。

The linked server supplier has provided me with a different source. 链接服务器供应商为我提供了其他来源。 Instead of connecting to a view (which itself was spread over multiple servers), I now connect to a single table. 现在,我连接到单个表,而不是连接到视图(它本身分布在多个服务器上)。 This, combined with Brian's INNER REMOTE JOIN suggestion returns the full dataset almost immediately. 结合Brian的INNER REMOTE JOIN建议,几乎立即返回完整的数据集。

Whilst it's a little frustrating that I couldn't follow the enhanced privileges/linked server options, at least this query is working well. 尽管我无法遵循增强的特权/链接服务器选项,这令人有些沮丧,但至少此查询运行良好。

Many thanks for everyone's help! 非常感谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM