Index in Database

Опубликовано: 01 Январь 1970
на канале: Global Exploration Knowledge Hub 2.0
3
1

Indexes in databases are critical for optimizing query performance by allowing the database management system (DBMS) to quickly locate and retrieve data without having to scan the entire table. Here’s an in-depth look at indexes, including their types, creation, usage, and best practices.

*1. What is an Index?*

An index is a database object that improves the speed of data retrieval operations on a table at the cost of additional storage space and potential overhead during data modification (inserts, updates, deletes). It works similarly to an index in a book, allowing quick access to specific information.

*2. Types of Indexes*

#### *2.1 Single-Column Index*

*Definition:* An index created on a single column of a table.
*Usage:* Ideal for queries that filter or sort by that single column.
*Syntax:*
```sql
CREATE INDEX index_name
ON table_name (column_name);
```
*Example:*
```sql
CREATE INDEX idx_lastname
ON Employees (LastName);
```

#### *2.2 Composite (or Multi-Column) Index*

*Definition:* An index created on two or more columns of a table.
*Usage:* Useful for queries that filter or sort by multiple columns.
*Syntax:*
```sql
CREATE INDEX index_name
ON table_name (column1, column2, ...);
```
*Example:*
```sql
CREATE INDEX idx_name_dept
ON Employees (LastName, DepartmentID);
```

#### *2.3 Unique Index*

*Definition:* Ensures that all values in the indexed column(s) are unique.
*Usage:* Useful for enforcing uniqueness constraints on columns.
*Syntax:*
```sql
CREATE UNIQUE INDEX index_name
ON table_name (column_name);
```
*Example:*
```sql
CREATE UNIQUE INDEX idx_email
ON Employees (Email);
```

#### *2.4 Clustered Index*

*Definition:* Determines the physical order of data in the table. A table can have only one clustered index.
*Usage:* Ideal for range queries where the data is frequently retrieved in a sorted order.
*Syntax:*
```sql
CREATE CLUSTERED INDEX index_name
ON table_name (column_name);
```
*Example:*
```sql
CREATE CLUSTERED INDEX idx_hiredate
ON Employees (HireDate);
```

#### *2.5 Non-Clustered Index*

*Definition:* Creates a separate structure from the data, with pointers to the actual data rows. A table can have multiple non-clustered indexes.
*Usage:* Useful for columns used in searches, filtering, and sorting, but does not affect the physical order of data.
*Syntax:*
```sql
CREATE NONCLUSTERED INDEX index_name
ON table_name (column_name);
```
*Example:*
```sql
CREATE NONCLUSTERED INDEX idx_salary
ON Employees (Salary);
```

#### *2.6 Full-Text Index*

*Definition:* Supports full-text search capabilities, enabling complex queries against character-based data.
*Usage:* Useful for searching large text fields for words or phrases.
*Syntax:*
```sql
CREATE FULLTEXT INDEX index_name
ON table_name (column_name);
```
*Example:*
```sql
CREATE FULLTEXT INDEX idx_description
ON Products (Description);
```

*3. Creating and Managing Indexes*

#### *3.1 Creating an Index*

*Basic Syntax:*
```sql
CREATE INDEX index_name
ON table_name (column_name);
```
*Example:*
```sql
CREATE INDEX idx_department
ON Employees (DepartmentID);
```

#### *3.2 Dropping an Index*

To remove an index, use the `DROP INDEX` statement.

*Syntax:*
```sql
DROP INDEX index_name;
```
*Example:*
```sql
DROP INDEX idx_department;
```

#### *3.3 Viewing Indexes*

To list existing indexes on a table, you can use:

*SQL Server:*
```sql
EXEC sp_helpindex 'table_name';
```

*MySQL:*
```sql
SHOW INDEX FROM table_name;
```

*PostgreSQL:*
```sql
\di table_name
```

*4. Best Practices for Indexing*

#### *4.1 Choose the Right Columns*

*High-Selectivity:* Index columns with high selectivity (i.e., columns with many distinct values).
*Frequently Queried Columns:* Index columns that are frequently used in `WHERE`, `JOIN`, and `ORDER BY` clauses.

#### *4.2 Avoid Over-Indexing*

*Impact on Performance:* Each index adds overhead to data modification operations. Avoid creating unnecessary indexes.

#### *4.3 Composite Indexes*

*Order Matters:* The order of columns in a composite index matters. Place the most selective columns first.
*Covering Indexes:* Create indexes that cover the columns used in queries to avoid accessing the table.

#### *4.4 Regular Maintenance*

*Rebuild Indexes:* Periodically rebuild or reorganize indexes to improve performance, especially in high-update environments.
*Monitor Performance:* Use database performance tools to monitor and adjust indexes based on query performance.