0% found this document useful (0 votes)
96 views23 pages

Performance Tunning

This document contains answers to 11 questions related to SQL Server performance tuning. It discusses topics like the different tools that can be used for performance tuning (including Performance Monitor, Profiler, Dynamic Management Views), how hardware configuration can help or hinder performance, avoiding functions in WHERE clauses, capturing I/O and time statistics for queries, and correlating Performance Monitor and Profiler data. It also provides additional details on gathering I/O statistics at the database file level and how query plans provide value from a performance tuning perspective by breaking down the physical execution of a query.

Uploaded by

Janardhan kengar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views23 pages

Performance Tunning

This document contains answers to 11 questions related to SQL Server performance tuning. It discusses topics like the different tools that can be used for performance tuning (including Performance Monitor, Profiler, Dynamic Management Views), how hardware configuration can help or hinder performance, avoiding functions in WHERE clauses, capturing I/O and time statistics for queries, and correlating Performance Monitor and Profiler data. It also provides additional details on gathering I/O statistics at the database file level and how query plans provide value from a performance tuning perspective by breaking down the physical execution of a query.

Uploaded by

Janardhan kengar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

1. Question 1. Reasons Of Poor Performance Of Query?

Answer :
o No indexes
o Excess recompilations of stored procedures.
o Procedures and triggers without SET NOCOUNT ON.
o Poorly written query with unnecessarily complicated joins
o Highly normalized database design.
o Excess usage of cursors and temporary tables.
2. Question 2. Tell Me In Brief The Cursor Optimization Tips?
Answer :
The following are few tips for cursor optimization:
o When the cursor is not needed, close the cursor
o Deallocate the cursor after closing it.
o Fetch less number of rows.
o Avoid triggers – because trigger executes whenever data gets
updated, leads to overload of the system.
o When the rows are not need to update, use the option FORWARD
ONLY
o Use where instead of having clause unless it is essential
3. Question 3. How Is Index Tuning Used To Improve Query Performance?
Answer :
The Index tuning wizard can be used to improve the performance of
queries and databases. It uses the following measures to do so:
o It uses the query optimizer to perform the analysis of queries with
respect to the workload and based on this knowledge, it recommends
the best usage of indexes.
o The changes in the usage of index, query distribution and their
performance are analysed for checking the effect.
o It also recommends ways of tuning the database for a small set of
problem queries.
4. Question 4. Tell Me What Is Index Tuning?
Answer :
Index tuning is part of database tuning for selecting and creating indexes. The
index tuning goal is to reduce the query processing time. Potential use of
indexes in dynamic environments with several ad-hoc queries in advance is a
difficult task. Index tuning involves the queries based on indexes and the
indexes are created automatically on-the-fly. No explicit actions are needed by
the database users for index tuning.
5. Question 5. What Are The Ways To Code Efficient Transactions?
Answer :
o We shouldn't allow input from users during a transaction.
o We shouldn't open transactions while browsing through data.
o We should keep the transaction as short as possible.
o We should try to use lower transaction isolation levels.
o We should access the least amount of data possible while in a
transaction.
6. Question 6. Do You Know What Are The Ways To Code Efficient
Transactions?
Answer :
o We shouldn't allow input from users during a transaction.
o We shouldn't open transactions while browsing through data.
o We should keep the transaction as short as possible.
o We should try to use lower transaction isolation levels.
o We should access the least amount of data possible while in a
transaction.
7. Question 7. Explain Some Disadvantages Of The Indexes?
Answer :
Indexes improve query performance but it slows down data modification
operations.
Indexes consume disk space.
8. Question 8. Explain Execution Plan?
Answer :
o SQL Server caches the plan of execution of query or stored
procedure which it uses in subsequent call.
o This is a very important feature with regards to performance
enhancement.
o You can view execution plan of data retrieval graphically or
textually.
9. Question 9. Explain In Brief The Cursor Optimization Tips?
Answer :
o Close cursor when it is not required.
o You shouldn’t forget to deallocate cursor after closing it.
o You should fetch least number of records.
o You should use FORWARD ONLY option when there is no need to
update rows.
10.Question 10. Tell Me What Are The Types Of Indexes?
Answer :
o Indexes can be clustered and non-clustered indexes.
o Clustered index keeps all the records in the database in the order
of clustered index key.
o There can be only one clustered index per table.
o Non-clustered indexes are stored as B-tree structure in their own
storage separate from data storage in the table.
11.Question 11. What Are Indexes?
Answer :
o Index can be thought as index of the book that is used for fast
retrieval of information.
o Index uses one or more column index keys and pointers to the
record to locate record.
o Index is used to speed up query performance.
o Both exist as B-tree structure.
o Kind of the indexes are clustered and non-clustered.

Problem

In the latest installment of the SQL Server interview questions, we will outline
questions suitable for a DBA or Developer interview to assess the candidates
skills related to SQL Server performance tuning. Good luck!

Solution

Question Difficulty = Easy

 Question 1: Name five different tools which can be used for performance
tuning and their associated purpose.
o Performance Monitor\System Monitor - Tool to capture macro level
performance metrics.
  Additional information
 Automate Performance Monitor Statistics Collection
