What is an index in SQL?
An index is created in a table to increase the performance of queries and the data pages are stored contiguously when the index is created and when the index is new-built.
Index allows us to retrieve very fast data from the database and allow us to searching millions of records quickly.
How do database indexes work?
There are some strategies that make indexes work,
1. Optimize your code.
2. Restructure your data.
3. Compress your data.
4. Materialize them.
How do indexes help?
For Example, suppose is a student and studying a book and this book contains 10,000 pages.
In the first day I read some topic “abc” and next day I want to read some another topic “pqr”. I will never manually go through page by page. It is very difficult to go there. In this situation, I am using book index to find specific topic and go directly to this page because IDEX allow us to search millions of record quickly!
What types of indexes?
4. Index with included columns
How does an index improve performance?
An index is a way to physically re-organise the records and used to run faster queries.
The index can be used as a pointer to the large table. It helps to find row quickly and then return back to the user.
What are some best practices for creating indexes?
Some of the rules are to creating index as,
1. Index ALL primary keys columns (It is default).
2. Index ALL foreign keys columns
3. You create more indexes only when the queries are too slow otherwise ignoring it.
The examples are,
-- CREATE INDEX
CREATE INDEX INDEX_NAME ON TABLE_NAME;
-- CREATE SINGLE COLUMN INDEXE
CREATE INDEX INDEX_NAME ON TABLE_NAME (COLUMN_NAME);
-- CREATE UNIQUE INDEXE
CREATE UNIQUE INDEX INDEX_NAME ON TABLE_NAME (COLUMN_NAME);
-- CREATE COMPOSITE INDEXE
CREATE INDEX INDEX_NAME ON TABLE_NAME (COLUMN_NAME_1, COLUMN_NAME_2);
DROP INDEX INDEX_NAME;
I hope you enjoying with this post! Thank you very much!