0% found this document useful (0 votes)
12 views7 pages

JETIR1805119

The document discusses SQL query optimization techniques aimed at improving query performance in SQL Server. It covers methods for identifying challenging queries, analyzing execution plans, and various optimization strategies such as using indexes, optimizing join orders, and applying hints. Additionally, it provides practical tips for writing efficient queries and emphasizes the importance of understanding database tools for performance tuning.

Uploaded by

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

JETIR1805119

The document discusses SQL query optimization techniques aimed at improving query performance in SQL Server. It covers methods for identifying challenging queries, analyzing execution plans, and various optimization strategies such as using indexes, optimizing join orders, and applying hints. Additionally, it provides practical tips for writing efficient queries and emphasizes the importance of understanding database tools for performance tuning.

Uploaded by

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

© 2018 JETIR May 2018, Volume 5, Issue 5 www.jetir.

org (ISSN-2349-5162)

SQL Query Optimization - Techniques and Tips


LALIT GANDHI, RESEARCH SCHOLAR, UIET, M.D.University, Rohtak(HARYANA) INDIA
Abstract: SQL Queries areused to fetch data from database. The main goal of writing queries is to fetch data but within
manageable time. For heavy databases, queries need to be optimized to give results in minimum possible timeframe.
Sometimes the reason for low query performance is that queries are not written effectively and efficiently.This paper
presents how these SQL queries can be improved for better functioning. Query optimization area is much extensive and
this paper attempts to cover the most important points. In this paper, in- depth analysis of identifying the critical queries
and simple query tuning tips & tricks which can be applied to gain fast enhancement improvement.
I. Introduction:
There are numerous writing and web distributions on methods and best practices of query improvement, covering all
accessible database administration frameworks, like Oracle and MySQL. This paper presents emphasis on SQL Server
Query enhancement tips. Here, I describe the steps I prefer and some examples of query optimization. This paper provides
tips and tricks for SQL server developer to optimize the query execution and enhance performance
II. Identifying Challenging Queries:
First step for query optimization is that it should be well known that which query need optimization and performance
could be enhanced. This may vary from case to case and it can have different start criteria –
(i) You already know that which query is not performing up to mark and need tuning.
(ii) Your entire system is not performing well and you need to find out which area need tuning.
In later instance, we need to identify the critical areas where improvement is required.
There are number of SQL Server tools that are available to identify the diagnostic areas. SQL Profiler is one of the built in
tools with SQL server package. SQL profiler discovers the query received from SQL Server. With the help of profiler, we
can check the execution time and text of query. SQL profiler is under tools menu of SQL Server. Queries, which are
taking longest time, can be identified using filters.

Figure 1
Following are steps followed, to check longest running queries.
1. Go to Event Selection in trace properties windows.
2. Select Custom Filters button, leads to edit filter dialogue box.
3. Select the duration and set filter accordingly.

JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 668
© 2018 JETIR May 2018, Volume 5, Issue 5 www.jetir.org (ISSN-2349-5162)

Required query will be selected, to be act upon. So, with the help of SQL profiler challenging query can be detected.
Once we detect the particular query then we start working in the direction of optimizing the same. Now, check the
execution plan for the same query and perform the optimization techniques to performance tune the same.
III. Execution plan:
Once we have identified the challenging query, and then analyze the execution of it. Execution of a query is portrayed
with execution plan. SQL server query optimizer facilitates execution plan. Execution plan tells about each minute details
of the execution actions performed while query execution. Execution plan can be obtained in textual or graphical form.
(a) Execution plan in text format:
Turn on the execution plan display by firing the statement “set showplan_text on”. Execute button will show Execution
plan. E.g. execution plan for a query in textual format can be shown as –
Query –

Figure 2
Execution plan -

Figure 3
(b) Execution plan in graphical format:
Execution plan can be graphically seen by pressing CTRL+L on query window. The graphical query plan can be seen as
in figure 4. Icons represent data processing and arrows show the percentage of data processed.

JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 669
© 2018 JETIR May 2018, Volume 5, Issue 5 www.jetir.org (ISSN-2349-5162)

