2. This session will cover…..
What are indexes and why they are used?
Indexes may be either Clustered or Non-
Clustered.
Types of indexes
How to create indexes
Indexes 2
3. What are indexes and why they
are used?
Every organization has its database and each and every day
with the increase in the data volume these organizations
has to deal with the problems relating to data retrieval and
accessing of data.
There is need of system which will results into increase in
the data access speed.
An index (in simple words it like index of any book eg.While
searching a word in Book we use index back of book to find
the occurrences of that word and its relevant page
numbers), which makes it easier for us to retrieval and
presentation of the data.
Indexes 3
4. An Index is a system which provides faster
access to rows and for enforcing constraints.
If we don't create any indexes then the SQL
engine searches every row in table (also
called as table scan). As the table data grows
to thousand, millions of rows and further then
searching without indexing becomes much
slower and becomes expensive.
Indexes 4
5. Consider the following query on the Products
table of the Northwind database.This query
retrieves products in a specific price range.
SELECT ProductID, ProductName, UnitPrice
FROM ProductsWHERE (UnitPrice > 12.5)
AND (UnitPrice < 14)
6. currently no index.
the database engine performs a scan and
examines each record to see if UnitPrice falls
between 12.5 and 14.
7.
8. Now imagine if we created an index on the
UnitPrice column.
Each index entry would contain a copy of the
UnitPrice value for a row, and a reference (just
like a page number) to the row where the value
originated.
SQL will sort these index entries into ascending
order.The index will allow the database to
quickly narrow in on the rows to satisfy the
query, and avoid scanning every row in the table.
10. How It Works
Conceptually, we may think of an index as
shown in the diagram below. On the left,
each index entry contains the index key
(UnitPrice). Each entry also includes a
reference (which points) to the table rows
which share that particular value and from
which we can retrieve the required
information.
11.
12. Clustered index
A common analogy for a clustered index is a
phone book. A phone book still sorts entries
into alphabetical order.The difference is,
once we find a name in a phone book, we
have immediate access to the rest of the data
for the name, such as the phone number and
address.
13.
14. A clustered index is the most important index
you can apply to a table.
If the database engine can use a clustered
index during a query, the database does not
need to follow references back to the rest of
the data, as happens with a nonclustered
index.
The result is less work for the database, and
consequently, better performance for a query
using a clustered index.
15. A Disadvantage to Clustered
Indexes
If we update a record and change the value of an
indexed column in a clustered index, the
database might need to move the entire row into
a new position to keep the rows in sorted order.
This behavior essentially turns an update query
into a DELETE followed by an INSERT, with an
obvious decrease in performance. A table's
clustered index can often be found on the
primary key or a foreign key column, because
key values generally do not change once a record
is inserted into the database.
16. Indexes may be either Clustered
or Non-Clustered.
A clustered index stores data rows in the
table based on their key values. Each table
can have only one clustered index as the key
values in the data rows are unique and the
index is built on the unique key column.When
a table has a clustered index, it is known as a
clustered table.
Indexes 16
17. Non-Clustered indexes have structures that
are different from the data rows.A non
clustered index key value is used to point to
data rows that contain the key value.This
value is known as row locator.
Indexes 17
18. Types of Indexes
In SQL Server 2005, indexes are created on
tables to increase the speed of data retrieval.
UNIQUE INDEX
FULLTEXT INDEX
XML INDEX
Indexes 18
19. UNIQUE INDEX:
A unique index does not allow any duplicate values
in the index field of a table.
It ensures that every row in the table is unique.
While designing a unique index, consider the
following guidelines:
A unique index cannot be created if duplicate key values
exists in the table.
Non-key columns can be included in a unique non-clustered
index.
Indexes 19
20. To create an index
1. In Object Explorer, right-click the table for
which you want to create an index and
click Modify.
Indexes 20
27. 6. Specify any other settings for the index and
click Close.
7.The index is created in the database when you
save the table.
SQL Server allows users create unique indexes
on unique columns such as the identity number
of the employee or student or whatever is the
unique key by which the component data are
identified.
Indexes 27
28. To create a unique index
In Object Explorer, right-click the table and
click Modify.
The table opens in Table Designer.
From the Table Designer menu,
click Indexes/Keys.
Click Add.The Selected Primary/Unique Key
or Index list displays the system-assigned
name of the new index.
Indexes 28
31. 6. Choose Index from the drop-down list to the
right of the property.
7. Under Column name, select the columns you
want to index.You can select up to 16 columns.
For optimal performance, select only one or two
columns per index. For each column you select,
indicate whether the index arranges values of
this column in ascending or descending order.
Indexes 31
34. 9. Choose Yes from the drop-down list to the
right of the property.
10. Select the Ignore duplicate keys option if
you want to ignore new or updated data that
would create a duplicate key in the index
(with the INSERT or UPDATE statement).
Indexes 34
36. 11. The index is created in the database
when you save the table or diagram
Indexes 36
37. note that unique indexes cannot be created
on a single column if the column contains
NULL in more than one row. Similarly indexes
cannot be created on multiple columns if the
combination of the columns contains NULL in
some rows.The NULL values are treated as
duplicate values.
Indexes 37
38. Clustered Index
Clustered indexes can be created in SQL
Server databases. In such cases the logical
order of the index key values will be the same
as the physical order of rows in the table. A
table can have only one clustered index.
Indexes 38
39. To create a clustered index
1. In Object Explorer, right-click the table for which you
want to create a clustered index and clickModify.
2.The table opens in Table Designer.
3. From the Table Designer menu, click Indexes/Keys.
4. In the Indexes/Keys dialog box, click Add.
5. Select the new index in the Selected Primary/Unique
Key or Index list.
6. In the grid, select Create as Clustered, and
choose Yes from the drop-down list to the right of the
property.
Indexes 39
40. 7. The index is created in the database
when you save the table.
Indexes 40
41. Full-Text Index
FullText Index helps to perform complex
queries against character data.
These queries can include word or phrase
searching.We can create a full-text index on a
table or indexed view in a database.
Only one full-text index is allowed per table
or indexed view.
Indexes 41
42. To create full-text indexes
In Object Explorer, right-click the table for
which you want to create a full-text index and
clickModify.
The table opens in Table Designer.
From the Table Designer menu, click Fulltext
Index.
Indexes 42
44. 4.The Full-text Index dialog box opens. If the
database is not enabled for full text indexing
the dialog box will have the add button
disabled.To enable full text indexing for the
database, right click the database>Click
properties and check the Full text indexing
check box.
Indexes 44
46. 5.Then create a catalog by right clicking on
Storage>FullText Catalog and creating a new
Catalog and entering the required
information in the dialog box that opens.
Indexes 46
49. 6. Now open the Full Text Index property
dialog box by clicking on it in the Table
Designer menu.
Indexes 49
50. 7. Click Add.
8. Select the new index in the Selected Full-
text Index list and set properties for the
index in the grid to the right.
9.Your index is automatically saved in the
database when you save your table in Table
Designer. The index is available for
modification as soon as you create it.
Indexes 50
51. To change index properties
1. In Object Explorer, right-click the table
you want to open and click Modify.
2. From the Table Designer menu,
click Indexes/Keys.
3. Select the index from the Selected
Primary/Unique Key or Index list.
4. Change properties in the grid.
5.The changes are saved to the database
when you save the table.
Indexes 51
52. To rename an index
1. In Object Explorer, right-click the table with
the index you want to rename and click Modify.
2. From the Table Designer menu,
click Indexes/Keys.
3. Select the index from the Selected
Primary/Unique Key or Index list.
4. In the grid, click Name and type a new name
into the text box.
Indexes 52
53. 5. The changes are saved to the database
when you save the table.
Indexes 53
54. Indexes can be deleted. Usually an index is
considered for deletion when the
performance of the INSERT,UPDATE and
DELETE operations are hindered by the
Index.
Indexes 54
55. To delete an index
1. In Object Explorer, right-click the table with
indexes you want to delete and click Modify.
2. From the Table Designer menu,
click Indexes/Keys.
3. In the Indexes/Keys dialog box, select the index
you want to delete.
4. Click Delete.
5.The index is deleted from the database when the
table is saved.
6. A similar procedure can be followed for deleting a
full text index by selecting Full text index from the
Table Designer and selecting the index name and
clicking delete button.
Indexes 55
56. FREETEXT( )
FREETEXT( ) Is predicate used to search
columns containing character-based data
types. It will not match the exact word, but
the meaning of the words in the search
condition.
Indexes 56
57. CONTAINS( )
CONTAINS( ) is similar to the Freetext but
with the difference that it takes one keyword
to match with the records, and if we want to
combine other words as well in the search
then we need to provide the “and” or “or” in
search else it will throw an error.
Indexes 57