我的问题是理论上的。说以下查询(选项A):
Select *
from orders o
inner join (
select *
from orderDetails
where product = 'shirt'
) od on o.orderId = od.orderId
与以下内容比较(选项B)
Select *
from orders o
inner join orderDetails od on o.orderId = od.orderId
where od.product = 'shirt'
一个相对于另一个有什么技术优势吗?例如,我得到的印象是,由于内部联接发生在已经缩小的行数上,因此选项A对DB的资源需求较少。尽管选项B给出了相同的结果,但是,它似乎在将所有可用的orderId缩小到衬衫之前执行了内部联接。
我对bc的最终影响感到好奇,有时存储过程会变得很大,并且我想确保它们不会不必要地影响报表加载时间。
这两个查询在SQL Server中的评估结果应完全相同-性能相同。这与索引无关。
Why? SQL is a descriptive language. A
SELECT
query describes the result set. It does not specify how the result set is created. In most databases, the work of figuring out what to do is handled by the SQL compiler and optimizer, which produce a directed-acyclic graph (DAG) of operations (some databases also do run-time optimizations). To the newcomer, the operations in the DAG look nothing like the originalSELECT
.并非所有数据库都具有与SQL Server一样聪明的优化器。例如,MySQL有所不同-特别是在旧版本中。 MySQL倾向于实现子查询,这通常会对性能产生不利影响。但是,这是由于优化策略不佳,而不是SQL所致。