for SQL Server and Windows
 Windows Reliability and Performance Monitor to
troubleshoot SQL Server
o Profiler - Tool to capture micro level performance metrics based on
the statements issued by a login, against a database or from host
name.
 Additional information: Tip Category - Profiler and Trace
o Server Side Trace - System objects to write the detailed statement
metrics to a table or file, similar to Profiler.
 Additional information: SQL Server Performance Statistics
Using a Server Side Trace
o Dynamic Management Views and Functions - SQL Server objects
with low level metrics to provide insight into a specific portion of
SQL Server i.e. the database engine, query plans, Service Broker, etc.
 Additional information: Dynamic Management Views\
Functions
o Management Studio's Built-In Performance Reports - Ability to
capture point in time metrics as pre-defined by Microsoft.
 Additional information: Built-In Performance Reports in SQL
Server 2005
o Custom scripts - Custom scripts can be developed to monitor
performance, determine IO usage, monitor fragmentation, etc. all in
an effort to improve performance.
o Third party applications - Performance monitoring and tuning
applications from vendors in the SQL Server community.
 Additional information: SQL Server Performance Monitoring
Tools

 Question 2: Explain how the hardware running SQL Server can help or
hinder performance.
o Taxed CPUs will queue requests and hinder query performance.
o Insufficient memory could cause paging resulting in slow query
performance.
o Incorrectly configured disk drives could exacerbate IO problems.
o Additional information: Hard Drive Configurations for SQL
Server and Hardware 101 for DBAs

 Question 3: Why is it important to avoid functions in the WHERE clause?


o Because SQL Server will scan the index or the table as opposed to
seeking the data.  The scan operation is a much more costly
operation than a seek.
o Often a slightly different approach can be used to prevent using the
function in the WHERE clause yielding a favorable query plan and
high performance.
o Additional information: Performance Tip: Avoid functions in
WHERE clause

 Question 4: How is it possible to capture the IO and time statistics for


your queries?
o Use the SET STATISTICS IO and SET STATISTICS TIME settings in
your queries or enable the settings in your Management Studio
session.
o Additional information: Getting IO and time stats for your queries

 Question 5: True or False - It is possible to correlate the Performance


Monitor metrics with Profiler data in a single SQL Server native product?
o True - This functionality is possible with SQL Server Profiler.
o Additional information: Correlating Performance Monitor and Trace
Data

Question Difficulty = Moderate


 Question 1: How can I/O statistics be gathered and reviewed for
individual database files?
o By using the fn_virtualfilestats function to capture the metrics.
o This process can be automated with a script to determine the file
usage with numerous samples.
o Additional Information: Gathering I/O statistics down to the
database file level

 Question 2: What is a query plan and what is the value from a


performance tuning perspective?
o A query plan is the physical break down of the code being passed to
the SQL Server optimizer.
o The value from a performance tuning perspective is that each
component of the query can be understood and the percentage of
resource utilization can be determined at a micro level.  As query
tuning is being conducted, the detailed metrics can be reviewed to
compare the individual coding techniques to determine the best
alternative.
o Additional Information:
 Maximizing your view into SQL Query Plans
 Capturing Graphical Query Plans with Profiler
 Tip Category: Query Plans
 Tip Category: Query Optimization

 Question 3: True or False - It is always beneficial to configure TempDB


with an equal number of fixed sized files as the number of CPU cores.
o False - With always being the operative word in the question.
o Depending on the version of SQL Server, the disk subsystem, load,
queries, etc., a 1 to 1 ratio of files to cores may be necessary on high
end SQL Servers with intense processing.
o If you do not have that luxury, a starting point may to be have half
the number of tempdb files as compared to CPU cores.
o This is a configuration to load test and monitor closely depending
on the type of processing, load, hardware, etc. that your SQL Server
is expected to support.

 Question 4: Explain the NOLOCK optimizer hint and some pros\cons of


using the hint.
o The NOLOCK query hint allows SQL Server to ignore the normal
locks that are placed and held for a transaction allowing the query
to complete without having to wait for the first transaction to finish
and therefore release the locks. 
o This is one short term fix to help prevent locking, blocking or
deadlocks.
o However, when the NOLOCK hint is used, dirty data is read which
can compromise the results returned to the user.
o Additional information: Getting rid of some blocking issues with
NOLOCK

 Question 5: Explain three different approaches to capture a query plan.


o SHOWPLAN_TEXT
o SHOWPLAN_ALL
o Graphical Query Plan
o sys.dm_exec_query_optimizer_info
o sys.dm_exec_query_plan
o sys.dm_exec_query_stats

Question Difficulty = Advanced

 Question 1: True or False - A LEFT OUTER JOIN is always faster than a


NOT EXISTS statement.
o False - With always being the operative word.  Depending on the
situation the OUTER JOIN may or may not be faster than a NOT
EXISTS statement.  It is necessary to test the techniques, review the
query plans and tune the queries accordingly.

 Question 2: Name three different options to capture the input (code) for a
