wasAsked.

Indexes and types

HCA Healthcare · Role not specified · technical

Published

Year not specified · SQL

Community answers

Posted by wasAsked editorial collection

An index is a data structure that helps the database find rows quickly without scanning the whole table, similar to a book's index. Main index types (SQL Server terminology, with equivalents in other databases) 1. Clustered index - Determines how table rows are stored: the leaf level contains the actual data, sorted by the index key. - Only one per table; often the primary key. - Best for range queries and the main access path. 2. Non-clustered index - A separate structure with key values and pointers (row locators) to the data. - Many per table; ideal for searching on columns other than the clustered key. - Can include extra columns (INCLUDE) to become covering. 3. Unique index - Ensures no duplicate values in the indexed columns; created automatically by PRIMARY KEY and UNIQUE constraints. 4. Composite (multi-column) index - An index on several columns, such as (LastName, FirstName). Column order matters: it supports filters on the leading column(s). 5. Covering index - Contains every column needed by a query, so the database never reads the base table. 6. Filtered index (SQL Server) / partial index (PostgreSQL) - Indexes only rows meeting a condition: CREATE INDEX IX_Orders_Open ON Orders(CreatedAt) WHERE Status = 'Open'; - Smaller and faster for queries targeting that subset; useful for unique constraints that ignore soft-deleted rows. 7. Columnstore index - Stores data by column with heavy compression; excellent for analytics and aggregations over large tables (data warehouses, reporting). 8. Full-text index - Supports word and phrase searches over text (CONTAINS, FREETEXT), with linguistic features. 9. Other specialised indexes - XML and spatial indexes in SQL Server; hash indexes for memory-optimised tables; GIN and GiST indexes in PostgreSQL for JSONB, arrays, full-text search and geometric data; computed-column or expression indexes (LOWER(Email)). Trade-offs - Indexes speed up reads but slow down inserts, updates and deletes, and consume storage. - Too many indexes hurt write-heavy tables; too few cause slow queries. Best practices: index foreign keys and frequent filter or sort columns, choose a narrow, unique, increasing clustered key, check execution plans, keep statistics updated, rebuild or reorganise fragmented indexes when it actually matters, and drop unused indexes.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics