In this article, we will discuss a few very common questions that you may be asked during a SQL Server administrator or developer technical job interview.

在本文中,我们将讨论一些非常常见的问题,在SQL Server管理员或开发人员进行技术面试时可能会询问您。

(Q1: What is a SQL Server Execution Plan?)

SQL Server Execution Plan is a binary representation of the steps that are followed by the SQL Server Engine to execute the query. It also is known as the most efficient roadmap for the query

SQL Server 执行计划是SQL Server引擎执行查询所遵循的步骤的二进制表示。 它也被称为查询的最有效路线图

For more information, check SQL Server Execution Plans overview

有关更多信息,请查看SQL Server执行计划概述。

(Q2: Which component of the SQL Server Engine is responsible for generating an Execution Plan for the submitted query?)

A: The SQL Server Query Optimizer is responsible for creating the most efficient plan to execute the provided query

答:SQL Server查询优化器负责创建最有效的计划以执行提供的查询

For more information, check SQL Server Execution Plans overview

有关更多信息,请查看SQL Server执行计划概述。

(Q3: Where will the generated Execution Plan be stored?)

A: The SQL Server Execution Plan will be stored in the Plan Cache memory storage

答:SQL Server执行计划将存储在计划缓存的存储器中

For more information, check SQL Server Execution Plans overview

有关更多信息,请查看SQL Server执行计划概述。

(Q4: What is the goal behind storing the Execution Plan for the query in the Plan Cache?)

A: The process of generating the most optimal execution plan is an expensive process. Instead of creating a new Execution Plan each time a new query is submitted, the SQL Server Query Optimizer will search in the plan cache storage for an existing Execution Plan for the submitted query and use it. If there is no plan that can be used for that query, the Query Optimizer will create a new plan, taking more time to execute that query. The Execution plans reuse mechanism is very helpful when there are stored procedures executed frequently

答:生成最佳执行计划的过程非常昂贵。 SQL Server查询优化器将在计划缓存存储中搜索已提交查询的现有执行计划,并使用它,而不是每次提交新查询时都创建新的执行计划。 如果没有可用于该查询的计划,则查询优化器将创建一个新计划,这将花费更多时间来执行该查询。 当存储过程频繁执行时,执行计划重用机制非常有用

For more information, check SQL Server Execution Plans overview

有关更多信息,请查看SQL Server执行计划概述。

(Q5: What are the main types of Execution Plans that you can generate for a T-SQL query and what is the difference between these two types?)

A: The Estimated Execution Plan. It is the plan that is generated by parsing the submitted query as an estimate of how the query will be executed, without being executed

答: 预计执行计划 。 它是通过解析提交的查询作为不执行该查询将如何执行的估计而生成的计划

The Actual Execution Plan, that is generated by executing the submitted query, displaying the actual steps that followed while executing the query

通过执行提交的查询生成的实际执行计划 ,显示执行查询时遵循的实际步骤

For more information, check SQL Server Execution Plans types

有关更多信息,请检查SQL Server执行计划类型。

(Q6: What are the three Execution Plan formats?)

A: Graphical Format, Text Format and XML Format

答: 图形格式文本格式和XML格式

For more information, check SQL Server Execution Plans types

有关更多信息,请检查SQL Server执行计划类型。

(Q7: How could the Execution Plan help in identifying the missing index for a specific query?)

A: Based on the available SQL Server statistics and the workload performed on the SQL Server, the SQL Server Query Optimizer will provide us with a suggested index, that may improve the performance of the submitted query in a calculated percentage. So, it will display that index as a recommendation with the query plan in green

答:根据可用SQL Server统计信息和在SQL Server上执行的工作量,SQL Server查询优化器将为我们提供建议的索引,该索引可以按计算出的百分比提高提交的查询的性能。 因此,它将以推荐的形式显示该索引,并以绿色显示查询计划

For more information, check How to Analyze SQL Execution Plan Graphical Components

有关更多信息,请检查如何分析SQL执行计划图形组件

(Q8: What is the correct way of reading the Execution Plan?)

A: The correct way to read the SQL Execution Plan is to start from the right side of the plan to the left side and from the top to the bottom, and the most left SELECT operator contains the final result of the submitted query

答:读取SQL执行计划的正确方法是计划的右侧开始到左侧,从顶部到底部 ,最左边的SELECT运算符包含提交的查询的最终结果。

For more information, check How to Analyze SQL Execution Plan Graphical Components

有关更多信息,请检查如何分析SQL执行计划图形组件