query in SQL Server.
o DBCC INPUTBUFFER
o fn_get_sql
o sys.dm_exec_sql_text
o Additional information:
 SQL Server statements currently running with fn_get_sql
 SQL Server 2000 to 2005 Crosswalk - Code Identification

 Question 3: Explain why the NORECOMPUTE option of UPDATE


STATISTICS is used.
o This command is used on a per table basis to prevent the table from
having statistics automatically updated based on the 'Auto Update
Statistics' database configuration.
o Taking this step will prevent UPDATE STATISTICS from running
during an unexpected time of the day and cause performance
problems.
o By setting this configuration it is necessary to manually UPDATE
STATISTICS on a regular basis.
o Additional information: The NORECOMPUTE option of UPDATE
STATISTICS

 Question 4: Explain a SQL Server deadlock, how a deadlock can be


identified, how it is a performance problem and some techniques to
correct deadlocks.
o A deadlock is a situation where 2 spids have data locked and cannot
release their lock until the opposing spid releases their lock.
Depending on the severity of the deadlock, meaning the amount of
data that is locked and the number of spids that are trying to access
the same data, an entire chain of spids can have locks and cause a
number of deadlocks, resulting in a performance issue.
o Deadlocks can be identified by Profiler in either textual, graphical or
XML format.
 Additional information: Capturing Deadlock Information in
XML Format and How To: Graphical Deadlock Chain
o Deadlocks are a performance problem because they can prevent 2
or more processes from being able to process data.  A deadlock
chain can occur and impact hundreds of spids based on the data
access patterns, number of users, object dependencies, etc.
o Deadlocks could require a database design change, T-SQL coding
change to access the objects in the same order, separating reporting
and OLTP applications, including NOLOCK statements in SELECT
queries that can accept dirty data, etc.

 Question 5: Please explain why SQL Server does not select the same query
plan every time for the same code (with different parameters) and how
SQL Server can be forced to use a specific query plan.
o The query plan is chosen based on the parameters and code being
issued to the SQL Server optimizer.  Unfortunately, a slightly
different query plan can cause the query to execute much longer
and use more resources than another query with exactly the same
code and only parameter differences.
o The OPTIMIZE FOR hint can be used to specify what parameter
value we want SQL Server to use when creating the execution plan.
This is a SQL Server 2005 and beyond hint.
Q1: What is the difference between a Heap table and a Clustered table? How can
we identify if the table is a heap table?
A Heap table is a table in which, the data rows are not stored in any particular
order within each data page. In addition, there is no particular order to control
the data page sequence, that is not linked in a linked list. This is due to the fact
that the heap table contains no clustered index.
A clustered table is a table that has a predefined clustered index on one column
or multiple columns of the table that defines the storing order of the rows within
the data pages and the order of the pages within the table, based on the clustered
index key.
The heap table can be identified by querying the sys.partitions system object
that has one row per each partition with index_id value equal to 0. You can also
query the sys.indexes system object also to show the heap table index details,
that shows, the id of that index is 0 and the type of it is HEAP.
For more information, see the article: SQL Server table structure overview.
Q2: Explain how the SQL Server Engine uses an Index Allocation Map (IAM)?
SQL Server Engine uses an Index Allocation Map (IAM) to keep an entry for
each page to track the allocation of these available pages. The IAM is considered
as the only logical connection between the data pages, that the SQL Server Engine
will use to move through the heap. 
For more information, see the article: SQL Server table structure overview.
Q3: What is the “Forwarding Pointers issue” and how can we fix it?
When a data modification operation is performed on heap table data
pages, Forwarding Pointers will be inserted into the heap to point to the new
location of the moved data. These forwarding pointers will cause performance
issues over time due to visiting the old/original location vs the new location
specified by the forwarding pointers to get a specific value.

Starting from SQL Server version 2008, a new method was introduced to
overcome the forwarding pointers performance issue, by using the ALTER
TABLE REBUILD command, that will rebuild the heap table.

For more information, see the article: SQL Server table structure overview.
Q4: What is a SQL Server Index?

A SQL Server index is considered as one of the most important factors in the
performance tuning process. Indexes are created to speed up the data retrieval
and the query processing operations from a database table or view, by providing
swift access to the database table rows, without the need to scan all the table’s
data, in order to retrieve the requested data.

You can imagine a table index akin to a book’s index that allows you to find the
requested information very fast within your book, rather than reading all the
book pages in order to find a specific item you are looking for.

For more information, see the article: SQL Server index structure and concepts.
Q5: Describe the structure of a SQL Server Index that provides faster access to
the table’s data?

A SQL Server index is created using the shape of B-Tree structure, that is made
up of 8K pages, with each page, in that structure, called an index node. The B-
Tree structure provides the SQL Server Engine with a fast way to move through
the table rows based on index key, that decides to navigate left or right, to
retrieve the requested values directly, without scanning all the underlying table
rows. You can imagine the potential performance degradation that may occur
due to scanning large database table.

The B-Tree structure of the index consists of three main levels:

 the Root Level, the top node that contains a single index page, form which
SQL Server starts its data search,
 the Leaf Level, the bottom level of nodes that contains the data pages we