Figure 4
(c) XML based execution plan :
It provided XML based complete information. There are two ways to obtain the same as SET SHOWPLAN_XML and
SET STATISTICS XML. It shows the complete details as -

Figure 5
Graphical plans can be displayed with Tooltips and can be saved in XML format. Text plans are not for beginners as the
are harder to read.

IV. Query Optimization


The basic ways for improving query optimization using execution plan:
1. Using Indexes:

Best method to improve the speed is to create indexes. Mere creating indexes does not resolve the problem and speed up
the query. It is also possible that creating an index may improve the reading but it may have adverse effect while
changing the table. So it is important, what operations we are performing on tables.

JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 670
© 2018 JETIR May 2018, Volume 5, Issue 5 www.jetir.org (ISSN-2349-5162)

In case of composite indexes, order of attributes is important. To find the order we can use selectivity coefficient. This
coefficient determines the importance of particular attribute with in query and number of records cut off, i.e. how many
records are selected compared to the general selection by using this. Number of records get reduced will result in faster
query processing. Primary and Unique keys have best selectivity coefficient, that is the reason for using primary keys as
default indexing attribute.

Selectivity coefficient is determined by analyzing the query and determining which column in the query is most effective.
This is determined by calculating the number of records returned by each column and in case of composite columns
indexing next effective column is determined to be used as part of composite index.
This method is useful only when we know the constants used during query execution. Different cases select different
columns. Selectivity coefficient can be determined by purpose of a column e.g. selectivity of field DOB is more than that
of gender of an employee in Employee table. We can also find the selectivity using statistics of the column.
There are some scenarios when not to use indexes. Indexes are created on columns but instead of using index, complete
table is scanned, in those scenarios, indexes should not be used. Optimizer does not use indexes when –
(i) A column value is calculated by using Functions. As function is applied to column so the index has no
importance and whole column, values are required to be calculated.
(ii) Values associated range is too large, optimizer checks that the range of values too large, then it decided
not to waste time using to and fro from index area to actual value area and scan the complete table
column.
To search fastest, equal(=) operator is recommended to be used with indexed column. Real scenario queries also need to
use comparison operators, so it’s suggested to use operators with column selection. This will reduce the number of
records and enhance performance of query. While using clustered index, it is suggested to use equal operator with first
column.
2. Optimizing order of Joining Tables:

Joins used in the queries affect the query performance. Time taken will be more in case query joins large tables in
advance rather than smaller tables. Joining larger tables in advance will cost more instead of joining smaller tables as it
will take less time.
However, SQL Server Optimizer tries to join tables those results in smallest possible set of records. Optimizer finds the
order of joining tables that results in smaller record set, but in complex queries it does not attempt to perform all
permutations, as this also consume a lot of resources and costs more. As a good practice user,it can join the tables those
results in smaller set of records. User can apply the Force Order to the query in case of complex queries, where user
knows in advance that this join will result in smaller record set. Consider the example in the below query-
SELECT PI.ParentID,CI.ChildID,SI.SmallID
FROM [dbo].[Parent] PI INNER JOIN
[dbo].[Child] CI ON CI.ParentID=PI.ParentID INNER JOIN
[dbo].[Small] SI ON SI.SmallID=CI.ParentID
OPTION (FORCE ORDER)

JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 671
© 2018 JETIR May 2018, Volume 5, Issue 5 www.jetir.org (ISSN-2349-5162)

Explain plan for this query results in thousands of records when joining Parent and Child records, and then joining the
same with smaller table results in less than hundred records.rows.

Figure 6

Now, take it another way to join smallest table first. Here the SQL statement is -
SELECT PI.ParentID, CI.ChildID, SI.SmallID
FROM [dbo].[Small] SI INNER JOIN
[dbo].[Parent] PI ON SI.SmallID=PI.ParentID INNER JOIN
[dbo].[Child] CI ON PI.ParentID=CI.ParentID

Explain plan for this query results in less than ten records when joining Parent and Small records, and then joining the
same with Child table results in less than hundred records.

Figure 7

This is evident from the queries explained that second option will work better and we can see the statistics of SQL
Profiler to confirm.This result in reducing the resources required to execute query

CPU Reads Writes Duration


