انتقل إلى المحتوى
أمين أبوزيد
MID-013

تحسين الاستعلامات

  • 8 درس
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • EXPLAIN ANALYZE
  • خطط التنفيذ
  • الانتقائية (selectivity)
  • الإحصائيات

تعلّم أولًا

4 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Read EXPLAIN (ANALYZE, BUFFERS) output: scan types, join algorithms, estimates vs actuals.
  2. Explain selectivity, statistics, and why the planner chooses a sequential scan.
  3. Find slow queries with pg_stat_statements and fix them by rewriting or indexing.
  4. Recognize ORM-generated queries in plans and fix them at the source.

الدروس

  1. 01كيف يفكّر المخطِّط (Planner)
  2. 02قراءة EXPLAIN ANALYZE
  3. 03خوارزميات الربط
  4. 04الإحصائيات والتقديرات
  5. 05إيجاد الاستعلامات البطيئة باستخدام pg_stat_statements
  6. 06إعادة كتابة استعلامات تفيد الأداء
  7. 07عرض عملي: من 4 ثوانٍ إلى 40 ms
  8. 08عرض عملي: تقدير خاطئ وكيف تصلحه الإحصائيات

المختبر والتمارين

Lab

MID-013-LAB1 · Slow query clinic

Outcomes: 1–4 · Time: ~3 h On a loaded Shop database (10 M orders), identify the five slowest statements with pg_stat_statements, including one ORM-generated query (N+1 or SELECT * from a repository). Fix each by rewriting, indexing, or changing the application code. Acceptance: before/after EXPLAIN (ANALYZE, BUFFERS) plans and timings recorded for each of the five.

Exercises (assessment: plan-reading test)

IDOutcomeTask
MID-013-EX011Annotate a given plan: scan types, join algorithm, the node with the largest time, estimate vs actual gaps.
MID-013-EX021Explain what loops=1000 and Buffers: shared read=50000 tell you.
MID-013-EX032Explain why WHERE active = true (95% true) uses a sequential scan, and what would change for active = false.
MID-013-EX042Fix a bad estimate on correlated columns using CREATE STATISTICS.
MID-013-EX053Reset pg_stat_statements, run a mixed workload, and list the top three queries by total time.
MID-013-EX063Rewrite WHERE date_trunc('day', created_at) = ... and a deep OFFSET query into index-friendly forms.
MID-013-EX074Show the SQL a Spring Data repository method generates (SQL logging) and fix an N+1 with a join fetch or projection.
أظهر الحلول
  • EX01 Name the nodes bottom-up; the slowest node is the one whose actual time minus children is largest; flag nodes where estimated rows differ from actual by 10x or more.
  • EX02 The node ran 1000 times (multiply per-loop time by 1000); about 50 000 pages (about 400 MB at 8 kB) were read from outside shared buffers.
  • EX03 An index would not narrow 95% of the rows, so the sequential scan is cheaper; for false (5%) an index or partial index can win.
  • EX04 CREATE STATISTICS s (dependencies) ON a, b FROM t; ANALYZE t; then re-check estimates.
  • EX05 SELECT pg_stat_statements_reset(); then query the view ordered by total_exec_time.
  • EX06 Half-open range on the raw column; keyset pagination WHERE (created_at, id) < (?, ?).
  • EX07 spring.jpa.show-sql or SQL logger; replace lazy loop with JOIN FETCH, entity graph, or a DTO projection.
  • The "EXPLAIN without ANALYZE" mistake is exercised in EX01 and EX02.

الاختبار

Outcomes: O1 reading plans · O2 selectivity and statistics · O3 finding and fixing slow queries · O4 ORM queries.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1EXPLAIN (ANALYZE) differs from EXPLAIN because it:O1
  2. 2A big gap between rows= and actual rows= usually indicates:O1
  3. 3The join best suited to a few outer rows with an indexed inner lookup:O1
  4. 4A sequential scan is correct when:O2
  5. 5Correlated columns are mis-estimated because the planner assumes:O2
  6. 6After a bulk load you should run:O2
  7. 7In pg_stat_statements, to find the biggest overall cost sort by:O3
  8. 8Which is index-friendly?O3
  9. 9Better than deep OFFSET pagination:O3
  10. 10An ORM N+1 problem appears in logs as:O4

الجزء العملي

Plan-reading test: given three plans, name the bottleneck node and the fix; plus exercises MID-013-EX01 to EX07 and the lab. Pass: quiz 8/10, test bottlenecks identified correctly, and lab before/after evidence for all five queries.

أخطاء شائعة

EXPLAIN without ANALYZE; functions on indexed columns; OFFSET pagination over deep pages.

التقييم

Plan-reading test.

الصلة بالمسار الوظيفي

The MID graduation gate's performance bottleneck.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.