are looking for, with the number of leaf pages depends on the amount of
data stored in the index,
 and finally the Intermediate Level, one or multiple levels between the
root and the leaf levels that holds the index key values and pointers to the
next intermediate level pages or the leaf data pages. The number of
intermediate levels depends on the amount of data stored in the index.

For more information, see the article: SQL Server index structure and concepts.
Q6: Explain Index Depth, Density and Selectivity factors and how these factors
affect index performance?

 Index depth is the number of levels from the index root node to the leaf
nodes. An index that is quite deep will suffer from performance
degradation problem. In contrast, an index with a large number of nodes in
each level can produce a very flat index structure. An index with only 3 to
4 levels is very common.
 Index density is a measure of the lack of uniqueness of the data in a table.
A dense column is one that has a high number of duplicates.
 Index selectivity is a measure of how many rows scanned compared to
the total number of rows. An index with high selectivity means a small
number of rows scanned when related to the total number of rows.

For more information, see the article: SQL Server index structure and concepts.
Q7: What is the difference between OLTP and OLAP workloads and how do they
affect index creation decisions?
On Online Transaction Processing (OLTP) databases, workloads are used for
transactional systems, in which most of the submitted queries are data
modification queries.
In contrast, Online Analytical Processing (OLAP) database workloads are used
for data warehousing systems, in which most of the submitted queries are data
retrieval queries that filter, group, aggregate and join large data sets quickly.

Creating a large number of indexes on a database table affects data modification


(e.g. Updates) operation performance. When you add or modify a row in the
underlying table, the row will also be adjusted appropriately in all related table
indexes. Because of that, you need to avoid creating a large number of indexes on
the heavily modified tables and create the minimum possible number of indexes,
with the least possible number of columns on each index. For Online Analytical
Processing (OLAP) workloads, in which tables have low modification
requirements, you can create a large number of indexes that improve the
performance of the data retrieval operations

For more information, see the article: SQL Server index design basics and
guidelines.
Q8: Why it is not recommended to create indexes on small tables?

It takes the SQL Server Engine less time scanning the underlying table than
traversing the index when searching for specific data. In this case, the index will
not be used but it will still negatively affect the performance of data modification
operations, as it will be always adjusted when modifying the underlying table’s
data.
For more information, see the article: SQL Server index design basics and
guidelines.
Q9: What are some different ways to create an index?

 CREATE INDEX T-SQL statement.  


 Using SQL Server Management Studio, by browsing the table on which you
need to create an index, right click on the Indexes node and choose New
Index option.
 Indirectly by defining the PRIMARY KEY and the UNIQUE constraint within
the CREATE TABLE or ALTER TABLE statements. 

For more information, see the article: SQL Server index operations.


Q10: What are the pros and cons of using ONLINE index creation or rebuilding
options?

Setting the ONLINE option to ON when you create or rebuild the index will
enable other data retrieving or modification processes on the underlying table to
continue, preventing the index creation process from locking the table. On the
other hand, the ONLINE index creation or rebuilding process will take longer
time than the offline default index creation process.

For more information, see the article: SQL Server index operations.


Q11: What is the difference between PAD_INDEX and FILLFACTOR?

 FILLFACTOR isused to set the percentage of free space that the SQL Server
Engine will leave in the leaf level of each index page during index creation.
The FillFactor should be an integer value from 0 to 100, with 0 or 100 is
the default value, in which the pages will be filled completely during the
index creation.
 PAD_INDEX is used to apply the free space percentage specified
by FillFactor to the index intermediate level pages during index creation.

For more information, see the article: SQL Server index operations.


Q12: How many Clustered indexes can be created on a table and why?
SQL Server allows us to create only one Clustered index per each table, as the
data can be sorted in the table using only one order criteria. 
For more information, see the article: Designing effective SQL Server clustered
indexes.
Q13: Describe the characteristics ideal Clustered index keys.

 Short: Although SQL Server allows us to add up to 16 columns to the


clustered index key, with a maximum key size of 900 bytes, the typical
clustered index key is much smaller than what is allowed, with as few
columns as possible. A wide Clustered index key will also affect all non-
clustered indexes built over that clustered index, as the clustered index
key will be used as a lookup key for all the non-clustered indexes pointing
to it.
 Static: It is recommended to choose columns that are not changed
frequently in a clustered index key. Changing the clustered index key
values means that the whole row will be moved to the new proper page to
keep the data values in the correct order.
 Increasing: Using an increasing (aka incrementing) column, such as the
IDENTITY column, as a clustered index key will help in improving the
INSERT process, that will directly insert the new values at the logical end
of the table. This highly recommended choice will help in reducing the
amount of memory required for the page buffers, minimize the need to
split the page into two pages to fit the newly inserted values and the
fragmentation occurrence, that required rebuilding or reorganizing the
index again.
 Unique: It is recommended to declare the clustered index key column or
combination of columns as unique to improve query performance.
Otherwise, SQL Server will automatically add a uniqueifier column to
enforce the clustered index key uniqueness.
 Accessed frequently: This is due to the fact that the rows will be stored in
