JETIR1805119
JETIR1805119
org (ISSN-2349-5162)
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.
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
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.
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)
JETIR1805119 Journal of Emerging Technologies and Innovative Research (JETIR) www.jetir.org 674