ضبط تكلفة الفهارس باستخدام ANALYZE والإحصاءات
تعلّم كيف يعتمد مخطّط PostgreSQL على إحصاءات الجداول لاختيار الفهارس، وكيف تحافظ على حداثة هذه الإحصاءات لتظل خطط الاستعلامات سريعة.
ضبط تكلفة الفهارس باستخدام ANALYZE والإحصاءات درس مجاني في Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Advanced PostgreSQL: Indexing, Partitioning, Replication، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Planner Needs Data About Data
PostgreSQL's planner picks between index scans and sequential scans using statistics about your tables: row counts, value distributions, and more.
Stale statistics lead to bad plans.
What ANALYZE Does
The ANALYZE command samples a table and updates statistics stored in the system catalogs, so the planner estimates row counts accurately.
ANALYZE orders;Autovacuum and Autoanalyze
The autovacuum daemon also runs ANALYZE automatically when enough rows change. But heavy bulk loads may need a manual ANALYZE right away.
Reading Estimates vs Actuals
Use EXPLAIN ANALYZE to compare the planner's estimated rows with actual rows. A big gap signals stale or insufficient statistics.
EXPLAIN ANALYZE SELECT * FROM orders WHERE status = 'paid';Statistics Target
The default_statistics_target controls sample size. Raise it on columns with skewed data for sharper histograms.
ALTER TABLE orders ALTER COLUMN status SET STATISTICS 500;Why Bad Estimates Hurt
If the planner underestimates matching rows it may pick an index scan that becomes slow; overestimate and it may skip a perfectly good index for a seq scan.
Inspecting pg_stats
The pg_stats view exposes the collected statistics like most common values and null fraction per column.
SELECT attname, n_distinct, null_frac FROM pg_stats WHERE tablename = 'orders';Extended Statistics
For correlated columns, create extended statistics so the planner understands dependencies between them.
CREATE STATISTICS orders_corr (dependencies) ON city, zip FROM orders;Cost Parameters
Settings like random_page_cost tell the planner how expensive random I/O is. On SSDs, lowering it makes index scans more attractive.
SET random_page_cost = 1.1;When the Index Is Ignored
If an index exists but is unused, suspect:
- Stale statistics
- Low selectivity (most rows match)
- A type mismatch preventing index use
A Tuning Workflow
- Run EXPLAIN ANALYZE
- Check estimate vs actual gap
- ANALYZE or raise statistics target
- Adjust cost settings if needed
- Re-measure
Quick Check
Test your statistics tuning knowledge.
Recap
The PostgreSQL planner chooses indexes based on statistics kept fresh by ANALYZE and autovacuum.
Compare estimated versus actual rows with EXPLAIN ANALYZE, raise the statistics target for skewed columns, add extended statistics for correlated ones, and tune cost parameters to guide index choice.
الأسئلة الشائعة
هل درس «ضبط تكلفة الفهارس باستخدام ANALYZE والإحصاءات» مجاني؟
نعم — نص درس «ضبط تكلفة الفهارس باستخدام ANALYZE والإحصاءات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Advanced PostgreSQL: Indexing, Partitioning, Replication، انتقل إلى CoddyKit PRO. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
ماذا ستتعلم في «ضبط تكلفة الفهارس باستخدام ANALYZE والإحصاءات»؟
تعلّم كيف يعتمد مخطّط PostgreSQL على إحصاءات الجداول لاختيار الفهارس، وكيف تحافظ على حداثة هذه الإحصاءات لتظل خطط الاستعلامات سريعة. تتمرن على Advanced PostgreSQL: Indexing, Partitioning, Replication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Advanced PostgreSQL: Indexing, Partitioning, Replication؟
لا تُشترط خبرة سابقة. Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «ضبط تكلفة الفهارس باستخدام ANALYZE والإحصاءات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Advanced PostgreSQL: Indexing, Partitioning, Replication هذا؟
نعم. كل درس في Advanced PostgreSQL: Indexing, Partitioning, Replication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تحليل خطط الاستعلام باستخدام EXPLAIN
- مراقبة استخدام الفهارس
- إعادة فهرسة الفهارس وصيانتها
- ضبط تكلفة الفهارس باستخدام ANALYZE والإحصاءات