Skip to content

Chapter 6 of 8

Indexes, Views, and Advanced Features

As tables grow, raw scans become expensive, and indexes are the primary tool for speeding up lookups. An index is a data structure, much like the index at the back of a book, that allows the database to find rows matching a column value without examining every record. It is created with a statement like CREATE INDEX idx_name ON table(column);. Different types of indexes serve different needs: B-tree indexes are the default and work well for equality and range queries, Hash indexes are optimized for exact matches, Bitmap indexes suit low-cardinality columns, and Full-text indexes support text searching. A clustered index physically sorts the table data according to the index key.

Views simplify complex queries and enhance security by exposing a virtual table defined by a SELECT statement. Creating CREATE VIEW active_users AS SELECT * FROM users WHERE active = 1; gives developers a convenient name for a frequent filter. Window functions add another layer of analytical power; they compute values across a set of rows related to the current row without collapsing them. Examples include ROW_NUMBER() OVER (ORDER BY score DESC), RANK(), and LAG(). For very large tables, partitioning divides the data into smaller, manageable pieces by range, list, or hash, which improves both query performance and maintenance.

To keep queries running fast, query optimization is essential. The first step is usually inspecting an execution plan with EXPLAIN, or EXPLAIN ANALYZE in PostgreSQL, which reveals how the database intends to run a query and where the bottlenecks lie. From there, developers can add appropriate indexes, rewrite queries to reduce joins or subqueries, and rely on the database's statistics about data distribution to choose efficient plans.

All chapters
  1. 1Database Fundamentals and SQL
  2. 2Tables, Keys, and Data Types
  3. 3Defining Database Structure with DDL
  4. 4Querying and Modifying Data with DML
  5. 5Aggregates, Joins, and Subqueries
  6. 6Indexes, Views, and Advanced Features
  7. 7Transactions, ACID, and Normalization
  8. 8SQL versus NoSQL and Database Security

Drill it

Reading is not remembering. These come from the SQL And Databases deck:

Q

What is a database?

A database is an organized collection of structured data stored and accessed electronically from a computer system. Databases allow efficient storage, retrieval...

Q

What is a Relational Database Management System (RDBMS)?

An RDBMS is a software system that manages relational databases, organizing data into tables with rows and columns related by keys. It supports SQL for querying...

Q

What are the main types of databases?

Databases are broadly classified into relational (SQL-based, like MySQL) and non-relational (NoSQL, like MongoDB for documents or Cassandra for wide-column stor...

Q

What is SQL?

SQL stands for Structured Query Language, a standard language for managing and manipulating relational databases. It includes sublanguages like DDL for schema d...