0% found this document useful (0 votes)
9 views

Lecture6_DBP2024

Visual programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture6_DBP2024

Visual programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

LECTURE6

Cursor and Indexes in


SQL Server
CURSOR IN SQL SERVER
CURSOR
A SQL cursor is a database object that is used to retrieve data from
a result set one row at a time.

A SQL cursor is used when the data needs to be updated row by


row.
Cursor life cycle
The following steps are involved in a SQL cursor life cycle.
Declaring Cursor: A cursor is declared by defining the SQL statement.
Opening Cursor: A cursor is opened for storing data retrieved from the
result set.
Fetching Cursor: When a cursor is opened, rows can be fetched from
the cursor one by one or in a block to do data manipulation.
Closing Cursor: The cursor should be closed explicitly after data
manipulation.
Deallocating Cursor: Cursors should be deallocated to delete the
cursor definition and release all the system resources associated with the
cursor.
CURSOR
Why use a SQL Cursor?
In relational databases, operations are made on a set of rows. For
example, a SELECT statement returns a set of rows which is called
a result set. Sometimes the application logic needs to work with one
row at a time rather than the entire result set at once. This can be
done using cursors.
CURSOR
The following cursor is defined for retrieving Id and Name from tblProducts.
The FETCH_STATUS value is 0 until there are rows. When all rows are
fetched then FETCH_STATUS becomes 1.
CURSOR
CURSOR
What are the limitations of a SQL Cursor
A cursor occupies memory from your system that may be
available for other processes.
The cursors are slower because they update tables row
by row.
Using cursor to retrieve data from two tables
simultaneously gives very poor performance. So , cursors
can be replaced using joins.
INDEXES IN SQL SERVER
INDEXES
It is a database object in SQL server which is used to
improve the performance of search operations.
When we create an index on any column of a table, then
SQL Server internally maintains a separate table called
index table.
In a table, we can use a maximum of 1000 indexes.
INDEXES
When SQL Server uses Indexes?
The SQL Server uses indexes of a table provided
that the select or update or delete statement
contained the “WHERE” clause and moreover the
where condition column must be an indexed
column. If the select statement contains an
“ORDER BY” clause then also the indexes can be
used.
INDEXES
How will be the database engine retrieves 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
INDEXES
What is Table Scan in SQL Server?
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 having more rows, then it will take more time for
searching the required data,
INDEXES
What is Index Scan/Seek in
SQL Server?
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 clustered index.
INDEXES
Type of Indexes SQL Server supports two types of indexes:.
Clustered Index.
Non-Clustered Index.
The syntax for creating an Index in SQL Server:
CREATE [UNIQUE] [CLUSTERED/ NON-CLUSTERED] INDEX
<INDEX NAME> ON <TABLE NAME> (<COLUMN LIST>)
To see the index: sp_helpindex <TABLE NAME>
To drop an index: Drop index <INDEX NAME>
Clustered Index
A clustered index sorts and stores the data rows of the table or
view in order based on the index key
clustered index is the index that will arrange the rows physically
in the memory in sorted order.
This type of index is implemented as a B-tree structure that
supports fast retrieval of the rows, based on their key values.
you can create only one clustered index for a table.
Clustered Index
We can create a Clustered Index using one of the following two
ways.
Using Primary Key
When we create a Primary Key on a table then the system creates a
Clustered Index automatically
Clustered Index
Using "Create Clustered Index" Query
We can also create an index using a "Create Clustered Index"
query . The following is a sample
Clustered Index
Insert and Update with Clustered Index
Since a clustered index arranges the rows physically in the
memory in sorted order, insert and update will become slow
because the row must be inserted or updated in sorted order.
Finally, the page into which the row must be inserted or
updated and if free space is not available in the page then
creating the free space and then performing the insert,
update and delete.
Clustered Index
Advantages of Clustered Indexes
Quickly returns a large range of data
Fast for presorted results
Physical ordering of the data of the column
A Clustered Index can improve the performance of data
retrieval
Disadvantages of Clustered Indexes
Frequently updated data increases the overhead of
rearranging the B-tree.
Only one Clustered Index per table.
If the size of the table is small then it is not effective
Non-clustered Index
A non-clustered index is an index that will not
arrange the rows physically in the memory in sorted
order.
You can create a maximum of 999 non-clustered
indexes on a table
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
directly.
Non-clustered Index
The following describes the creation of a non-clustered and non-unique
index for a single column.
Non-clustered Index
Insert and Update with a Non-clustered Index
There will be no effect of insert and update with a non-
clustered index because it will not arrange the row
physically in the memory in sorted order.
With a non-clustered index, rows are inserted and
updated at the end of the table.
Indexes
Clustered Index Non- Clustered Index

This will arrange the rows physically in the memory This will not arrange the rows physically in the
in sorted order memory in sorted order.

This will fast in searching for the range of values. This will not fast in searching for the range of
values.

You can create a maximum of 999 non clustered


Only one clustered index per table
indexes for table.

doesn't require additional disk space additional storage space is required.

leaf node of b-tree of clustered index will contain


Leaf nodes of b-tree of non-clustered index
the table data
contains the pointers to the pages that contain the
table data and not the table data directly..
Indexed View in SQL Server
In general, when we create a view, the view does not store any data
by default, so, when we query a view, it actually queries the
underlying base table and gets the data.
But we can change this default behavior in SQL Server i.e. the SQL
Server Views can store the data physically. In order to do this, first,
you need to create an index on the view
Indexed View in SQL Server
Rules for creating an Index view in SQL Server:
• The view should be created with the SCHEMABINDING option
• If an Aggregate function is used in the select list which references an
expression, and if there is a possibility for that expression to
become NULL, then, a replacement value should be specified. In this
example, we are using ISNULL() function, to replace NULL values
with ZERO with the expression SUM(ISNULL((QuantitySold *
UnitPrice), 0)) AS TotalSalesPrice
• If the GROUP BY clause is specified, then the view select list must
contain a COUNT_BIG(*) expression.
• The base tables in the view should be referenced with 2 part name. In
this example, Product and ProductSales are referenced
using dbo.Product and dbo.ProductSales respectively.
Indexed View in SQL Server
 Create a view:

 Creating an index on a view:


Indexed View in SQL Server
• When to use Indexed View in SQL Server?
• Indexed views are ideal for scenarios, where the underlying data is not
frequently changed.
• Indexed views are more often used in OLAP systems (online analytical
processing) because the data is mainly used for reporting and analysis
purposes.
• The Indexed views may not be suitable for OLTP systems (Online
Transaction Processing), as the data is frequently added and changed.
https://dotnettutorials.net/lesson/sql-server-indexed-view/
Indexed View in SQL Server

You might also like