0Pricing
PostgreSQL Performance & Query Optimization · درس

قراءة تقديرات التكلفة وأعداد الصفوف في EXPLAIN

تعلّم تفسير أرقام التكلفة والصفوف المقدّرة وقيم العرض التي يربطها EXPLAIN بكل عقدة في الخطة، وكيفية اكتشاف أخطاء تقديرات المخطّط

قراءة تقديرات التكلفة وأعداد الصفوف في EXPLAIN درس مجاني في PostgreSQL Performance & Query Optimization على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في PostgreSQL Performance & Query Optimization، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة PostgreSQL Performance & Query Optimization 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What the Cost Numbers Mean

Every node in an EXPLAIN plan shows a cost=startup..total pair. These are arbitrary planner units, not milliseconds. The planner uses them only to compare alternative plans and pick the cheapest one.

Startup vs Total Cost

The two numbers tell different stories:

  • Startup cost: work before the first row is returned (e.g. building a hash table)
  • Total cost: work to return all rows

A node that must sort or aggregate everything has a high startup cost.

A Sample Plan

Run EXPLAIN on a simple query and read the cost on each line.

EXPLAIN SELECT * FROM orders WHERE total > 100;

The rows Estimate

Each node shows rows=N, the planner's estimated number of rows it will emit. The planner derives this from table statistics gathered by ANALYZE.

Bad estimates lead to bad plans, so this number matters a lot.

The width Value

The width=N field is the estimated average row size in bytes. Multiplied by rows it estimates memory and I/O needs, which influences whether sorts or hashes spill to disk.

Estimated vs Actual

EXPLAIN alone shows only estimates. Add ANALYZE to also run the query and see real timings and real row counts side by side.

EXPLAIN ANALYZE SELECT * FROM orders WHERE total > 100;

Spotting Estimate Errors

Compare rows (estimate) with actual rows in EXPLAIN ANALYZE output. A large gap — say estimate 10 but actual 100000 — signals stale or insufficient statistics.

Why Estimates Drift

Estimates go stale when:

  • Data changed a lot since the last ANALYZE
  • Columns are correlated and the planner assumes independence
  • The default statistics target is too low for skewed data

Refreshing Statistics

Run ANALYZE to recompute statistics for a table. Better stats produce better row estimates and therefore better plans.

ANALYZE orders;

Raising the Statistics Target

For columns with skewed distributions, increase how many distinct values are sampled. A higher target means more accurate estimates at the cost of slightly slower ANALYZE.

ALTER TABLE orders ALTER COLUMN total SET STATISTICS 500;
ANALYZE orders;

Cost Multipliers in postgresql.conf

Constants like seq_page_cost, random_page_cost, and cpu_tuple_cost scale how the planner weighs disk vs CPU. On SSDs, lowering random_page_cost makes index scans look cheaper.

Quick Check

Test your understanding of cost estimates.

Recap

You learned to read EXPLAIN's numbers:

  • Cost is in arbitrary units; startup vs total cost differ
  • rows and width are estimates from statistics
  • Compare estimated vs actual rows to spot bad stats
  • Fix drift with ANALYZE and a higher statistics target
  • Cost constants tune disk-vs-CPU weighting

الأسئلة الشائعة

هل درس «قراءة تقديرات التكلفة وأعداد الصفوف في EXPLAIN» مجاني؟

نعم — نص درس «قراءة تقديرات التكلفة وأعداد الصفوف في EXPLAIN» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة PostgreSQL Performance & Query Optimization، انتقل إلى CoddyKit PRO. تتضمن دورة PostgreSQL Performance & Query Optimization 4 دروس في المجموع.

ماذا ستتعلم في «قراءة تقديرات التكلفة وأعداد الصفوف في EXPLAIN»؟

تعلّم تفسير أرقام التكلفة والصفوف المقدّرة وقيم العرض التي يربطها EXPLAIN بكل عقدة في الخطة، وكيفية اكتشاف أخطاء تقديرات المخطّط تتمرن على PostgreSQL Performance & Query Optimization مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ PostgreSQL Performance & Query Optimization؟

لا تُشترط خبرة سابقة. PostgreSQL Performance & Query Optimization على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «قراءة تقديرات التكلفة وأعداد الصفوف في EXPLAIN»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس PostgreSQL Performance & Query Optimization هذا؟

نعم. كل درس في PostgreSQL Performance & Query Optimization يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى EXPLAIN وANALYZE
  2. تفسير عقد الخطة
  3. تحديد نقاط اختناق الأداء
  4. قراءة تقديرات التكلفة وأعداد الصفوف في EXPLAIN
← العودة إلى PostgreSQL Performance & Query Optimization