0Pricing
Supabase Backend as a Service · 课时

用于性能优化的数据库索引

了解索引的重要性、如何创建有效的索引,以及如何分析查询计划来提升数据库读取性能。

用于性能优化的数据库索引 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

Boost Database Performance

Imagine searching for a specific topic in a massive textbook without an index. You'd flip through every page, right?

  • Databases face a similar challenge when retrieving data.
  • Without help, they might scan every single row to find what you need.
  • This lesson explores database indexing: a powerful technique to dramatically speed up data retrieval.

How Indexes Work

A database index is like a book's index. It's a special lookup table that the database search engine can use to speed up data retrieval.

  • It contains a sorted list of values from one or more columns.
  • Each value points directly to the location of the full row of data.
  • This allows the database to quickly jump to the relevant data, rather than scanning the entire table.

When to Use Indexes

Indexes are most effective on columns frequently used for:

  • Filtering (WHERE clauses): Finding specific rows quickly.
  • Sorting (ORDER BY clauses): Retrieving data in a particular order efficiently.
  • Joining (JOIN conditions): Matching rows between tables faster.

Columns with high cardinality (many unique values) are generally good candidates.

Creating Your First Index

Let's create a simple table and then add an index to one of its columns. We'll index the email column, which might be used often for lookups.

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL
);

CREATE INDEX idx_users_email ON users (email);

The Impact on Queries

After creating the index on email, a query searching for a user by their email will be significantly faster, especially in large tables. The database can now use the index to find the row directly.

SELECT id, name FROM users WHERE email = 'alice@example.com';

Indexing's Hidden Costs

While indexes boost read performance, they come with trade-offs:

  • Disk Space: Indexes require extra storage space.
  • Write Overhead: Every time you INSERT, UPDATE, or DELETE a row, the index must also be updated. This adds a small performance cost to write operations.

Don't over-index! Only index columns that genuinely benefit from it.

Introducing Query Plans with EXPLAIN

How do you know if your index is actually being used? PostgreSQL provides the EXPLAIN command to show you the query plan – how the database intends to execute your query.

  • It helps you understand the steps involved and identify potential bottlenecks.
  • This is crucial for optimizing complex queries.

Reading a Basic EXPLAIN Output

When you run EXPLAIN, look for terms like Seq Scan (sequential scan, meaning no index was used) versus Index Scan (index was used).

EXPLAIN SELECT id, name FROM users WHERE email = 'bob@example.com';

Deeper Dive with EXPLAIN ANALYZE

To get even more detail, use EXPLAIN ANALYZE. This not only shows the planned execution but also actually runs the query and provides real-world statistics, including execution time and the number of rows processed.

EXPLAIN ANALYZE SELECT id, name FROM users WHERE email = 'charlie@example.com';

Indexing Knowledge Check

You've learned about database indexing and how to analyze query plans. Now, let's test your understanding.

Indexing for Speed: Recap

Great job! You've learned the fundamentals of database indexing:

  • Indexes significantly improve SELECT query performance.
  • They work like a book's index, allowing fast data lookups.
  • Create indexes on columns used in WHERE, ORDER BY, and JOIN clauses.
  • Be mindful of the overhead on write operations and disk space.
  • Use EXPLAIN and EXPLAIN ANALYZE to understand query plans and verify index usage.

Mastering indexing is key to building high-performance database applications!

常见问题解答

「用于性能优化的数据库索引」课时是免费的吗?

是的 — 「用于性能优化的数据库索引」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 3 节课。

「用于性能优化的数据库索引」这节课中我会学到什么?

了解索引的重要性、如何创建有效的索引,以及如何分析查询计划来提升数据库读取性能。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Supabase Backend as a Service 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。

「用于性能优化的数据库索引」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?

能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 高级 SQL 查询与连接
  2. 用于性能优化的数据库索引
  3. 数据库函数与触发器
← 返回 Supabase Backend as a Service