简体   繁体   English

SQL Server如何处理以下查询?

[英]How does SQL Server process the following query?

I was making some queries to the database called: Northwind that is available to make proves, if you download it from the official site, I performed the following query: 我正在对名为“ Northwind”的数据库进行查询,该数据库可用于证明,如果您从官方网站下载它,则会执行以下查询:

SELECT C.CategoryName NAME,
    P.CATEGORYID CODE, 
    COUNT(*) 'PIECES',
    SUM(UnitPrice*UnitsInStock) 'AMOUNT'
FROM 
    Northwind.dbo.Categories C, Northwind.dbo.[products] P
WHERE
    P.CategoryID = C.CategoryID
AND 
    P.CategoryID>2
GROUP BY
    P.CategoryID, C.CategoryName
HAVING SUM(UnitPrice*UnitsInStock)>10000
ORDER BY 'AMOUNT' DESC;

The query is giving me the right results: 该查询给了我正确的结果:

NAME            CODE        PIECES      AMOUNT
--------------- ----------- ----------- ---------------------
Seafood         8           12          13010.35
Dairy Products  4           10          11271.20
Confections     3           13          10392.20

The fact is that I can't figure out, how does SQL Server compute that query, since many operations are involved, In some point SQL Server has to generate a temporary data base to perform the HAVING : 事实是,由于涉及许多操作,我不知道SQL Server如何计算该查询。在某些情况下,SQL Server必须生成一个临时数据库来执行HAVING

HAVING SUM(UnitPrice*UnitsInStock)>10000

I would like to receive an explanation about the order that takes SQL Server to perform the query, I mean what is the first and last operation that SQL Server perform besides I can't figure out how does it know that has to apply the operation called SUM to the table called: products, 我想收到有关SQL Server执行查询的顺序的解释,我的意思是SQL Server执行的第一个和最后一个操作是什么,除了我不知道如何知道必须应用称为将表的总和称为:产品,

SUM(UnitPrice*UnitsInStock) 'AMOUNT'

I am not specifying that however is computing well the operation, also the COUNT for me is very ambiguous but is giving me the correct result, I really appreciate any detailed explanation of how does it computes this query, thanks for the support. 我并没有说明计算操作是否很好,对我来说COUNT也很模棱两可,但是给了我正确的结果,我非常感谢有关如何计算此查询的详细说明,感谢支持。

You will have to look at logical order processing of a query by Itzik Ben-Gan ,he dedicated a whole chapter and explained in detail each step..The link is here ... 你将不得不看的伊茨克奔甘查询的逻辑顺序处理,他奉献了一整章进行了详细解释每个step..The 链接在这里 ...

Below are the clauses that may execute in logical order and output of each clause is presented to the clause in next phase.. 以下是可以按逻辑顺序执行的子句,每个子句的输出在下一阶段呈现给该子句。

5 SELECT 5.2 DISTINCT 7 TOP <TOP_spec>
                                  5.1 <select_list>
                              1 FROM <table_operators: JOIN, APPLY, PIVOT, UNPIVOT>
                              2 WHERE <predicate>
                              3 GROUP BY <definition_of_grouping_sets>
                              4 HAVING <predicate>
                              6 ORDER BY <order_by_list>
                              7 OFFSET <offset_spec> FETCH <fetch_spec>

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

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