Now, letโs move to the next topic:
Indexes ๐
๐ง 1. What is an INDEX?
An INDEX is used to make data retrieval faster
๐ Think like a book ๐
- Without index โ scan every page
- With index โ jump directly to topic
Same happens in databases ๐ฏ
โก 2. Why Use Indexes?
โ Faster SELECT queries
โ Faster searching
โ Better performance on large tables
โ But:
- Uses extra storage
- INSERT/UPDATE become slightly slower
๐ Visual Understanding
โก 3. Create an INDEX
CREATE INDEX idx_salary
ON employees(salary);
๐ Creates index on salary column
๐ 4. Query Using Indexed Column
SELECT FROM employees
WHERE salary > 50000;
โ Faster because of index
โ 5. Drop an INDEX
DROP INDEX idx_salary ON employees;
๐ฅ 6. Primary Key Automatically Creates Index
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(50)
);
โ PRIMARY KEY โ automatically indexed
โก 7. Types of Indexes
- Primary Index: Created on primary key
- Unique Index: Prevent duplicate values
- Composite Index: Index on multiple columns
๐ฏ 8. Composite Index Example
CREATE INDEX idx_dept_salary
ON employees(department, salary);
โ Useful when filtering both columns together
๐ฏ 9. Practice Tasks
1. Create index on employee name
2. Create index on department column
3. Create composite index on department + salary
4. Query employees using indexed column
5. Drop created index
โก Mini Challenge ๐ฅ
๐ Create a unique index on email column
๐ฅ Indexes improve READ speed but may slow down INSERT / UPDATE
Double Tap โค๏ธ For More
Post #2770
5.62K
- โค 21
- ๐ 4