كل المقررات

الرئيسية / مقرر قاعدة البيانات / الفصل 11 الجزء 2

الفصل 11 (الجزء 2): Query Processing and Optimization

Heuristic and cost-based optimization

على طول: إزاي الـ optimizer بيحسّن الخطة: قواعد عامة وتقدير تكلفة.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

اختيار الخطة الأرخص

أمثلة SQL

أمثلة قصيرة وبسيطة

الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.

المثال ١: قبل الـ index

Sample output (press Run to produce your own):
id | parent | notused | detail                                      
---+--------+---------+---------------------------------------------
3  | 0      | 0       | SCAN e                                      
5  | 0      | 0       | SEARCH d USING INTEGER PRIMARY KEY (rowid=?)
(2 rows)

المثال ٢: بعد index على emp(dept_id)

Sample output (press Run to produce your own):
id | parent | notused | detail                                       
---+--------+---------+----------------------------------------------
4  | 0      | 0       | SCAN d                                       
8  | 0      | 0       | SEARCH e USING INDEX idx_emp_dept (dept_id=?)
(2 rows)

المثال ٣: ANALYZE

Sample output (press Run to produce your own):
tbl  | idx          | stat   
-----+--------------+--------
dept | NULL         | 3      
emp  | idx_emp_dept | 600 200
(2 rows)

افتح الموضوع المقابل في صفحة LiU TDDD12

اختبار قصير

اختبر فهمك

1. Why push selections down the tree?

2. A cost-based optimizer uses:

3. A Cartesian product followed by a selection should become a:

4. The first thing to check in a slow plan:

5. Which command refreshes statistics in SQLite?

تمارين برمجة

جرّب بإيدك

التمرين 1

اعمل index على emp(city) واعرض الـ plan لـ city = 'Cairo'.

Create an index on emp(city) and show the plan for city = 'Cairo'.

(output appears here)

التمرين 2

شغّل ANALYZE بعد إنشاء index على city واعرض الإحصائيات.

Run ANALYZE after creating an index on city and show the statistics.

(output appears here)

مشروع صغير في آخر المحاضرة

حسّن استعلام بطيء

الداتا: Generated sample data: 600 employees in 3 departments. Written for this course (our own data, free to reuse).

License / source: Our own sample data; SQLite is in the public domain. Dataset page

الخطوات

  1. الاستعلام: أسماء موظفي Sales.
  2. اعرض الـ plan قبل أي index.
  3. اعمل index على emp(dept_id) واعرض الـ plan تاني.
  4. اعرض عدد موظفي Sales. المتوقع: 200.
Expected output (press Run to produce your own):
id | parent | notused | detail                                      
---+--------+---------+---------------------------------------------
3  | 0      | 0       | SCAN e                                      
5  | 0      | 0       | SEARCH d USING INTEGER PRIMARY KEY (rowid=?)
(2 rows)
id | parent | notused | detail                                       
---+--------+---------+----------------------------------------------
4  | 0      | 0       | SCAN d                                       
8  | 0      | 0       | SEARCH e USING INDEX idx_emp_dept (dept_id=?)
(2 rows)
n  
---
200
(1 row)

مشروع نهاية الفصل

ملف المشروع العملي للفصل 11

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

Credits: the topic order follows the lecture list of the course TDDD12 Database Technology at Linköping University (https://www.ida.liu.se/~TDDD12/fo/), whose reading guide is based on the textbook Fundamentals of Database Systems by Elmasri and Navathe. We found no reuse licence on that page, so we link to it and copy nothing from it: the explanations, diagrams, data and code in this lecture are our own. Reference: Elmasri and Navathe, Fundamentals of Database Systems, the chapters on query processing and optimization.