0Pricing
Supabase Backend as a Service · บทเรียน

การทำดัชนีฐานข้อมูลเพื่อประสิทธิภาพ

ทำความเข้าใจความสำคัญของดัชนี วิธีสร้างดัชนีที่มีประสิทธิภาพ และวิเคราะห์แผนการสืบค้นเพื่อเพิ่มประสิทธิภาพการอ่านฐานข้อมูล

การทำดัชนีฐานข้อมูลเพื่อประสิทธิภาพ เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 3 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทำดัชนีฐานข้อมูลเพื่อประสิทธิภาพ”

ทำความเข้าใจความสำคัญของดัชนี วิธีสร้างดัชนีที่มีประสิทธิภาพ และวิเคราะห์แผนการสืบค้นเพื่อเพิ่มประสิทธิภาพการอ่านฐานข้อมูล คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Supabase Backend as a Service หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Supabase Backend as a Service บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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