简体   繁体   English

运行此sql命令时sql server上发生了什么

[英]What is happening on sql server when you run this sql command

I have this sql query. 我有这个SQL查询。 I now what is it returning , but can't understand how server parses and execute it step by step. 我现在回归什么,但无法理解服务器如何逐步解析和执行它。 I want to understand the principles of work in deep level, what is going on server when you run sql command, how it iterates in each row before return result. 我想深入了解深层次的工作原理,运行sql命令时服务器上发生了什么,如何在返回结果之前在每一行中进行迭代。

在此输入图像描述

I have users and friends table, in last one have friend_id and user_id combination, both of them are ids of users. 我有usersfriends表,最后一个有friend_iduser_id组合,两者都是用户的ID。

select u.id,f.user_id, f.friend_id from friends f
    INNER JOIN users u on (u.id = f.friend_id or u.id = f.user_id)
    where f.user_id = 72 or f.friend_id = 72
  • How can explain me what is going on in each step of this query? 怎么能解释我在这个查询的每一步中发生了什么?
  • How it is behaves in each step? 它在每一步中的表现如何?
  • How OR operator is behaves in each step here? OR运算符在每个步骤中的行为如何?
  • Does it check both statements of OR operator in each step or query, or eg in first iteration joins with friend_id then with user_id. 它是否在每个步骤或查询中检查OR运算符的两个语句,或者例如在第一次迭代中与friend_id连接,然后与user_id连接。

Without a query plan I can only make a guess how the engine could potentially execute your query, I hope it helps: 如果没有查询计划,我只能猜测引擎如何潜在地执行您的查询,我希望它有所帮助:

First step - FROM: 第一步 - FROM:

If you have an index on the where columns (user_id, user_id), an index seek can be executed on your friends table with the where conditions to limit the results which need to be evaluated later in the execution plan. 如果在where列(user_id,user_id)上有索引,则可以在friends表上执行索引查找,其中where条件用于限制需要在执行计划中稍后评估的结果。 If you don't have an index, a table scan can be performed. 如果您没有索引,则可以执行表扫描。

Your query would select all rows from the friends table where user_id or friend_id is equal to 72. 您的查询将从friend表中选择user_id或friend_id等于72的所有行。

OR means here that only one condition needs to be true for the row to be added to the data stream of rows from friends. OR在这里意味着只有一个条件需要为行添加到朋友的行的数据流。

2nd step - JOIN: 第二步 - 加入:

After the friends rows are found applying the where predicate, the actual join processing would start. 在找到应用where谓词的朋友行之后,将开始实际的连接处理。 Depending on the table statistics and query costs the engine could do a nested loop join where each row is evaluated from friends against users. 根据表统计信息和查询成本,引擎可以执行嵌套循环连接,其中每个行都是针对用户的朋友进行评估的。 It could also build a hash table for one table and probe each row from the other table using the hash key function. 它还可以为一个表构建一个哈希表,并使用哈希键函数从另一个表中探测每一行。 This way the ids can be matched between your two tables (friends and users) 这样可以在两个表(朋友和用户)之间匹配ID

The ON defines which columns it needs to compare. ON定义了需要比较的列。

Let's consider the case of a nested loop join which is easier to understand. 让我们考虑一个更容易理解的嵌套循环连接的情况。 This join type loops through both data streams ( friends and users ) which are being joined together in a row by row fashion and evaluates each row. 这种连接类型循环遍历两个数据流(朋友和用户),这些数据流以逐行方式连接在一起并评估每一行。

In your query it will compare the u.id against friend_id. 在您的查询中,它会将u.id与friend_id进行比较。 If this condition returns true the join will be fulfilled and the engine will combine the matching row from table friends and users for further processing. 如果此条件返回true,则将满足连接,并且引擎将组合来自表朋友和用户的匹配行以进行进一步处理。 If the first condition of ON is false or unknown the engine will evaluate the second condition user_id. 如果ON的第一个条件为假或未知,则引擎将评估第二个条件user_id。 True fulfills the join, false or unknown means the row is not matched so it will not be returned as in this case both on conditions would fail. True表示连接,false或unknown表示行不匹配,因此不会返回,因为在这种情况下,条件都会失败。

3rd step - SELECT: 第3步 - 选择:

After the tables are joined the engine has all the data it needs to finally execute the SELECT statement returning the columns you've asked it for. 表连接后,引擎具有最终执行SELECT语句所需的所有数据,返回您要求的列。

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

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