Unit-3: Indexes, Query Optimization and Performance Tuning
Unit-3: Indexes, Query Optimization and Performance Tuning
II)
#2101CS302
Unit-3
Indexes, Query
Optimization and
Performance Tuning
Prof. Naimish R. Vadodariya
Computer Engineering Department
Darshan University, Rajkot
[email protected]
8866215253
Outline
Looping
• Index Introduction
• Types of Indexes
• Clustered v/s Non-clustered Index
• Execution Plan
• Types of Execution Plan
• Query Optimization Techniques
How will the database engine retrieve the data from a table in SQL Server?
Whenever the database engine wants to retrieve the data from a database table it will adopt
two different mechanisms for searching the data
Table scan
Index Scan/Seek
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 3
What is Table Scan in SQL Server?
In Table Scan, the SQL Server Search Engine will search for the required information
sequentially one by one from the start to the last record of the table.
If the table has more rows, then it will take more time for searching the required data, so it is
a time-consuming process.
Let us understand how the SQL Server Database Engine searches the data when there is no
index available on the table i.e. Table Scan.
When there is no index in the table, SQL Server searches the data sequentially.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 4
What is Table Scan in SQL Server?
Please have a look at the following image for a better understanding.
Suppose, you want to search the value 50, then the search engine (i.e. SQL Server Search
Engine) will scan the record sequentially one by one from the beginning i.e. from 1, and until
it reaches the value 50.
If you want to increase the search performance, then somehow you have to minimize the
number of scans. That is what exactly the B-Tree (Balanced Tree) does.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 5
What is Index Scan/Seek in SQL Server?
In Index Scan, the SQL Server Search Engine uses a B-Tree structure to search the required
data which drastically improves the performance of your search query by reducing the
number of scans.
So, let us first understand what B-Tree structure is and how it reduces the number scan which
ultimately improves the performance of your search query.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 6
Understanding the Balanced Tree (B-Tree) in SQL Server:
Whenever you create an index (or indexes) on
some column(s) of a table in SQL Server then
what happens internally is, it creates a B-Tree
structure.
In the B-Tree structure, the data is divided into
three sections i.e. Root Node, Non-Leaf
Nodes, and Leaf Nodes.
In order to understand this better please have a
look at the following image which shows how
the data is divided and stored.
As you can see, in the Root Node it has 30 and
50. In the Non-Leaf node, it has 10, 30, 40,
and 50.
And in the leaf node, we have the actual data.
So, the leaf node is actually pointing to data.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 7
Understanding the Balanced Tree (B-Tree) in SQL Server:
Suppose, you want to search 50 here, then what
will happen internally is, the search engine will
start the search from the root node.
It will check whether 50 is less than or equal to 30.
As 50 is not less than or equal to 30, so the non-
leaf nodes and leaf nodes that come under the root
node 30 are completely bypassed.
Then it will go to the next node i.e. 50 and check
whether 50 is less than or equal to 50.
And the condition satisfies here. Then it goes to the
non-leaf nodes (40, 50) which are under the root
node 50.
It will check whether 50 is less than or equal to 40
and the condition fail, so, it will bypass all the leaf
nodes which come under the non-leaf node 40.
Then it will check the other non-leaf node i.e. 50
and here the condition satisfies as 50 equals 50 and That is, it approximately scans 10 records.
it goes to scan the leaf node sequentially.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 8
Indexes
Section - 1
Indexes : Introduction
SQL Indexes are used in relational databases to quickly retrieve data.
They are similar to indexes at the end of the books whose purpose is to find a topic quickly.
Data in a table is stored in rows in an unordered structure called Heap.
If you have to fetch data from a table, the query optimizer has to scan the entire table to
retrieve the required row(s).
If a table has a large number of rows, then SQL Server will take a long time to retrieve the
required rows.
So, to speed up data retrieval, SQL Server has a special data structure called indexes.
An index is mostly created on one or more columns which are commonly used in the
SELECT clause or WHERE clause.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 10
Types of Indexes
SQL Server supports two types of indexes:
Clustered Index
Non-Clustered Index
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 11
Clustered Index
Clustered indexes define the way records are physically sorted in a database table.
A clustered index is very similar to the Dictionary.
Either the Letters are sorted according to their relevance or they can be sorted alphabetically.
There can be only one way in which records can be physically sorted on a disk.
For example, records can either be sorted by their ids (in table) or they can be sorted by the
alphabetical order of some string column or any other criteria.
However, you cannot have records physically sorted by ids as well as names (in real world
scenario).
Hence, there can be only one clustered index for a database table and it is called as
clustered index.
A database table has one clustered index by default on the primary key column.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 12
Clustered Index Cont..
To see the default index, you can use the sp_helpindex stored procedure as shown below:
You can see the clustered index name and the column on which the clustered index has been
created by default.
Example
Think About State Table if we write SELECT * FROM State
You can see that the records have been sorted by default clustered index for the primary key
column i.e. StateID.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 13
Clustered Index Cont..
To create a clustered index in SQL Server, Here is the syntax:
CREATE CLUSTERED INDEX <index_name>
ON <table_name>(<column_name> ASC/DESC)
--Example
CREATE CLUSTERED INDEX PK_MST_State_StateID
ON State(StateID ASC)
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 14
Clustered Index - Example
Suppose, we have Book table, we want to Select * From Books
create clustered index on price column of
the table.
CREATE CLUSTERED INDEX
IX_BUK_Book_Price
ON Books (Price ASC)
We create a clustered index
named IX_BUK_Book_Price.
This clustered index physically sorts all
the records in the Books table by the
ascending order of the price.
Let’s now select all the records from the
Books table to see if they have been
sorted in the ascending order of their
prices:
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 15
Understanding the Balanced Tree (B-Tree) in SQL Server:
Whenever you create an index (or indexes) on
some column(s) of a table in SQL Server then
what happens internally is, it creates a B-Tree
structure.
In the B-Tree structure, the data is divided into
three sections i.e. Root Node, Non-Leaf
Nodes, and Leaf Nodes.
In order to understand this better please have a
look at the following image which shows how
the data is divided and stored.
As you can see, in the Root Node it has 30 and
50. In the Non-Leaf node, it has 10, 30, 40,
and 50.
And in the leaf node, we have the actual data.
So, the leaf node is actually pointing to data.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 16
Clustered Index - Example
In SQL Server, there is a system table with the name sys.indexes that content information
about indexes are available on tables into the database.
Select * From Sys.indexes
Where Name like 'PK%’
A table even has no index, there will be one row in the sys.indexes table related to that table
indicating there is no index on the table.
When you write a select statement with a condition where clause than the first SQL Server
will refer to the “indid” (index id).
Columns of the sys.indexes table determine whether or not the column on which you write
the conditions has an index.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 17
Clustered Index
A B-Tree (computed) clustered index is the index that will arrange the rows physically in the
memory in sorted order.
An advantage of a clustered index is that searching for a range of values will be fast.
A clustered index is internally maintained using a B-Tree data structure leaf node of B-Tree
of clustered index will contain the table data.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 18
Remember!!
Clustered Index Non-Clustered Index
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 19
Non-clustered Index
A non-clustered index is an index that doesn’t physically sort the database records.
Rather, a non-clustered index is stored at a separate location from the actual database table.
It is the non-clustered index which is actually similar to an index of a book.
A book index is stored at a separate location, while the actual content of the book is
separately located.
To create a non-clustered index in SQL Server, you can modify SQL CREATE INDEX. Here
is the syntax:
CREATE NONCLUSTERED INDEX <index_name>
ON <table_name>(<column_name> ASC/DESC)
--Example
CREATE NONCLUSTERED INDEX PK_MST_State_StateName
ON State(StateName ASC)
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 20
Non-clustered Index Cont..
The non-clustered index is stored at a location which is different from the location of the
actual table, the non-clustered index that we created will look like this:
StateName Record Addresses
Gujarat Record address
Rajasthan Record address
Maharastra Record address
Karachi Record address
Islamabad Record address
Now anyone searches for StateName, StateID & Code for a specific state, the database will
first search the state’s name in the non-clustered index.
Once the state name is searched, the StateID & Code of the state are searched from the actual
table using the record address of the record in the actual table.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 21
Non-clustered Index Cont..
A non-clustered index is an index that will not arrange the rows physically in the memory
in sorted order.
An advantage of a non-clustered index is searching for the values that are in a range will be
fast.
You can create a maximum of 999 non-clustered indexes on a table, which is 254 up to SQL
Server 2005.
A non-clustered index is also maintained in a B-Tree data structure but leaf nodes of a B-Tree
of non-clustered index contains the pointers to the pages that contain the table data and not
the table data (rows/records) directly.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 22
Retrieving data with non-clustered index (Remember)
When you write a select statement with a condition in a where clause then SQL Server will
refer to “index_id” columns of sys.indexes table and when this columns contains the value in
the range of 2 to 1000 then it indicates that the table has a non –clustered index and in this
case it will refer to the columns root of sys.indexes table to get the addresses.
Select * From Sys.indexes
Where type_desc like 'NONCLUSTERED'
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 23
Clustered v/s Non–Clustered Index
Parameters Clustered Index Non – Clustered Index
A non-clustered index helps you to creates a
You can sort the records and store clustered index
Use for logical order for data rows and uses pointers
physically in memory as per the order.
(addresses) for physical data files.
Allows you to stores data pages in the leaf nodes This indexing method never stores data pages in
Storing method
of the index. the leaf nodes of the index.
The size of the non-clustered index is small
Size The size of the clustered index is quite large.
compared to the clustered index.
Data accessing Faster Slower compared to the clustered index
Additional disk
Not Required Required to store the index separately
space
By Default Primary Keys Of The Table is a It can be used with unique constraint on the table
Type of key
Clustered Index. which acts as a composite key.
A clustered index can improve the performance of It should be created on columns which are used
Main feature
data retrieval. in joins.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 24
Execution Plan
Section - 2
Introduction : Execution Plan
Being a Data Professional, it is very essential that we must understand how to write efficient
queries that return faster results and how to tune the slow performing queries to achieve a
boost in performance.
In order to understand how the database engine works behind the scenes, we need to know
the logical operations performed by the query processor.
This can be done by investigating the execution plans generated by the query processor.
An execution plan in SQL Server is a simple graphical representation of the operations
that the query optimizer generates to calculate the most efficient way to return a set of
results.
Once a query is executed, the query processing engine quickly generates multiple execution
plans and selects the one which returns the results with the best performance.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 26
Types of Execution Plan
There are two types of execution plans
1. Estimated Execution Plan –
As the name suggests, this type of execution plan is just a guess by the query processor about how the
specific steps that are to be involved while returning the results.
It is often generated before the query has been executed.
2. Actual Execution Plan –
The Actual Execution Plan is generated after the query has been executed.
It shows the actual operations and steps involved while executing the query.
This may or may not differ from the Estimated Execution Plan.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 27
How to generate Estimated Execution Plans?
The Execution Plans in SQL Server can be generated before and after the query has been
executed.
There are several ways to get the estimated execution plan in SQL Server.
1. Once the query is written completely, you can hit “Ctrl + L” and it will generate the
estimated execution plan.
2. You can also right-click on the query window and select “Display Estimated Execution
Plan” from the context menu that appears
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 28
How to generate Estimated Execution Plans? Cont..
Alternatively, you can directly click the “Display Estimated Execution Plan” icon which is
available on the toolbar in SQL Server Management Studio
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 29
How to generate Actual Execution Plans?
1. Hit “Ctrl + M” and it will generate the actual execution plan after the query has been
executed successfully.
2. Right-click on the query window and select “Display Actual Execution Plan” from the
context menu
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 30
How to generate Actual Execution Plans? Cont..
Alternatively, you can directly click the “Display Actual Execution Plan” icon which is
available on the toolbar in SQL Server Management Studio
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 31
1. Estimated Execution Plan - Example
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 32
2. Actual Execution Plan
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 33
Estimated = Actual
Estimated Actual
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 34
Saving an Execution Plan in SQL Server Management Studio
Sometimes, it is essential that after interpreting the plan generated by the query, you might
want to save if for future references.
SSMS provides an option to save the plan in the file system with an extension of “.sqlplan“.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 35
Saving an Execution Plan in SQL Server Management Studio
Cont..
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 36
Query Optimization
Section - 3
Query Optimization
SQL (Structured Query Language) performance tuning can be an incredibly challenging task,
especially when working with large-scale data where even the smallest change can have a
dramatic performance effect, positively or negatively.
In order to get the exact data we’re looking for we need to provide the appropriate query.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 38
1. Use of Varchar instead of Char Data Type
Both char and varchar data types are used to store character strings in the table.
But varchar is much more memory efficient than char.
The char datatype can only store the character string of fixed length defined.
If the length of the string is less than the fixed length, then it will pad the blank spaces to
make its length equal to the set length.
This will unnecessarily waste memory in padding.
i.e. CHAR(100) will take 100 bytes of memory even if a single character is stored.
On the other hand, varchar datatype stores the character string of variable length having a
length less than the maximum length specified.
It does not pad the blank spaces and only takes the memory equal to the string's actual length.
i.e.VARCHAR(100) takes only 1 byte of memory when storing a single character.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 39
2. SELECT fields, rather than using SELECT *
Use the SELECT statement optimally, instead of always fetching all data from the table.
Fetch only the necessary data from the table, thereby avoiding the costs of transferring
unwanted data and processing it.
Inefficient
SELECT * FROM Business
Efficient
SELECT CustName, Phone, C_Address, CompanyZip FROM Business
This query is much simpler, and only pulls the required details from the business table.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 40
3. Avoid DISTINCT in SELECT query
SELECT DISTINCT is a simple way of removing duplicates from a database.
SELECT DISTINCT works to generate distinct outcomes by using the GROUP BY clause,
which groups all the fields in the query.
However, a large amount of processing power is required to do this. So, avoid DISTINCT in
SELECT queries if not required.
Inefficient
SELECT DISTINCT FName, LName, Country FROM Customers
Multiple people in the same country might have the same first and last name.
Efficient
SELECT ID, FName, LName, Country, State, City, Zip FROM Customers
Unduplicated records are returned without using SELECT DISTINCT by adding more fields.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 41
4. Be aware of NULL in your results
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without
adding a value to this field.
Then, the field will be saved with a NULL value.
Note: A NULL value is different from a zero value or a field that contains spaces.
Inefficient
Select 1 + NULL
Answer will be NULL, but in real world this is wrong because many columns with having null values and
when you do any operation on it then result will be null.
Efficient
Select 1 + ISNULL(NULL,0)
Answer will be 1. Whenever you work with null values try to replace null with some meaningful values.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 42
5. Do not include column numbers in ORDER BY
The main purpose of using this is for usability and scalability and not just performance.
It might not be a concern while you create the database, but when time progresses and new
columns are applied to the SELECT statement or whether you have used ColumnNumber, the
original table is reordered.
When you use ColumnNumber, then the results will be unpredictable and wrong in terms of
ORDER BY.
Inefficient
Select StateName, StateCode, Remarks From State Order By 1,2
This will perform order by with column sequence of your table.
Efficient
Select StateName, StateCode, Remarks From State Order By StateCode, StateName
Always write column names in order by instead of numbers.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 43
6. Prefix for stored procedures not that starts with “sp_”
You should not use a prefix for stored procedures that starts with “sp_”.
It is a syntax of the system that SQL provides us.
It is advised to follow a unique pattern to name written procedures that can be differentiated
easily.
Because system procedures also starts with “sp_” like sp_helptext, sp_helpindex etc.
Inefficient
Create Procedure sp_tblState_insert
Efficient
Create Procedure PR_LOC_State_Insert
Create Procedure PR_LOC_State_SelectForAll
Create Procedure PR_LOC_State_Delete
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 44
7. Indexing
Indexing in SQL Server helps retrieve data more quickly from a table, thereby giving a
tremendous boost to SQL query performance.
Allow effective use of clustered and non-clustered indexes.
Use a index to reduce the time needed for the execution of commonly used statements.
Indexes occupies disk space, the more indexes you have, the greater the space used on the
disk.
In SQL Server, a clustered index requires no additional disk space, but any non-clustered
index needs additional disk space as it is stored separately from the list.
When you run a query in SQL Server, the optimizer creates an execution plan.
If it finds out that there is a missing index that might be created to improve performance, it
will create a suggestion that will be displayed in the warning section.
This suggestion will tell you which columns should be indexed in current SQL and how
performance will be enhanced after that.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 45
8. To check the existence of records, use EXISTS() rather than COUNT()
Both EXISTS() and COUNT() methods can be used to check the existence of a record entry
in the table.
The COUNT() method would scan the entire table to return the number of records in the table
that match the provided constraint.
The EXISTS() method is more effective as it exits processing as soon as it finds the first
entry of the record in the table.
Inefficient
IF (SELECT COUNT(CountryID) FROM Country WHERE CountryName like 'in%') > 0
PRINT 'Yes‘
Efficient
IF EXISTS (SELECT CountryID FROM Country WHERE CountryName like 'in%')
PRINT 'Yes'
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 46
9. Use WHERE instead of HAVING
The HAVING clause filters the rows after all the rows are selected.
It works just like a filter, do not apply the HAVING clause for any other purpose.
HAVING statements are determined in the SQL operating order after WHERE statements.
Therefore, it is quicker to execute the WHERE query.
Inefficient
SELECT c.ID, c.CompanyName, b.CreatedDate FROM Business b
INNER JOIN Company c ON b.CompanyID = c.ID
GROUP BY c.ID, c.CompanyName, b.CreatedDate
HAVING b.CreatedDate BETWEEN '2020-01-01' AND '2020-12-31'
Efficient
SELECT c.ID, c.CompanyName, b.CreatedDate FROM Business b
INNER JOIN Company c ON b.CompanyID = c.ID
WHERE b.CreatedDate BETWEEN '2020-01-01' AND '2020-12-31'
GROUP BY c.ID, c.CompanyName, b.CreatedDate
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 47
10. Ignore linked sub queries
A linked subquery depends on the query from the parent or from an external source.
It runs row by row, so the average cycle speed is greatly affected.
Inefficient
SELECT b.Name, b.Phone, b.Address, b.Zip, (SELECT CompanyName FROM Company WHERE
ID = b.CompanyID) AS CompanyName
FROM Business b
For each row returned by the external query, the inner query is run every time. Alternatively, JOIN can be
used to solve these problems for SQL database optimization.
Efficient
SELECT b.Name, b.Phone, b.Address, b.Zip, c. CompanyName
FROM Business b
Inner Join Company c
ON b.CompanyID = c.ID
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 48
11. Ordering JOINs from a Larger Table to a Smaller Table
Applying the JOIN operation from a larger table to a smaller table is a common SQL
optimization technique.
Because joining from a larger table to a smaller table will make your query to execute faster.
If we apply a JOIN operation from a smaller table to a larger table, our SQL engine has to
search in a larger table for matching rows.
This is more resource-intensive and time-consuming.
But on the other hand, if the JOIN is applied from a larger table to a smaller table, then the
SQL engine has to search in a smaller table for matching rows.
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 49
12. Ordering JOINs from a Larger Table to a Smaller Table Cont..
Inefficient
-- Join from a smaller table to a larger table
SELECT * FROM Customer
LEFT OUTER JOIN Order
ON Customer.id = Order.id
Efficient
-- Join from a larger table to a smaller table
SELECT * FROM Order
LEFT OUTER JOIN Customer
ON Customer.id = Order.id
Prof. Naimish R. Vadodariya #2101CS302 (DBMS-II) Unit 3 – Indexes, Query Optimization and Performance Tuning 50
Database Management System - II (DBMS-II)
2101CS302
Thank
You