(Q9: How could we use the arrow between the Execution Plan operators to read the plan?)

A: You can make use of the arrows that are connecting the operators in identifying the direction and the amount of the data passed between the Execution Plan operators. In addition, the arrow is an indication of how much data passed between the operators

答:您可以利用连接运算符的箭头来确定执行计划运算符之间传递的数据的方向和数据 。 此外,箭头表示操作员之间传递了多少数据

For more information, check How to Analyze SQL Execution Plan Graphical Components

有关更多信息,请检查如何分析SQL执行计划图形组件

(Q10: Can the Estimated SQL Execution Plan result be trusted?)

A: This depends on the statistics. If it is updated, the results should be the same. You need the Estimated SQL Execution Plan in case the query will take a long time to execute and you need to troubleshoot it

答:这取决于统计数据。 如果更新,结果应相同。 您需要估算SQL执行计划,以防查询需要很长时间来执行并且需要对其进行故障排除

For more information, check How to Analyze SQL Execution Plan Graphical Components

有关更多信息,请检查如何分析SQL执行计划图形组件

(Q11: What is the difference between the RID and the Key Lookup operators?)

A: RID is a row locator that includes information about the location of that record such as the database file, the page, the slot numbers that helps to identify the location of the row quickly

答:RID是一个行定位器,其中包含有关该记录位置的信息,例如数据库文件,页面,插槽号,这些信息有助于快速识别行的位置

The Key Lookup operator is the Clustered equivalent of the RID Lookup operator

键查找操作是RID Lookup操作的集群等同

For more information, check SQL Server Execution Plan Operators – Part 2

有关更多信息,请检查SQL Server执行计划操作员–第2部分

(Q12: What is the Aggregate operator in the Execution Plan?)

A: The Aggregate Operator is mainly used to calculate the aggregate expressions in the submitted query, by grouping the values of an aggregated column. The aggregate expressions include the MIN, MAX, COUNT, AVG, SUM operations

答:聚合运算符主要用于通过对聚合列的值进行分组来计算提交的查询中的聚合表达式。 聚合表达式包括MIN,MAX,COUNT,AVG,SUM操作

For more information, check SQL Server Execution Plan Operators – Part 3

有关更多信息,请检查SQL Server执行计划操作员–第3部分

(Q13: What is the Compute Scalar operator in the Execution Plan?)

A: The Compute Scalar operator is used to perform scalar computation operations in order to calculate a new value from the existing row value

答: Compute Scalar运算符用于执行标量计算操作,以便从现有行值中计算新值

For more information, check SQL Server Execution Plan Operators – Part 3

有关更多信息,请检查SQL Server执行计划操作员–第3部分

(Q14: What is the Concatenation operator in the Execution Plan?)

A: The Concatenation operator takes one or more data sets in sequence as inputs and returns all records from all the input data set. A good example of the concatenation operator is the UNION ALL T-SQL statement

答: 串联运算符按顺序将一个或多个数据集作为输入,并从所有输入数据集中返回所有记录。 连接运算符的一个很好的例子是UNION ALL T-SQL语句

For more information, check SQL Server Execution Plan Operators – Part 3

有关更多信息,请检查SQL Server执行计划操作员–第3部分

(Q15: What is the ASSERT operator in the Execution Plan?)

A: The Assert operator will verify whether the inserted values meet the defined CHECK or FOREIGN KEY constraints on the table called by the query

答: Assert运算符将验证插入的值是否满足查询调用的表上定义的CHECK或FOREIGN KEY约束

For more information, check SQL Server Execution Plan Operators – Part 3

有关更多信息,请检查SQL Server执行计划操作员–第3部分

(Q16: What is the Hash Match operator in the Execution Plan?)

A: Hashing table is used when the SQL Server engine divides the joined tables in the query into equally sized buckets, using a Hashing Function, so that it can access these data in a quick manner. In this case, the SQL Server Optimizer will use the Hash Match operator to perform that action

答:当SQL Server引擎使用哈希 函数将查询中的联接表划分为大小相等的存储桶时,将使用哈希表 以便它可以快速访问这些数据。 在这种情况下,SQL Server Optimizer将使用哈希匹配运算符执行该操作

For more information, check SQL Server Execution Plan Operators – Part 3

有关更多信息,请检查SQL Server执行计划操作员–第3部分

(Q17: What is the Lazy Spool operator in the Execution Plan?)

A: The SQL Server Lazy Spool is used to build a temporary table on the TempDB and fill it in a lazy manner. In other words, it fills the table by reading and storing the data only when individual rows are required by the parent operator