the clustered index in a sorted order based on that index key that is used
to access the data.
 Used in the ORDER BY clause: In this case, there no need for the SQL
Server Engine to sort the data in order to display it, as the rows are already
sorted based on the index key used in the ORDER BY clause.

For more information, see the article: Designing effective SQL Server clustered
indexes.
Q14: Why it is not recommended to use GUID and CHARACTER columns as
Clustered index keys?

For GUID columns, that are stored in UNIQUE IDENTIFIER columns, the main
challenge that affects the clustered index key sorting performance is the nature
of the GUID value that is larger than the integer data types, with 16 bytes size,
and that it is generated in random manner, different from the IDENTITY integer
values that are increasing continuously. 

For the CHARACTER columns. The main challenges include limited sorting
performance of the character data types, the large size, non-increasing values,
non-static values that often tend to change in the business applications and not
compared as binary values during the sorting process, as the characters
comparison mechanism depends on the used collation. 
For more information, see the article: Designing effective SQL Server clustered
indexes.
Q15: What is the main difference between a Clustered and Non-Clustered index
structure?

A Non-clustered index is different from a Clustered index in that, the underlying


table rows will not be stored and sorted based on the Non-clustered key, and the
leaf level nodes of the Non-clustered index are made of index pages instead of
data pages. The index pages of the Non-clustered index contain Non-clustered
index key values with pointers to the storage location of these rows in the
underlying heap table or the Clustered index.

For more information, see the article: Designing effective SQL Server non-
clustered indexes.
Q16: What is the main difference between a Non-clustered index that is built over
a Heap table and a Non-clustered index that is built over a Clustered table? What
is the difference between a RID Lookup and a Key Lookup?
If a Non-Clustered index is built over a Heap table or view (read more about SQL
Server indexed views, that have no Clustered indexes) the leaf level nodes of that
index hold the index key values and Row ID (RID) pointers to the location of the
rows in the heap table. The RID consists of the file identifier, the data page
number, and the number of rows on that data page.
On the other hand, if a Non-clustered index is created over a Clustered table, the
leaf level nodes of that index contain Non-clustered index key values and
clustering keys for the base table, that are the locations of the rows in the
Clustered index data pages. 
A RID Lookup operation is performed to retrieve the rest of columns that are not
available in the index from the heap table based on the ID of each row.
A Key Lookup operation is performed to retrieve the rest of columns that are not
available in the index from the Clustered index, based on the Clustered key of
each row, 
For more information, see the article: Designing effective SQL Server non-
clustered indexes.
Q17: How could we benefit from the INCLUDE feature to overcome Non-
Clustered index limitations?
Rather than creating a Non-clustered index with a wide key, large columns that
are used to cover the query can be included to the Non-clustered index as non-
key columns, up to 1023 non-key columns, using the INCLUDE clause of the
CREATE INDEX T-SQL statement, that was introduced in SQL Server 2005, with a
minimum of one key column.
The INCLUDE feature extends the functionality of Non-clustered indexes, by
allowing us to cover more queries by adding the columns as non-key columns to
be stored and sorted only in the leaf level of the index, without considering that
columns values in the root and intermediate levels of the Non-clustered index. In
this case, the SQL Server Query Optimizer will locate all required columns from
that index, without the need for any extra lookups. Using the included columns
can help to avoid exceeding the Non-clustered size limit of 900 bytes and 16
columns in the index key, as the SQL Server Database Engine will not consider
the columns in the Non-clustered index non-key when calculating the size and
number of columns of the index key. In addition, SQL Server allows us to include
the columns with data types that are not allowed in the index key, such as
VARCHAR(MAX), NVARCHAR(MAX), text, ntext and image, as Non-clustered
index non-key columns.
For more information, review SQL Server non-clustered indexes with included
columns.
Q18: Which type of indexes are used to maintain the data integrity of the
columns on which it is created?
Unique Indexes, by ensuring that there are no duplicate values in the index key,
and the table rows, on which that index is created.
For more information, see the article: Working with different SQL Server indexes
types.
Q19: How could we benefits from a Filtered index in improving the performance
of queries?

It uses a filter predicate to improve the performance of queries that retrieve a


well-defined subset of rows from the table, by indexing the only portion of the
table rows. The smaller size of the Filtered index, that consumes a small amount
of the disk space compared with the full-table index size, and the more accurate
filtered statistics, that cover the filtered index rows with only minimal
maintenance cost, help in improving the performance of the queries by
generating a more optimal execution plan.

For more information, see the article: Working with different SQL Server indexes
types.
Q20: What are the different ways that can be used to retrieve the properties of
the columns participating in a SQL Server index?

 Using SSMS, by expanding the Indexes node under a database tabl, then


right-clicking on each index, and choose the Properties option. The
problem with gathering the indexes information using the UI method is
that you need to browse it one index at a time per each table. You can
imagine the effort required to see the article: all indexes in a specific
database. 
  The sp_helpindex system stored procedure, by providing the name of the
table that you need to list its indexes. In order to gather information about
all indexes in a specific database, you need to execute the sp_helpindex
number of time equal to the number of tables in your database.
 The sys.indexes system dynamic management view. The sys.indexes
