Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Advanced SQL Interview Questions and Answers on SQL Server Architecture

$
0
0

These SQL interview questions and answers deal with the following topics:

SQL Server High & Low-Level Architecture Transaction Log Architecture Query processing Architecture Memory Architecture New Features added in SQL Server versions 2008, 2008 R2, 2012, 2014, 2016 SQL Server High & Low-Level Architecture 1.Can you explain SQL Server Architecture?

You might be asked this question to know how you strong in SQL internals. You need not draw the diagram, but you should be able to explain the components available and how they work. I have seen this question on interviews at Microsoft (IT and GTSC), Amazon, Barclays, Pythian, CA and E&Y so, let’s understand how components work and you just need to know how communication happens from end client to the database page.

There are total 4 major components in SQL Server Architecture:

Protocol Layer Query Processor/Relational Engine Storage Engine SQLOS

Each instruction must be interacting with these 4 components.

High level SQL Server Architecture can be explained in 4 steps as below:

The Protocol Layer receives the request from the end client and translates it into the form where SQL Server Relational Engine can understand and work on it. The Query Processor accepts the T-SQL batches processes and executes the T-SQL batch. If data is required, the request is passed to Storage Engine The Storage Engine manages the data access and service the requested data. The SQLOS takes responsibility of operating system and manages locks, synchronization, buffer pool, memory, thread scheduling, I/O etc.
Advanced SQL Interview Questions and Answers on SQL Server Architecture
2.Can you explain the SQL Server Protocol Layer?

End clients/Applications should use a protocol to communicate with SQL Server Database Engine. Data is transferred in the form of TDS (Tabular Data Stream) packets over the network. SNI (SQL Server Network Interface) takes the responsibility of encapsulating TDS packets on a standard communication protocol.

SQL Server supports 4 types of network protocols:

Shared Memory:Clients using the shared memory protocol can only connect to a SQL Server instance running on the same computer; it is not useful for most database activity. Use the shared memory protocol for troubleshooting when you suspect the other protocols are configured incorrectly.

Server Machine 1

Clients Machine 1

TCP/IP:TCP/IP is a common protocol widely used over the Internet. It communicates across interconnected networks of computers that have diverse hardware architectures and various operating systems. TCP/IP includes standards for routing network traffic and offers advanced security features. It is the most popular protocol that is used in business today.

Server Machine 1

Clients WAN (Any machine from any network)

Named Pipes:Named Pipes is a protocol developed for local area networks. A part of memory is used by one process to pass information to another process, so that the output of one is the input of the other. The second process can be local (on the same computer as the first) or remote (on a networked computer).

Server Machine 1

Clients LAN (Any machine from LAN)

VIA:Virtual Interface Adapter (VIA) protocol works with VIA hardware. This feature is deprecated from SQL Server 2012.

3.Can you explain phases and components in SQL Server Relational Engine?

We are giving a detailed description for each component and phase. This is only for your understanding. When you are preparing for an interview you just need to quickly review these phases and components.

Query Processor/Relational Engine process a T-SQL batch in the following phases:

Parsing Binding Compiling Optimizing Cache Lookup Normalizing Optimizing Caching Executing

There are various components involved on above phases. They are:

Parser ALGEBRIZER T-SQL Compiler Optimizer SQL Manager Database Manager Query Executer 4.Can you explain each phase in detail? To simplify the question how SQL Server relational Engine works in processing SQL queries?

Here we’ll see how SQL Server Relational engine process a SQL Server query in all phases.

Parsing:It converts high level language to machine understandable language.

Parser:Parser checks for correct syntax, including the correct spelling and use of keywords and builds a parse/sequence tree.

Binding/ALGEBRIZER: It Loads Meta data for tables and columns Checks whether tables and columns exist, Adds information about required conversions to the sequence tree. Replaces references to a view with the definition of that view and performs a few syntaxes based optimizations.

These syntax optimizations include converting right outer joins to equivalent left outer joins, flattening sub-queries to equivalent join syntax, and simplifying some negative expressions etc.

Compiling: The query processor looks for statements for which optimization is not required. These statements Ex. variable declarations and assignments, conditional processing with IF, iteration with WHILE etc. These statements don’t require optimization but needs to be compiled through the T-SQL language compiler. Compilation is only for special T-SQL statements which are not required any optimization.

Optimizing:The primary goal of this phase is to produce a best cost-based execution plan.

Not all queries are sent for optimization. Queries like DDL are compiled into an internal form and queries like DML sent to the optimizer.

Cache Lookup:First it verifies if matching plan is already existing in procedure cache. If exists, then it skips the next process and go to execution phase.

Normalizing:Query is first broken down into simple fine-grained form.

Optimizing:Designs the best cost-based plan based on various parameters.

Here the cost factor is not based on time but on resource (I/O, CPU, Memory etc.) consumption.

Parameters include data volume, type of operations, cardinality, indexes, statistics, configurations, access methods etc.

Caching:Now the optimizer stores the prepared plan to procedure cache. When a user process inserts an execution plan into the cache it sets the current cost equal to the original query compile cost. Thereafter, each time a user process references an execution plan, it resets the current cost to the original compile cost.

When memory pressure exists, the Database Engine responds by removing execution plans with zero cost from the procedure cache. The Database Engine uses the resource monitor and user threads to free memory from the procedure cache

SQL Manager:This component takes care of the execution of stored procedures. It is responsible for deciding as to when a stored procedure needs recompilation and for storing the plan cache for reuse by other stored procedures. It also manages auto parameterization.

Database Manager:The database manager handles access to the metadata needed for query compilation and optimization, making it clear that none of these separate modules can be run completely separately from the others. The metadata is

Viewing all articles
Browse latest Browse all 3160

Trending Articles