Index (INDEX) in SQL
SQL index (INDEX) is a special lookup table that database search engines can use to quickly increase the time and performance of data retrieval.
Simply put, an index is a pointer to each value that appears in the indexed table / column. Index in Database has the same meaning as the entries in the Index of a book.
INDEX speeds up SELECT queries containing WHERE or ORDER clauses, but it slows down data entry with UPDATE and INSERT statements. Indexes can be created or deleted without affecting data.
Index types available in SQL:
- Single-Column Index
- Unique Index
- Composite Index
- Implicit Index
To create an index we use the CREATE INDEX command , you can name the index, specify the table, the columns you want to index, and specify the index in ascending or descending order.
CREATE INDEX command
The basic syntax of CREATE INDEX command in SQL is as follows:
CREATE INDEX ten_index ON ten_bang;
SINGLE-COLUMN index
Single-Column Index is created for only 1 column in the table. The basic syntax is as follows:
CREATE INDEX ten_index
ON ten_bang (ten_cot);
UNIQUE index
Unique Index is a unique index, used to increase performance and ensure data integrity. A unique index does not allow any duplicate values to be inserted into the table. Basic syntax is as follows.
CREATE UNIQUE INDEX ten_index
ON ten_bang (ten_cot);
Index COMPOSITE
Composite Index is a combined index for two or more columns in a table. Its basic syntax is as follows:
CREATE INDEX ten_index
ON ten_bang (cot1, cot2);
Note :
- Creating Single-Column Index or Composite Index depends on how often you use the WHERE clause of the query as a filter condition.
- If only one column is used, the best option is the Single-column Index . If two or more columns are used frequently in the WHERE clause as filters, then the Composite Index index is the more optimal option.
IMPLICIT INDEX
Implicit Index is an index that is created automatically by the Database Server when a table is created. Default indexes are automatically created for Primary key constraints and Unique constraints.
DROP INDEX command
When you don't need to use INDEX, you can DROP according to the following syntax:
DROP INDEX ten_index;
You should be careful while deleting an index, because then the performance may be slower or not improved.
When should I avoid using INDEX?
Although INDEX is intended to improve Database performance, sometimes you should avoid using them. Here are some cases you need to consider to decide whether to use INDEX:
- Should not be used in small tables, few records.
- Index should not be used in tables where frequent UPDATE and INSERT operations occur.
- It should not be used for columns that contain a large number of NULL values.
- Index should not be used for columns that are frequently modified.
In the next article, Quantum will discuss with you how to use the ALTER TABLE command in SQL. Have you remembered to it!
Previous lesson: Create a temporary name using ALIAS in SQL
Next lesson: ALTER TABLE statement in SQL
You should read it
- Use Index in Access 2016
- How to write the above index, below index in Excel
- 10 T-SQL Index statements needed with DBA
- Instructions for indexing on the index on the index in Word
- Air quality index - What is AQI?
- What is a crawl? What is index?
- Index in SQL Server
- Limitations of indexes in MongoDB
- What is FPS index? What FPS setting is good for gaming?
- Instructions for using Index function in Excel
- How to use the INDEX function in excel?
- Index (Mong) in MongoDB