contains one row per each index in the table or view. It is recommended to
join sys.indexes DMV with other systems DMVs, such as the
sys.index_columns, sys.columns and sys.tables in order to return
meaningful information about these indexes. 

For more information, see the article: Gathering SQL Server indexes statistics and
usage information.
Q21: How can we get the fragmentation percentage of a database index?

 Using SSMS, from the Fragmentation tab of the


index Properties window. Checking the fragmentation percentage of all
indexes in a specific database, using the UI method requires a big effort, as
you need to check one index at a time.
 The sys.dm_db_index_physical_stats dynamic management function, that
was first Introduced in SQL Server 2005. The
sys.dm_db_index_physical_stats DMF can be joined with the sys.indexes
DMV to return the fragmentation percentage of all indexes under the
specified database.

For more information, see the article: Gathering SQL Server indexes statistics and
usage information.
Q22: When checking the index usage statistics information, retrieved by querying
the sys.dm_db_index_usage_stats dynamic management view, explain the results
of the returned number of seeks, scans, lookups and updates.

 The number of Seeks indicates the number of times the index is used to


find a specific row,
 the number of Scans shows the number of times the leaf pages of the index
are scanned,
 the number of Lookups indicates the number of times a Clustered index is
used by the Non-clustered index to fetch the full row
 and the number of Updates shows the number of times the index data has
modified. 

For more information, see the article: Gathering SQL Server indexes statistics and
usage information.
Q23: What is the difference between index Rebuild and Index Reorganize
operations?
Index fragmentation can be resolved by rebuilding and reorganizing SQL Server
indexes regularly. The Index Rebuild operation removes fragmentation by
dropping the index and creating it again, defragmenting all index levels,
compacting the index pages using the Fill Factor values specified in rebuild
command, or using the existing value if not specified and updating the index
statistics using FULLSCAN of all the data. 
The Index Reorganize operation physically reorders leaf level pages of the index
to match the logical order of the leaf nodes. The index reorganizes operation will
be always performed online. Microsoft recommends fixing index fragmentation
issues by rebuilding the index if the fragmentation percentage of the
index exceeds 30%, where it recommends fixing the index fragmentation issue
by reorganizing the index if the index fragmentation percentage exceeds
5% and less than 30%. 
For more information, see the article: Maintaining SQL Server indexes.
Q24: How can you find the missing indexes that are needed to potentially
improve the performance of your queries?

 The Missing Index Details option in the query execution plan, if available.


 The sys.dm_db_missing_index_details dynamic management view, that
returns detailed information about missing indexes, excluding spatial
indexes,
 A combination of the SQL Server Profiler and the Database Engine
Tuning Advisor tools.

For more information, see the article: Tracing and tuning queries using SQL
Server indexes.
Q25: Why is an index described as a double-edged sword?

A well-designed index will enhance the performance of your system and speed
up the data retrieval process. On the other hand, a badly-designed index will
cause performance degradation on your system and will cost you extra disk
space and delay in the data insertion and modification operations. It is better
always to test the performance of the system before and after adding the index to
the development environment, before adding it to the production environment.

MS SQL Server Database Interview Question and Answer

1. What are the types of indexes available with SQL Server?

There are basically two types of indexes that we use with the SQL Server.
Clustered and the Non-Clustered.

2. When do we use the UPDATE_STATISTICS command?

This command is basically used when we do a large processing of data. If we do a


large amount of deletions any modification or Bulk Copy into the tables, we need
to basically update the indexes to take these changes into account.
UPDATE_STATISTICS updates the indexes on these tables accordingly.

3. Which TCP/IP port does SQL Server run on?

SQL Server runs on port 1433, however it can be change better security purpose.

4. From where can you change the default port?

From the Network Utility TCP/IP properties –> Port number.both on client and
the server.

5. What is the use of DBCC commands?

DBCC stands for database consistency checker. We use these commands to check
the consistency of the databases, i.e., maintenance, validation task and status
checks.

6. Can you give me some DBCC command options?

(Database consistency check) - DBCC CHECKDB - Ensures that tables in the db


and the indexes are correctly linked.and DBCC CHECKALLOC - To check that all
pages in a db are correctly allocated. DBCC SQLPERF - It gives report on current
usage of transaction log in percentage. DBCC CHECKFILEGROUP - Checks all
tables file group for any damage.

7. What command do we use to rename a db?

sp_renamedb ‘oldname’ , ‘newname’

8. Well sometimes sp_reanmedb may not work you know because if some
one is using the db it will not accept this command so what do you think
you can do in such cases?

8. Well sometimes sp_reanmedb may not work you know because if some one is
using the db it will not accept this command so what do you think you can do in
such cases?

9. What is a Join in SQL Server?

Join actually puts data from two or more tables into a single result set.

10. Can you explain the types of Joins that we can have with Sql Server?

There are three types of joins: Inner Join, Outer Join, Cross Join

11. When do you use SQL Profiler?


SQL Profiler utility allows us to basically track connections to the SQL Server and
also determine activities such as which SQL Scripts are running, failed jobs etc..

