0Pricing
PostgreSQL Performance & Query Optimization · บทเรียน

การใช้มุมมองแบบจัดเก็บเพื่อเพิ่มประสิทธิภาพ

ค้นพบว่ามุมมองแบบจัดเก็บช่วยคำนวณผลลัพธ์คำสั่งค้นหาที่ซับซ้อนไว้ล่วงหน้า เพื่อเร่งการจัดทำรายงานและการวิเคราะห์ได้อย่างไร

การใช้มุมมองแบบจัดเก็บเพื่อเพิ่มประสิทธิภาพ เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Materialized Views?

Welcome to the lesson on Materialized Views! These are powerful tools in PostgreSQL for speeding up complex queries.

Think of a Materialized View (MV) as a pre-computed result set of a query that is stored on disk. Unlike a regular view, which runs its query every time you access it, an MV holds the actual data.

Why Use Materialized Views?

Materialized Views are incredibly useful for performance, especially when dealing with:

  • Complex Joins: Queries involving many tables.
  • Aggregations: Calculations like sums, averages, or counts over large datasets.
  • Reporting & Analytics: Dashboards and reports that frequently query the same complex data.

By pre-calculating and storing these results, MVs can drastically reduce query execution time for repeated requests.

Creating Your First MV

You create a Materialized View using the CREATE MATERIALIZED VIEW statement, followed by a SELECT query. The query defines the data that will be stored in your MV.

Let's create a simple Materialized View to see the average price per category from a products table.

Hands-on MV Creation

Try running this example. It sets up a small products table, inserts some data, and then creates a Materialized View named category_avg_price based on that data.

-- Setup table
CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    category VARCHAR(50),
    price DECIMAL(10, 2)
);

-- Insert data
INSERT INTO products (name, category, price) VALUES
('Laptop', 'Electronics', 1200.00),
('Mouse', 'Electronics', 25.00),
('Keyboard', 'Electronics', 75.00),
('Desk Chair', 'Furniture', 150.00),
('Lamp', 'Furniture', 40.00);

-- Create Materialized View
CREATE MATERIALIZED VIEW category_avg_price AS
SELECT category, AVG(price) AS average_price, COUNT(*) AS product_count
FROM products
GROUP BY category
ORDER BY category;

-- Select from MV
SELECT * FROM category_avg_price;

Keeping Data Fresh

A key difference from regular views is that Materialized Views do not update automatically when the underlying data changes. You need to explicitly refresh them.

Use the REFRESH MATERIALIZED VIEW command to update the data in an MV. For large MVs, you can add CONCURRENTLY to allow other queries to access the view while it's refreshing.

`REFRESH MATERIALIZED VIEW` Demo

Let's add a new product to our products table and then refresh our category_avg_price Materialized View. Notice how the MV shows the old data until it's refreshed.

-- Add new data to the base table
INSERT INTO products (name, category, price) VALUES
('Monitor', 'Electronics', 300.00);

-- Check MV (will NOT show new data yet)
SELECT * FROM category_avg_price;

-- Refresh the Materialized View
REFRESH MATERIALIZED VIEW category_avg_price;

-- Check MV again (NOW shows new data)
SELECT * FROM category_avg_price;

MV vs. Standard Views

It's important to understand the core differences between Materialized Views and standard views:

  • Standard Views: Are essentially stored queries. They don't store data themselves; they execute their defining query every time they are accessed.
  • Materialized Views: Store the result of their defining query as actual data on disk. This makes reading from them much faster, but they require manual (or scheduled) refreshing.

Choose MVs when performance is critical for complex, static-ish data.

When to Choose Materialized Views

Materialized Views are ideal for:

  • Batch Reporting: Generating daily, weekly, or monthly reports that don't need real-time data.
  • Data Warehousing: Pre-aggregating data for faster analytical queries.
  • External Dashboards: Providing quick access to complex metrics for BI tools.
  • Static Data: When the underlying tables don't change very frequently, minimizing refresh overhead.

Avoid MVs for highly transactional, real-time data that needs immediate updates.

MV Knowledge Check

Consider a Materialized View named daily_sales_summary that aggregates sales data from a transactions table.

If new transactions are added to the transactions table, what must you do for daily_sales_summary to reflect these new sales?

Materialized Views: Recap

In this lesson, you've learned about Materialized Views in PostgreSQL.

  • They are pre-computed query results stored on disk, offering significant performance gains for complex, repetitive queries.
  • You create them with CREATE MATERIALIZED VIEW.
  • They require explicit refreshing using REFRESH MATERIALIZED VIEW to update their data.
  • They are perfect for reporting, analytics, and data warehousing where real-time updates aren't critical.

Keep practicing with MVs to master this powerful optimization technique!

คำถามที่พบบ่อย

บทเรียน “การใช้มุมมองแบบจัดเก็บเพื่อเพิ่มประสิทธิภาพ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การใช้มุมมองแบบจัดเก็บเพื่อเพิ่มประสิทธิภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

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

ค้นพบว่ามุมมองแบบจัดเก็บช่วยคำนวณผลลัพธ์คำสั่งค้นหาที่ซับซ้อนไว้ล่วงหน้า เพื่อเร่งการจัดทำรายงานและการวิเคราะห์ได้อย่างไร คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การใช้มุมมองแบบจัดเก็บเพื่อเพิ่มประสิทธิภาพ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การปรับแต่งฟังก์ชันรวมและฟังก์ชันหน้าต่าง
  2. CTE แบบเรียกซ้ำและคำสั่งค้นหากราฟ
  3. การใช้มุมมองแบบจัดเก็บเพื่อเพิ่มประสิทธิภาพ
  4. การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข
← กลับไปที่ PostgreSQL Performance & Query Optimization