简体   繁体   English

SQL性能-在联合之前或之后对查询进行过滤

[英]SQL performance - Filtering on query before or after unions

I have a query that looks like this: 我有一个查询,看起来像这样:

SELECT PersonId,
       Note,
       Amount
FROM Table1 A
WHERE A.PersonId = @PersonId

UNION 

SELECT PersonId,
       Note,
       Amount
FROM Table2 B
WHERE B.PersonId = @PersonId

UNION 

SELECT PersonId,
       Note,
       Amount
FROM Table3 C
WHERE C.PersonId = @PersonId

Now, this query will eventually return thousands of results and so I want to build the best performing query possible. 现在,此查询最终将返回数千个结果,因此我想构建性能最佳的查询。 I want to know if that's more efficient than filtering on PersonId after the unions such as this: 我想知道这样是否比在诸如此类的联合之后对PersonId进行过滤更有效:

SELECT * FROM 
(
SELECT PersonId,
       Note,
       Amount
FROM Table1 A

UNION 

SELECT PersonId,
       Note,
       Amount
FROM Table2 B

UNION 

SELECT PersonId,
       Note,
       Amount
FROM Table3 C
) A
WHERE A.PersonId = @PersonId

First at all you will have a query optimizer. 首先,您将拥有一个查询优化器。
There will be no difference in speed, because the PersonId is on the display layer. 速度不会有差异,因为PersonId在显示层上。
The query optimizer will change the execution order of the display parameters so that there will be a minimal amount of parameters. 查询优化器将更改显示参数的执行顺序,以使参数数量最少。
So both queries will be equal at speed. 因此,两个查询的速度将相等。
If you are interested, you can see this execution plan in your SQL developer. 如果您有兴趣,可以在SQL开发人员中查看此执行计划。

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

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