12. What is a Linked Server?

Linked Servers is a concept in SQL Server by which we can add other SQL Server
to a Group and query both the SQL Server dbs using T-SQL Statements.

13. Can you link only other SQL Servers or any database servers such as
Oracle?

We can link any server provided we have the OLE-DB provider from Microsoft to
allow a link. For Oracle we have a OLE-DB provider for oracle that microsoft
provides to add it as a linked server to the sql server group.

14. Which stored procedure will you be running to add a linked server?

sp_addlinkedserver, sp_addlinkedsrvlogin

15. What are the OS services that the SQL Server installation adds?

MS SQL SERVER SERVICE, SQL AGENT SERVICE, DTC (Distribution transac co-
ordinator)

16. Can you explain the role of each service?

SQL SERVER - is for running the databases


SQL AGENT - is for automation such as Jobs, DB Maintanance, Backups
DTC - Is for linking and connecting to other SQL Servers

17. How do you troubleshoot SQL Server if its running very slow?

First check the processor and memory usage to see that processor is not above
80% utilization and memory not above 40-45% utilization then check the disk
utilization using Performance Monitor, Secondly, use SQL Profiler to check for the
users and current SQL activities and jobs running which might be a problem.
Third would be to run UPDATE_STATISTICS command to update the indexes

18. Lets say due to N/W or Security issues client is not able to connect to
server or vice versa. How do you troubleshoot?

First I will look to ensure that port settings are proper on server and client
Network utility for connections. ODBC is properly configured at client end for
connection ——Makepipe & readpipe are utilities to check for connection.
Makepipe is run on Server and readpipe on client to check for any connection
issues.
19. What are the authentication modes in SQL Server?

Windows mode and mixed mode (SQL & Windows).

20. Where do you think the users names and passwords will be stored in
sql server?

20. Where do you think the users names and passwords will be stored in sql
server?

21. What is log shipping? Can we do logshipping with SQL Server 7.0

Logshipping is a new feature of SQL Server 2000. We should have two SQL Server
- Enterprise Editions. From Enterprise Manager we can configure the
logshipping. In logshipping the transactional log file from one server is
automatically updated into the backup database on the other server. If one server
fails, the other server will have the same db and we can use this as the DR
(disaster recovery) plan

22. Let us say the SQL Server crashed and you are rebuilding the databases
including the master database what procedure to you follow?

For restoring the master db we have to stop the SQL Server first and then from
command line we can type SQLSERVER –m which will basically bring it into the
maintenance mode after which we can restore the master db.

23. What is BCP? When do we use it?

BulkCopy is a tool used to copy huge amount of data from tables and views. But it
won’t copy the structures of the same.

24. What should we do to copy the tables, schema and views from one SQL
Server to another?

We have to write some DTS packages for it.

Question 1 – How can SQL Server Management Studio help while
troubleshooting Performance Issues?
 SQL Server Management Studio (SSMS), helps us to run the queries or DMV’s,
We can generate execution query plans, Read/Write statistics, Time Statistics
by running the query from SSMS.

Question 2 – How can Server Profiler trace help while troubleshooting


Performance Issues?
 SQL Server Profiler trace can be used to identify all the queries running during
the problem time and which queries are taking longer times to complete.
Question 3 – What is SQL Server Database Tuning Adviser (DTA)?
 SQL Server Database Tuning Adviser (DTA) is available to check for missing
index and statistics recommendations.

Question 4 – What is SQL Server Performance Dashboard?


 SQL Server Performance Dashboard is used to generate performance related
reports (This needs to be installed as additional plugin)
 
Question 5 – What are SQL Server Extended Events?
  Starting SQL Server 2008 R2, we can use SQL Server Extended Events can be
used for more granular details with less performance overhead than profiler.
Once you know all the tools that you have – here is a real life scenario.

How do you approach or troubleshoot performance problems in SQL Server?


There are different ways of troubleshooting performance issues and there is
no right or wrong way of troubleshooting performance issues, it is just my
way of approaching in identifying the problem and then taking corrective
actions.
 First try to understand more details about the performance issues.
Normally performance problems are reported by Developers or Client as
per feedback from end users or some monitoring tools reporting specific
performance issue on the server.
 Ask Developers or Client questions to understand what they mean by
performance problems and gather mode details to pinpoint issue to a
specific application or a specific functionality of the application.
 Troubleshooting approach will differ for general performance problems
affecting whole application or more specific problem like slowness with
specific Stored Procedure or Query.
 Check the overall hardware resource usage like, CPU usage on the server,
Memory usage on the server, I/O usage on the server and Network usage to
see if there are any spikes in any of their usage. If yes, then drill further
down in that direction, if everything looks normal, then will proceed with
checking at SQL Server level.

Question 6 – How do you monitor resource usages?


 Resource usages can be monitored using Task Manager, Perfmon and using
sys.dm_exec_query_stats and sys.dm_exec_sql_text
 Check SQL Server errorlogs and eventlogs for any errors.

Question 7 – How do you check SQL Server error logs and Event Logs
 SQL Server logs can be viewed using xp_readerrorlogs. SQL Server
