Skip to content

索引

一句话解释

索引是 数据结构那个制作查找行 通过列 much faster, 如 book's 索引 for 快速 lookup.

用大白话说

An index is a separate data structure that the database maintains to find rows quickly. Without an index, the database must scan every row in a table (a "完整表 scan") to find matches. With an index, it can jump directly to the relevant rows — like using a book's index instead of flipping through every page.

Indexes are created on columns that are frequently searched, joined, or used in WHERE clauses. The most common type is a B-tree index, which keeps values sorted for fast lookups and range queries.

The trade-off is that indexes take up extra storage and slow down writes (INSERT, UPDATE, DELETE) because the index must be updated too. You should index columns you query often, but not every column.

示例

sql
-- Create an index on the email column
CREATE INDEX idx_users_email ON users(email);

-- This query is now fast:
SELECT * FROM users WHERE email = '[email protected]';

不使用索引, 数据库 scans 每个行. 使用索引, 它查找 matching 行 instantly.

常见混淆

  • 容易混淆的对象: 主键

    关键区别: 主键是 uniqueness constraint; 索引是 性能结构. 主要键 automatically 获取索引, 但你 创建 other 索引 manually for 性能.

相关术语

相关资源

相关课程

  • 索引

    学习如何索引速度上 查询.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表