0% found this document useful (0 votes)
5 views49 pages

Querying Data With Transact-SQL Part 03 (1)

The document provides a comprehensive overview of SQL indexes, including their types, structures, and importance in database management. It covers concepts such as clustered and non-clustered indexes, index fragmentation, and strategies for maintaining indexes. Additionally, it introduces indexed views and their benefits in improving query performance.

Uploaded by

volinets0
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)
5 views49 pages

Querying Data With Transact-SQL Part 03 (1)

The document provides a comprehensive overview of SQL indexes, including their types, structures, and importance in database management. It covers concepts such as clustered and non-clustered indexes, index fragmentation, and strategies for maintaining indexes. Additionally, it introduces indexed views and their benefits in improving query performance.

Uploaded by

volinets0
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/ 49

Database

Management System​
Querying Data with Transact-SQL

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Indexes

BI Lab Training Course 2020

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


FOR YOUR INFORMATION

• Please do not record video


• Please turn off the
microphone.
• If you have questions, ask
them in the chat.
• Duration: 2 hours
• Coffee break 15 minutes

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 3


AGENDA:

1) What is An index?
2) B-tree index structure
3) Clustered Tables vs Heap Tables
4) Types of Indexes
5) Clustered index
6) Non Clustered index
7) Unique index
8) Filtered index
9) Covered index
10) Index Fragmentation
11) Rebuild vs. Reorganize Strategy
12) Fill factor
13) Introduction to SQL Server indexed view

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 4


SQL - INDEXES

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


TABLE BASICS
SQL Server tables are contained within database
object containers that are called Schemas. The schema
also works as a security boundary, where you can limit
database user permissions to be on a specific schema
level only. You can imagine the schema as a folder that
contains a list of files. You can create up to
2,147,483,647 tables in a database, with up to 1024
columns in each table. When you design a database
table, the properties that are assigned to the table
and the columns within the table will control the
allowed data types and data ranges that the table
accepts. Proper table design, will make it easier and
faster to store data into and retrieve data from the
table.
Physically, SQL Server tables are stored in database as
a set of 8 KB pages.
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
SQL SERVER INDEX

SQL Server index is considered as one of the most


important factors of the performance tuning process,
that is 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
the table index as 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 subject. An example of a book’s index
that locates the page where you can find each word is
shown below:

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


WHAT IS AN INDEX?
• An index is an on-disk structure associated with a table or view that speeds retrieval of rows
from the table or view. An index contains keys built from one or more columns in the table
or view. These keys are stored in a structure (B-tree) that enables SQL Server to find the row
or rows associated with the key values quickly and efficiently.
• You can find general information about tables and indexes in the sys.indexes catalog view.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


B-TREE INDEX STRUCTURE

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CLUSTERED TABLES VS HEAP TABLES

Heap Tables Clustered Tables


• If a table has no indexes or only has • The name suggests itself, these tables
non-clustered indexes it is called a have a Clustered Index. Data is stored
heap in a specific order based on a
Clustered Index key.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CLUSTERED TABLES VS HEAP TABLES

Heap Tables
• Data is not stored in any particular order
• Specific data can not be retrieved quickly, unless there are also
non-clustered indexes.
• Data pages are not linked, so sequential access needs to refer
back to the index allocation map (IAM) pages
• Since there is no clustered index, additional time is not needed
to maintain the index
• Since there is no clustered index, there is not the need for
additional space to store the clustered index tree
• These tables have a index_id value of 0 in the sys.indexes
catalog view
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
CLUSTERED TABLES VS HEAP TABLES

Clustered Table
• Data is stored in order based on the clustered index
key
• Data can be retrieved quickly based on the clustered
index key, if the query uses the indexed columns
• Data pages are linked for faster sequential access
• Additional time is needed to maintain clustered
index based on INSERTS, UPDATES and DELETES
• Additional space is needed to store clustered index
tree
• These tables have a index_id value of 1 in the
sys.indexes catalog view

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CONFIDENTIAL | © 2020 EPAM Systems, Inc.
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
TYPES OF INDEXES

• Clustered index
• Non Clustered index
• Unique index
• Filtered index
• Covered index

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CLUSTERED INDEX

• A clustered index sorts and stores the data rows of


the table or view in order based on the clustered
index key.
• Only one clustered index per table!
• Leaf level of the index tree - actual data rows
• Good for sequential access and range selection
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
CLUSTERED INDEX

• MS Sql Server INSERTS data according to the way a clustered


index was created
• Most often: PRIMARY KEY => Clustered Index
• Every table SHOULD have clustered index
• w/o clustered index: records added to the end of the last page
• with clustered index: data added to suitable position dictated
by the index

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CREATE [UNIQUE] CLUSTERED INDEX index_name
ON <object> ( column [ ASC | DESC ] [ ,...n ] )
[ ON { partition_scheme_name ( column_name )
| filegroup_name
| default
}
]

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


THE ATTRIBUTES THAT MAKE UP AN EFFICIENT CLUSTERED INDEX KEY:

• Narrow – as narrow as possible, in terms of the number of bytes it stores


• Unique – to avoid the need for SQL Server to add a “uniqueifier” to
duplicate key values
• Static – ideally, never updated
• Ever-increasing – to avoid fragmentation and improve write performance

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