management studio also provides Log file viewer tool.
 Check for any blocking or heavy locking or high number of suspended
sessions.
Question 8 – How do you check blocking, locking or suspended connections?
 By running SP_WHO2, SP_WHO, select * from sys.sysprocesses and Activity
Monitor can be used to view blocking.
 Check waits stats to see the top waits
.
Question 9 – how do you check Wait stats?
 Select * from sys.dm_os_wait_stats gives information about wait stats.

Question 10 – What is wait time; signal wait time & resource wait time?
 SQL Server keeps track of the time that elapses between leaving the RUNNING
state and becoming RUNNING again called the “wait time” The time spent on
the RUNNABLE queue called the “signal wait time” – i.e. how long does the
thread need to wait for the CPU after being signaled that its resource is
available. We need to work out the time spent waiting on the SUSPENDED list
called the “resource wait time” by subtracting the signal wait time from the
overall wait time.
 Checking if there are regular maintenance on the SQL Server like
rebuilding indexes and update of statistics. If not, then will implement
those which will significantly improve the performance.

Question 11 – How can you check stats information?


 DBCC SHOW_STATISTICS (‘Person.Address’, AK_Address_rowguid) can be used
to view Stats information about individual index on a table.

Question 12 – How can you update stats on all tables in one go?
Use MyDatabase
Go
Exec sp_MSForEachtable 'UPDATE STATISTICS ? WITH FULLSCAN'
GO

Question 13 – What is difference between Reorganizing and Rebuilding index?


 Index Rebuild: This process drops the existing Index and Recreates the index.
 Index Reorganize: This process physically reorganizes the leaf nodes of the
index.

Question 14 – What is the key parameter for decision making about Rebuilding
or Reorganizing index?
 Its Fragmentation level

Question 15 – How do you check the fragmentation level?


 DMF sys.dm_db_index_physical_stats can be used to view fragmentation level
of an index.
 
 Will run DMV’s to identify Top Duration, Top CPU, Top Read or Write
intensive queries and try to tune them by creating appropriate indexes or
report them to developer suggesting to re-write those queries.

Question 16 – What are missing indexes and how can they be identified?
 When you run a SQL query, SQL Server determines what indexes it would like
to use, if these are not available, it makes a note of them. You can see details of
these missing indexes by using DMVs.

Question 17 – What are unused indexes and how can they be identified?
 Unused indexes are those indexes that exist, but are not being used and
therefore can be dropped to improve performance and decrease storage
requirements.
 

What question do you ask Developers or Client to understand more about the
performance issue?
 What kind of performance issue are you seeing, can you be more specific? It
is often said that the Application is slow or website is slow.
 Is there any specific feature or a webpage of the application that is slow or
is it that entire application is slow?

Question 18 – If you know that a feature of an application is performing slower


than expected, how will you identify the corresponding SQL Statement?
 There are many ways of doing this exercise. A profiler trace can be used; a
member of development team can be consulted. If SPID is known, then DBCC
Input buffer and other options can be used to find the SQL Statement.
 Since when you started seeing performance problems?
 Have you started seeing these problems after recent upgrades to
Application or SQL Server or OS and any other changes? Or were there
anything changed recently on the system, like applying patches, Hardware
or Application changes performed recently?

Question 19 – How can you check last patching activity status of SQL Server or
Operating System?
 Open Windows Update by clicking the Start button. In the search box, type
Update, and then, in the list of results, click Windows Update. In the left pane,
click View update history. This shows the latest patch that is applied with other
information like dates and KB Number.
 Are you aware of any changes to the data or increase in number of users on
the SQL Server recently?
Question 20 – If major bulk data deletion/insertion activity happened last night,
how will this activity hit performance?
 All DML operations (INSERT, UPDATE, and DELETE) can cause index
fragmentation.     
 So far have you observed anything that can point in a direction where could
be the problem?
 Have you checked application and web server to make sure problem does
not lie there itself? How you came to conclusion that problem is with SQL
Server?
 Do we have any baseline on the application performance or query
performance like, how much time it used to take before and how much time
it taking now?
 Have you performed any troubleshooting thus far and what are your
findings, if any, so far?
 

How do you troubleshoot slowness with a specific Stored Procedure or a


Query?
 First, get more details like, how much time on an average this query was
taking previously (baseline)?
 Were there any changes to the stored procedure or query recently?
 How often this query does runs?
 Does the problem only happen on this server or other servers too, meaning
can this be reproduced on Test or Dev servers?
 Check if this query is being blocked by other sessions.
 Check if this query is waiting some any resource using wait stats DMV’s.
 Check if statistics are up to date for the tables and indexes used in the
Stored procedure or the query.
 Check fragmentation of the objects in the stored procedure or the query.
 Collect execution plan of the Stored Procedure and the statements inside
the SP or the query. Also collect Read/Write and Time execution statistics of
the query.
 Check for any missing indexes based on the execution plan, based on table
or clustered index scans.
 Try and suggest to developers if the query needs to be rewritten in a better
way like replacing temp variables with tempdb tables or usage of functions
in where clause.

You might also like