First Join 270 6000 0 300
Second Join 10
0 30 0

3. Using Hints for SQL Query Optimizing


Hints, is a way of controlling the query execution plan. Query optimizer is not bound to follow the Hints but user can
provide the same to guide optimizer for following up Hints.The following Hints can be used for controlling the query
execution.
A. Indexes
a. WITH (INDEX<index_name>) – guides the optimizer to use specific index for retrieving data.
b. WITH (INDEX(0)) – guides the optimizer, not to use index and perform full table scan.

JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 672
© 2018 JETIR May 2018, Volume 5, Issue 5 www.jetir.org (ISSN-2349-5162)

B. Joining Tables
a. LOOP, HASH – provide the directions to optimizer to use Nested Loops or Hash joins.
b. OPTION (LOOP/HASH JOIN) – asks the Optimizer to use Nested Loops/Hash Match method for all
table joins.
c. OPTION (FORCE ORDER) –joined tables must follow the order mentioned in query.

4. Analyzing the Statistics


Query optimizer utilizes statistics to generate execution plans. These are the statistical information objects, which are
connected to sharing of values inside indexed views or columns of the table. If statistics are generated for any index or a
table, then query optimizer can find the optimal execution plan quicker. User can generate the statistics and use the same
for query optimization.Execution plan for smaller and larger volume of data are much different and statistics are also
different. In addition, test data cannot provide same results as real data can. This is also not possible to get the real time
data so we can get the stats from production data and use the same for loading into test environment. Therefore, optimizer
will follow that it is working with large volume of data and will create execution plans consequently.

V. Tips and Tricks


There are few tips and tricks that can be used while writing the queries, to improve performance.
1. EXISTS is preferable in place of IN.
2. Practice to put column names instead of * in SELECT statement.
3. Choice of appropriate datatype for attributes can reduce considerable time of query execution.
4. Do not use nchar and nvarchar, as these occupy double memory.
5. Having clause should be used for further result filtering only
6. Use Indexing, both Clustured and Non-Clustered indexing whichever is required.
7. Drop indexes that are not in use.
8. Integer indexes should be preferred over String or character indexes
9. Sub-queries should be avoided and usage of joins instead of is recommended.
10. Use UNION ALL in place of UNION if possible.
There can be n number of tips and tricks to be followed up depending upon scenario and system.

VI. Conclusion
In this paper we have discussed, basic query optimization techniques and few tips. SQL server provides enough tools
to improve performance analysis and tuning the sql queries. Important area is learning and using the same. These
techniques help user to avoid annoyance and improve the quality of work. Main database objects like tables, indexes
must follow the optimization rules and with the passage of time this will automatically come into nature of user.
VII. References:

1. https://www.apriorit.com/dev-blog/381-sql-query-optimization
2. General information about SQL Server Profiler - https://technet.microsoft.com/en-
us/library/ms181091%28v=sql.110%29.aspx;
3. View and Analyze Traces with SQL Server Profiler - https://msdn.microsoft.com/en-
us/library/ms175848%28v=sql.120%29.aspx
4. Description of graphical objects - https://msdn.microsoft.com/en-
us/library/ms191158.aspx, https://technet.microsoft.com/en-us/library/ms178071%28v=sql.105%29.aspx;
5. Improving SQL performance using Execution Plan - https://msdn.microsoft.com/en-us/library/ff647793.aspx
6. Dan Tow «SQL Tuning» (with multiple SQL query optimization examples) – http://www.amazon.com/SQL-
Tuning-Dan-Tow/dp/0596005733;

JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 673
© 2018 JETIR May 2018, Volume 5, Issue 5 www.jetir.org (ISSN-2349-5162)

7. SQL Query Tuning for SQL Server –


http://cdn.swcdn.net/creative/v9.4/pdf/Whitepapers/SW_SQL_Query_Tuning_SQL_Server_Confio_Whitepa
per_May2012.pdf;
8. SQL Server Index Design Guide – https://msdn.microsoft.com/en-
us/library/jj835095%28v=sql.120%29.aspx;
9. Statistics – https://msdn.microsoft.com/en-us/library/ms190397%28v=sql.120%29.aspx;

JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 674

You might also like