跳转到主要内容
eLearner.app
模块 10 · 第 2 课(共 4)课程中的38/57~15 min
模块课程(2/4)

解释和分析

Have you ever wondered how the database chooses whether to use that nice index you just created? As soon as you hit Enter on a query (say, a long join with a chained group by), the Query Planner computes dozens of strategies on the fly to carry the query out and picks the one believed, in theory, to be fastest.

But as a developer, you need to be able to look inside the dark mind of the Query Planner.

EXPLAIN

To discover how the Planner intends to proceed, just put the magic word EXPLAIN at the start of your query.

SQL
EXPLAIN SELECT * FROM orders WHERE status = 'delivered';

The result won't be the orders! Instead, you'll see the plan in output (with estimated statistics):

Code
Seq Scan on orders  (cost=0.00..25.88 rows=15 width=40)
  Filter: (status = 'delivered'::text)

This tells us:

  1. The Planner will use a Seq Scan (brute, sequential read of the entire disk). Bad if there are billions of orders.
  2. It estimates ~15 rows will be returned, and computes the "cost" in pre-loaded CPU cycles.

EXPLAIN ANALYZE

While EXPLAIN relies on algorithmic guesses (based on global statistics computed in the last few hours over the data), EXPLAIN ANALYZE actually runs the query for real (and waits for the full logical results), comparing the real elapsed time in mathematically absolute terms.

SQL
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 90;
Code
Index Scan using idx_orders_cust on orders  (cost=0.15..8.13 rows=4 width=40) (actual time=0.012..0.015 rows=4 loops=1)
  Index Cond: (customer_id = 90)
Planning Time: 0.081 ms
Execution Time: 0.033 ms

Hooray! Here you learn that the database has decided to apply an Index Scan on our secondary idx_orders_cust index, taking a microscopic execution time of a thousandth of a second, skipping millions of rows on the fly.

Your turn

锻炼#sql.m10.l2.e1
尝试:0加载中...

Write a basic EXPLAIN command to discover how PostgreSQL intends to find products with a current stock below 10 units. (Do not use ANALYZE in this step.)

正在加载编辑器...
显示提示

Just prepend the word EXPLAIN at the start of the existing statement.

3 次尝试后可用的解决方案

Getting truthful metrics

锻炼#sql.m10.l2.e2
尝试:0加载中...

We want a real performance diagnosis (timings and cycles), not a mathematically guessed sketch.\nUse the dedicated statement to study, by actually running it, the lookup of ALL 'customers' signed up before the 1st of January 2025 ('signed_up_on' < '2025-01-01').

正在加载编辑器...
显示提示

Add the word ANALYZE right after EXPLAIN.

3 次尝试后可用的解决方案