Performance Tunning
Performance Tunning
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 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 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 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 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.
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?
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.
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: 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?
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?
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?
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?
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.
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?
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.
There are basically two types of indexes that we use with the SQL Server.
Clustered and the Non-Clustered.
SQL Server runs on port 1433, however it can be change better security purpose.
From the Network Utility TCP/IP properties –> Port number.both on client and
the server.
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.
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?
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
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)
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?
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.
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?
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 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 12 – How can you update stats on all tables in one go?
Use MyDatabase
Go
Exec sp_MSForEachtable 'UPDATE STATISTICS ? WITH FULLSCAN'
GO
Question 14 – What is the key parameter for decision making about Rebuilding
or Reorganizing index?
Its Fragmentation level
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 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?