NON-CLUSTERED INDEX

• Data in pages in random order


• Logical data order in index
• Non-Clustered index tree
– Keys in sorted order
– Leaf pages contain pointers to rows in data pages
• Typically created on column used in JOIN, WHERE, ORDER BY
• Good for tables whose values may be modified frequently
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
NON-CLUSTERED INDEX

• MS Sql Server:
CREATE INDEX -> Non-Clustered by default
• Allowed more than one index on a db table
• MS Sql Server 2008:
up to 999 Non-Clustered indexes per table

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CREATE [ UNIQUE ] NONCLUSTERED INDEX index_name
ON <object> ( column [ ASC | DESC ] [ ,...n ]
)
[ INCLUDE ( column_name [ ,...n ] ) ]
[ WHERE <filter_predicate> ]
[ WITH ( <relational_index_option> [ ,...n ] )
]
[ ON { partition_scheme_name ( column_name )
| filegroup_name
| default
}
]

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


UNIQUE INDEX

• An index that ensures the uniqueness of each value in the indexed column.
• A unique index ensures that the index key contains no duplicate values.
• Both clustered and nonclustered indexes can be unique.
• If the index is a composite, the uniqueness is enforced across the columns as
a whole, not on the individual columns.
• A unique index is automatically created when you define a primary key or
unique constraint:
o Primary key
o Unique
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
COVERING INDEXES

• Extending functionality of Non-Clustered indexes


• Adding non-key columns to the leaf level
• Index covers more types of queries
• Covering Indexes = Indexes with include columns
• Great performance benefits

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


FILTERING INDEXES

• SQL Server 2008 introduces Filtered Indexes which is an index


with a WHERE clause
• An optimized non-clustered index, especially suited to cover
queries that select from a well-defined subset of data.
• Filtered indexes can provide the following advantages over full-table
indexes:
o Improved query performance and plan quality
o Reduced index maintenance costs
o Reduced index storage costs
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
EXAMPLES
Table with NO indexes

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Create NONCLUSTERED INDEX

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Table with non-clustered index on LastName

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Create CLUSTERED INDEX

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Table with clustered index on LastName

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CONFIDENTIAL | © 2020 EPAM Systems, Inc.
Create NONCLUSTERED INCLUDED INDEX

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Table with non-clustered Index on LastName including FirstName

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Create NONCLUSTERED FILTERED INDEX

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Table with filtered index

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Does the order of columns in a multi-column non-clustered index
created for a SQL Server table really matter?

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


INDEX FRAGMENTATION

Internal fragmentation External fragmentation

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CONFIDENTIAL | © 2020 EPAM Systems, Inc.
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
 the internal fragmentation (the avg_page_space_used_in_percent column)
 the external fragmentation (the avg_fragmentation_in_percent column)

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


REBUILD VS. REORGANIZE STRATEGY

• Fragmentation is less than 10% – no de-fragmentation is required. It is


generally accepted that in majority of environments index
fragmentation less than 10% in negligible and its performance impact on
the SQL Server is minimal.
• Fragmentation is between 10-30% – it is suggested to perform index
reorganization
• Fragmentation is higher than 30% – it is suggested to perform index
rebuild

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SOLUTIONS TO ADDRESS INDEX FRAGMENTATION:

• Recreate - CREATE INDEX WITH DROP_EXISTING – Not a good choice


because of possible issues with constraints (Foreign Keys etc.)
• Rebuild - ALTER INDEX ... REBUILD (replaces DBCC REINDEX)
• Reorganize - ALTER INDEX ... REORGANIZE (replaces DBCC
INDEXDEFRAG)
• To reduce the extent fragmentation of a heap, create a clustered index
on the table and then drop the index

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


CONFIDENTIAL | © 2020 EPAM Systems, Inc.
FILL FACTOR
• Tuning storage and performance
• Fill factor = % of space for data in leaf pages
• Remainder of the page for future growth
• E.g. Fill factor=80% => 20% page empty
• Reserved space between index rows
(rather than at the of the index)
• Applied on CREATE or REBUILD INDEX
CONFIDENTIAL | © 2020 EPAM Systems, Inc.
FILL FACTOR
• If data is always inserted at the end of the table, then
the fill factor could be between 90 to 100 percent since
the data will never be inserted into the middle of a
page.
• If the data can be inserted anywhere in the table then a
fill factor of 60 to 80 percent could be appropriate based
on the INSERT, UPDATE and DELETE activity.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


INTRODUCTION TO SQL SERVER INDEXED VIEW
Regular SQL Server views are the saved queries that provide some benefits such as query simplicity, business
logic consistency, and security. However, they do not improve the underlying query performance.
Unlike regular views, indexed views are materialized views that stores data physically like a table hence may
provide some the performance benefit if they are used appropriately.
To create an indexed view, you use the following steps:
• First, create a view that uses the WITH SCHEMABINDING option which binds the view to the schema of the
underlying tables.
• Second, create a unique clustered index on the view. This materializes the view.
Because of the WITH SCHEMABINDING option, if you want to change the structure of the underlying tables
which affect the indexed view’s definition, you must drop the indexed view first before applying the changes.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


T H A N K YO U F O R YO U R AT T E N T I O N

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy