5853accbde83c 1318223 Sample
5853accbde83c 1318223 Sample
5853accbde83c 1318223 Sample
ISBN 978-1-946390-97-4
This book has been published with all efforts taken to make the material error-free after the
consent of the author. However, the author and the publisher do not assume and hereby disclaim
any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether
such errors or omissions result from negligence, accident, or any other cause.
No part of this book may be used, reproduced in any manner whatsoever without written
permission from the author, except in the case of brief quotations embodied in critical articles
and reviews.
Contents
Acknowledgmentsxv
viii|Contents
In-Memory OLTP 336
TEMPDB342
Miscellaneous349
Contents|ix
CHAPTER 1
Fig 1.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?
Ans:
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 5 phases:
ØØ Parsing
üü Parsing
üü Binding
ØØ Compiling
ØØ Optimizing
üü Cache Lookup
üü Normalizing
üü Optimizing
4. Can you explain each phase in detail? To simplify the question how SQL Server relational
Engine works in processing SQL queries?
Ans:
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,
üü Add information about required conversions to the sequence tree.
üü Replaces references to a view with the definition of that view and performs a few syntax
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.
Caching:
Now 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 stored as data and is managed by the storage engine, but metadata
elements such as the data types of columns and the available indexes on a table must be available
during the query compilation and optimization phase, before actual query execution starts.
Executing:
Query executer component of Relational Engine takes care of this execution. It acts as the
despatcher of the commands mentioned in the execution plan. The query executer works in
association of the Storage engine as each command needs to access the database.
Access Methods:
ØØ The access methods are nothing but programming unit codes and these are called when
server requested for data.
ØØ It requests the data/index pages, creates the OLE DB row-set which can be returned to
the relational engine.
ØØ The access methods contain code to open table, retrieve qualified data, and update data.
ØØ The pages are not retrieved rather the request to the buffer manager is made.
ØØ The manager serves the page in the cache or reads from the disk.
ØØ The access methods include Rows, Pages, Indexes, Versions, and Allocations.
Locking Operations:
ØØ Locking is a crucial function of a multi-user database system such as SQL Server.
ØØ Locking operations make sure that SQ: Server transactions following the configured
ISOLATION level.
ØØ It manages compatibility between the lock types, resolves deadlocks, and escalates locks if
needed. The locking code controls partition, schema, table, page, and row locks as well as
system data locks.
Transaction Services:
ØØ This component makes sure that all transactions are atomic which means they are following
ACID properties.
ØØ In SQL Server, if there is a system failure before the transaction is committed, all the work
is rolled back. Write ahead logging has the ability to roll back work in progress or roll
forward committed work.
ØØ This component coordinates with the current locking code and manages the transactions
accordingly.
Utility Commands:
This is the other component of SQL Server Storage Engine for controlling utilities such as:
ØØ DBCC Commands
ØØ Full-text Index Population and Management
7. When I say compile and execute a SQL Batch does it means compiler compiles each
line of code?
Ans:
Compilation is only for special T-SQL statements which are not required any optimization. Ex:
Variable declaration and assignment etc. We usually say compile a stored procedure. This means
compile and optimize the procedure. Compilation is required for non DML commands and
optimization is required for DML commands.
9. What is a Page?
Ans:
The fundamental unit of data storage in SQL Server is the page.
Page size is 8kb and 128 pages = 1 MB.
Page starts with the header of 96 bytes that is used to store page number, page type, the amount
of free space on the page, and the object id that owns the page. The maximum size of a single
row on a page is 8060KB. But this restriction is relaxed for tables which are having VARCHAR,
NVARCHAR, VARBINARY, TEXT and IMAGE columns.
Disadvantages
ØØ The first is the administration that is involved in keeping track of the files in the
FILEGROUP and the database objects that are placed in them.
ØØ The other is that if you are working with a smaller database and have RAID-5 implemented,
you may not be improving performance.
16. What are the database activities that get benefit from Instant File Initialization?
Ans:
ØØ Creating a new Database
ØØ Restoring a database from backup file
ØØ Increasing database data file size manually
ØØ Increasing database data file size due to Auto Growth option
ØØ TEMPDB creation at the time of SQL Server restart
Note: Remember growing log file still uses the zeroing process
17. How to check if Instant File Initialization is enabled for a SQL Server
Ans:
Enabling trace flags (3004, 3605) enable writing zeroing process information into SQL Server
error log. If Instant File Initialization is enabled for SQL Server we can’t see zeroing process
messages for data file where we can still can see zeroing process messages related to log files
something like “Zeroing Completed On …….._log.LDF.”
19. Have you ever implemented Instant file Initialization in any environment? If yes did you
observe any performance gain?
Ans:
Yes! I have enabled this option in most of the environments I worked and I strongly suggest
enabling it.
I have clearly seen the performance gain by enabling Instant File Initialization. An example:
ØØ Restoring a database from the backup (160 GB) – 3Hr 47Min
ØØ After Enabling Instant File Initialization the same operation took – 2Hr 8 Min
20. Does Instant File Initialization is enabled for SQL Server by default?
Ans:
No! By default IFI is not enabled for SQL Server as there is a slight security risk.
As you may know, when data is deleted from disk by the operating system, it really is not
physically deleted; the space holding the data is just marked as being (space) available. At some
point, the older data will be overwritten with new data.
ØØ When Instant File Initialization is not enabled: Data is zeroed out before writing anything
on that page.
ØØ When Instant File Initialization is enabled: There is a slight security risk here. When a new
database is created those new pages are not zeroed out and there is a chance that newly
allocated pages might contain previously deleted data and one can read that data using a
recovery tool.
23. There is a big change happened in SQL Server Log architecture on SQL Server 2014.
Do you have any idea on that?
Ans:
Yes! VLF creation algorithm got changed from SQL Server 2014 which results into a smaller
number of VLF when compared to the earlier (Before 2014) algorithms.
29. I have a web application and the backend is SQL Server. A user logged into the
application, updated his details and submitted the request. The request includes a set
of select statements and update statements. Now I wanted to know the step by step
procedure for how the request reaches the database and work done. Can you be able
to demonstrate?
Ans:
At Client:
1. User enter data and click on submit
2. The client database library transforms the original request into a sequence of one or more
Transact-SQL statements to be sent to SQL Server. These statements are encapsulated in
one or more Tabular Data Stream (TDS) packets and passed to the database network library.
SNI (SQL Server Network Interface) takes this responsibility.
3. The database network library uses the network library available in the client computer to
repackage the TDS packets as network protocol packets.
4. The network protocol packets are sent to the server computer network library using the
server chosen network protocol (Ex TCP, Named Pipes etc.)
At Relational Engine:
8. Check permissions and determines if the query can be executed by the user associated with
the request
9. Query sends to Query Parser
ØØ It checks that the T-SQL is written correctly
ØØ Build a Parse Tree\Sequence Tree
10. Parse Tree sends to “ALGEBRIZER”
ØØ Verifies all the columns, objects and data types
ØØ Aggregate Binding (determines the location of aggregates such as GROUP BY, and MAX)
ØØ Builds a Query Processor Tree in Binary Format
11. Query Processor Tree sends to Optimizer
ØØ Based on the query processor tree and Histogram (Statistics) builds an optimized execution
plan
ØØ Stores the execution plan into cache and send it to the database engine
At Database Engine:
12. Database engine map a batch into different tasks
13. Each task associated with a process
14. Each process assigned with a Windows Thread or a Windows Fiber. The worker thread takes
care of this.
15. The Thread/Fiber send to the execution queue and wait for the CPU time.
16. The Thread/Fiber identifies the table location where the data need to be stored
17. Go to the file header, checks the PFS, GAM and SGAM and go to the correct page
18. Verifies the page is not corrupted using Torn page Detection/Check SUM
19. If new pages are required then it allocates a new page and it updates the below locations
ØØ PFS
ØØ Page Header – Checksum/Torn Page Detection (Sector info)
ØØ BCM
ØØ DCM
20. It issues the appropriate lock/latch on that page; pick that page from disk (MDF) to memory
(RAM).
21. It applies the required modification on the page in Memory
Memory Architecture
30. How Min and Max server memory options impact memory usage from SQL Server?
Ans:
The min server memory and max server memory configuration options establish upper and lower
limits to the amount of memory used by the buffer pool of the Microsoft SQL Server Database
Engine. The buffer pool starts with only the memory required to initialize. As the Database
Engine workload increases, it keeps acquiring the memory required to support the workload. The
buffer pool does not free any of the acquired memory until it reaches the amount specified in min
server memory. Once min server memory is reached, the buffer pool then uses the standard
algorithm to acquire and free memory as needed. The only difference is that the buffer pool never
drops its memory allocation below the level specified in min server memory, and never acquires
more memory than the level specified in max server memory.