答:SQL Server惰性缓冲池用于在TempDB上构建临时表并以惰性方式填充它。 换句话说,仅当父运算符需要单独的行时,它才通过读取和存储数据来填充表

For more information, check SQL Server Execution Plan Operators – Part 4

有关更多信息,请检查SQL Server执行计划操作员–第4部分

(Q18: What is the Parallelism operator in the Execution Plan?)

A: The parallel plan is used by the SQL Server Engine to execute the expensive queries faster. The SQL Server Engine decides to use a parallel plan to execute the query when the SQL Server is installed on a multi-processor server, the number of threads that are requested are available to be assigned, the value of the Maximum Degree of Parallelism option is not equal to 1 and the cost of the submitted query is larger than the Cost Threshold for Parallelism value. The Parallelism operator is used by the SQL Server Engine to execute the query using a parallel plan

答:SQL Server Engine使用并行计划来更快地执行昂贵的查询。 当SQL Server安装在多处理器服务器上时,SQL Server引擎决定使用并行计划执行查询,可以分配请求的线程数,“ 最大并行度”选项的值为不等于1,并且提交的查询的成本大于“ 并行度的成本阈值”值。 SQL Server引擎使用Parallelism运算符使用并行计划执行查询

For more information, check SQL Server Execution Plan Operators – Part 4

有关更多信息,请检查SQL Server执行计划操作员–第4部分

(Q19: How could we take benefits from the SQL Server Execution Plan in tuning the T-SQL queries performance?)

A: The SQL Server Execution Plan can be used in identifying the bad performance parts of the query. The first thing to look at is the most expensive operator with the highest cost, compared with the overall query cost. In addition, having a fat arrow, which is followed by a thin one, is an indication of the missing index that forced scanning a large amount of data to retrieve a small number of records

答:SQL Server执行计划可用于识别查询的不良性能部分。 首先要考虑的是与整体查询成本相比,成本最高的最昂贵的运营商 。 此外,带有粗线箭头 (后接细线箭头)表示缺少索引,该索引强制扫描大量数据以检索少量记录

Next, you need to search for the extra operators, as its overhead will degrade the query performance. Also, the Scan operators that read the overall table or index is an indication of a missing index, the existing index is badly used, or the submitted query has no filtering condition. The Execution Plan Warnings messages are a sign of different query performance problems that should be checked

接下来,您需要搜索额外的运算符 ,因为它的开销会降低查询性能。 此外,读取整个表或索引的扫描运算符还指示缺少索引,现有索引使用不当或所提交的查询没有过滤条件。 执行计划警告消息是应检查的不同查询性能问题的标志。

For more information, check Using the SQL Execution Plan for Query Performance Tuning

有关更多信息,请选中“ 使用SQL执行计划进行查询性能调整”

(Q20: What is the SQL Server level option that can be used to enhance the Plan Cache usage performance and minimize the memory pressure when the majority of your system workload are ad-hoc queries?)

A: You can enable the Optimize for Ad hoc Workloads option, to store the SQL Execution Plan of the query in the Plan Cache at the second execution of the query

答:您可以启用“ 优化临时工作负载”选项,以在第二次执行查询时将查询SQL执行计划存储在计划缓存中

For more information, check Saving your SQL Execution Plan

有关更多信息,请选中保存您SQL执行计划。

(Q21: How long will the plan be stored in the Plan cache?)

A: It is useless to keep the SQL Server Execution Plan in the Plan cache forever. The SQL Server Engine will automatically drop any plan from the Plan Cache whenever more memory is required by the system or when the plan becomes old and not called for a long time. The SQL Server Engine users the Lazy Writer system process to clean these aged plans

答:将SQL Server执行计划永久保留在计划缓存中是没有用的。 每当系统需要更多内存或计划变旧且长时间未调用时,SQL Server引擎都会自动从计划缓存中删除任何计划。 SQL Server Engine用户使用Lazy Writer系统进程来清理这些老化的计划

For more information, check Saving your SQL Execution Plan

有关更多信息,请选中保存您SQL执行计划。

(Q22: How could we explicitly clear the Plan cache?)

A: Using the DBCC FREEPROCCACHE T-SQL command

答:使用DBCC FREEPROCCACHE T-SQL命令

For more information, check Saving your SQL Execution Plan

有关更多信息,请选中保存您SQL执行计划。

翻译自: https://www.sqlshack.com/sql-server-execution-plans-interview-questions/