Indexing and Pagenation in SQL
Indexing
An index can be created in a table to increase the performance of application and we can get the data more quickly and efficiently
Let’s see an example to illustrate this point suppose now we are reading book in that I need to check the information for dbmanagement to get this information I need to search each page of the book because I don’t know in which page that word information exists it’s time taken process. Instead of reading the each page of book to get that particular word information if I check the index of book (Glossary) it is much quicker for us to get the pages which contains the information with dbmanagement word. By using second method we can save lot of time and we can get information in efficient way.
Example of creating SQL Index on multiple columns
CREATE INDEX SampleIndex ON UserInformation (UserName,FirstName)
|
To Drop Index on table use the below statement
DROP INDEX TABLE_NAME.INDEX_NAME
|
In SQL we are having two types of indexes are there
1) Clustered Index
2) Non-Clustered Index
Clustered Index
Only 1 allowed per table physically rearranges the data in the table to confirm to the index constraints for use on columns that are frequently searched for ranges of data for use on columns with low selectivity.
Non-Clustered Index
Create table School
(
ID INT,
Class VARCHAR(10),
Name VARCHAR(10)
PRIMARY KEY (ID)
)
exec sp_helpindex School
INSERT INTO School Values(1,'Fi1','senthil')
INSERT INTO School Values(4,'Fi3','senthil')
INSERT INTO School Values(3,'Fi2','senthil')
INSERT INTO School Values(5,'FiF2','senthil')
INSERT INTO School Values(2,'Fi1','senthil')
Create NonClustered index New_School
ON School (Class,Name)
Up to 249 allowed per table creates a separate list of key values with pointers to the location of the data in the data pages For use on columns that are searched for single values For use on columns with high selectivity
A clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages. A non-clustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a non-clustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.
Pagenation
Declare @PageNo int = 2
Declare @PageSize int = 10
Select * from (
Select Class,Name ,
Row_Number() over(order by ID)as RowNo
From School)T
Where T.RowNo Between ((@PageNo - 1)*@Pagesize)+1 AND (@PageNo*@PageSize)
Comments
Post a Comment