Indexes in SQL

medium · SQL

The Query Optimization Engine: Indexing By default, when you execute a query with a WHERE clause filtering by a non-indexed column, the database engine must perform a Full Table Scan . This means it reads every single data block from the physical disk drive, row-by-row, from the beginning of the table to the end. If a table holds millions of records, a full table scan causes massive disk I/O bottlenecks and high CPU usage. Indexing solves this problem. An index is a specialized lookup structure that the database engine builds and maintains alongside your data, allowing it to pinpoint the exact location of specific rows instantly without scanning the entire table. 1. What is Indexing? (The B-Tree Architecture) Most relational database engines (like PostgreSQL and MySQL) store indexes in a balanced hierarchical tree structure known as a B-Tree (Balanced Tree) . A B-Tree index organizes sorted data pointers into structural layers: Root Nodes , Internal Nodes , and Leaf Nodes . Instead of searching linearly, the query engine starts at the root node, navigates through down-pointers based on range boundaries, and jumps directly to the matching data target. This reduces a slow, sequential search algorithm of O ( N ) down to a highly efficient logarithmic path of O ( lo g N ) . THE B-TREE INDEX TRAVERSAL ┌───────────┐ │ Root Node │ │ [ 50 ] │ └─────┬─────┘ ┌─────────────────┴─────────────────┐ ▼ ▼ ┌─────────────┐ ┌─────────────┐ │Internal Node│ │Internal Node│ │ [25 | 35] │ │ [65 | 80] │ └──────┬──────┘ └──────┬──────┘ ┌───────────┼───────────┐ ┌───────────┼───────────┐ ▼ ▼ ▼ ▼ ▼ ▼ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │Leaf:1-24│ │Leaf:25-34││Leaf:35-49│ │Leaf:50-64││Leaf:65-79││Leaf:80+ │ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ 2. Primary Storage Structures: Clustered vs. Non-Clustered Indexes Relational engines utilize two distinct index designs to manage how data rows relate physically to disk block layouts. TABLE DATA LAYOUT ARCHITECTURES │ ┌──────────────────────────────┴──────────────────────────────┐ ▼ ▼ Clustered Index Non-Clustered Index • Dictates physical disk layout. • Separate standalone lookup tree structure. • One index per table maximum. • Multiple separate indexes permitted. • Leaf nodes hold the actual raw data rows. • Leaf nodes hold reference pointers to rows. A. Clustered Index A clustered index determines the physical, sequential order in which rows are sorted and stored on the storage drive. Because the data rows themselves can only be sorted in one way on disk, a table can have exactly one clustered index. The Rule: In almost all relational engines, your table's Primary Key automatically acts as the clustered index. Leaf Node Payload: The leaf nodes of a clustered index do not contain pointers; they contain the actual physical row data itself. B. Non-Clustered Index A non-clustered index is a completely separate structure from the data table. It acts like an index at the back of a textbook: it stores a sorted list of values from a specific column alongside a physical reference address (a Pointer or Row ID) pointing back to where the full row resides. Leaf Node Payload: The leaf nodes contain the index value paired with a pointer back to the actual data location (either a row identifier or the clustered index key). 3. Specialized Multi-Column and Guardrail Indexes A. Composite Index (Multi-Column Index) A composite index is an index built over two or more columns simultaneously . It is used to optimize queries that consistently filter by multiple criteria in their WHERE clauses. SQL -- Creating a composite index tracking matching node behaviors CREATE INDEX idx_nodes_region_status ON cluster_nodes (cloud_region, node_status); ⚠️ The Critical Left-to-Right Prefix Rule The database engine sorts a composite index based on the columns in the exact order they are defined (from left to right). Because of this, the index will only speed up your queries if your filters include the leftmost column. Optimized: WHERE cloud_region = 'us-east-1' AND node_status = 'ACTIVE' (Uses full index). Optimized: WHERE cloud_region = 'us-east-1' (Uses partial left prefix). NOT Optimized: WHERE node_status = 'ACTIVE' (Bypasses index entirely; causes a slow table scan because the leading column is missing). B. Unique Index A unique index ensures that no two rows in a table can share the exact same value within the indexed column. The database engine enforces this constraint at the hardware engine layer, blocking any duplicate insert attempts. SQL -- Enforces absolute network identity values while speeding up point lookups CREATE UNIQUE INDEX idx_unique_node_ip ON cluster_nodes (assigned_ip); Identity Relationship: When you define a UNIQUE constraint on a column via standard DDL table parameters, the RDBMS automatically creates a matching Unique Index behind the scenes to handle the constraint check efficiently. 4. System Optimization Blueprint & Guardrails While indexes drastically speed up read queries ( SELECT ), they come with a distinct performance cost that must be balanced during database design: THE INDEX BALANCING ACT │ ┌─────────────────────────────┴─────────────────────────────┐ ▼ ▼ Read Queries (SELECT) Write Queries (INSERT/UPDATE) • Ultra-fast execution path. • Slower execution overhead. • Bypasses full-table scans. • Engine must update all index trees. • Drastically reduces disk I/O. • Increases total storage footprint. Production Optimization Best Practices Index Foreign Keys explicitly: While engines automatically index Primary Keys, they generally do not automatically index Foreign Keys. Always create indexes manually on your foreign key columns to speed up JOIN execution paths. Avoid indexing low-cardinality columns: Cardinality represents the number of unique values in a column. Indexing a field with very few unique states (like a boolean is_active flag) is highly inefficient; the query planner will usually ignore the index and run a full-table scan anyway. Prevent Function Blindness: If you apply a calculation or modification function directly to an indexed column inside a filter, the database engine cannot use the index. SQL -- ANTI-PATTERN: Triggers a slow full table scan because of the function transformation SELECT node_id FROM cluster_nodes WHERE LOWER(node_name) = 'edge-node'; -- PRODUCTION STANDARD: Keep columns clean to ensure the index executes correctly SELECT node_id FROM cluster_nodes WHERE node_name = 'Edge-Node'; Database Indexing Reference Matrix Index Category Vector Physical Disk Storage Relationship Multi-Column Capability Critical Production Guardrail Clustered Index Identical. Physically reorders the raw table records on disk. Supported (Composite Primary Keys). Limit exactly 1 per table. Modifying a clustered value forces the engine to physically relocate the entire row on disk. Non-Clustered Index Standalone secondary B-Tree lookup structure pointing back to data. Supported. Every index you add consumes extra storage space and slows down write operations ( INSERT , UPDATE , DELETE ). Composite Index B-Tree sorted by the left-to-right definition hierarchy. Yes. Combines multiple target columns together. Always put the most frequently filtered column first to satisfy the Left-to-Right Prefix Rule . Unique Index Prevents duplicate entries inside the tracking index node keys. Supported. Accepts NULL values depending on engine rules. Multiple rows can contain NULL because NULL represents an unknown state, meaning it technically never matches another null.

Back to SQL

Browse all study material on Careeroza