فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة 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)
اختبار قصير
اختبر فهمك
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)
sql('''
CREATE TABLE dept (dept_id INTEGER PRIMARY KEY, label TEXT);
CREATE TABLE emp (id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER, city TEXT);
INSERT INTO dept VALUES (1,'IT'), (2,'Sales'), (3,'HR');
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM n WHERE i < 600)
INSERT INTO emp SELECT i, 'emp' || i, 1 + i % 3, CASE WHEN i % 5 = 0 THEN 'Cairo' ELSE 'Giza' END FROM n;
CREATE INDEX idx_emp_city ON emp(city);
EXPLAIN QUERY PLAN SELECT * FROM emp WHERE city = 'Cairo';
''')
Output
id | parent | notused | detail ---+--------+---------+--------------------------------------------- 3 | 0 | 0 | SEARCH emp USING INDEX idx_emp_city (city=?) (1 row)
التمرين 2
شغّل ANALYZE بعد إنشاء index على city واعرض الإحصائيات.
Run ANALYZE after creating an index on city and show the statistics.
(output appears here)
sql('''
CREATE TABLE dept (dept_id INTEGER PRIMARY KEY, label TEXT);
CREATE TABLE emp (id INTEGER PRIMARY KEY, name TEXT, dept_id INTEGER, city TEXT);
INSERT INTO dept VALUES (1,'IT'), (2,'Sales'), (3,'HR');
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM n WHERE i < 600)
INSERT INTO emp SELECT i, 'emp' || i, 1 + i % 3, CASE WHEN i % 5 = 0 THEN 'Cairo' ELSE 'Giza' END FROM n;
CREATE INDEX idx_emp_city ON emp(city);
ANALYZE;
SELECT tbl, idx, stat FROM sqlite_stat1 ORDER BY idx;
''')
Output
tbl | idx | stat -----+--------------+-------- dept | NULL | 3 emp | idx_emp_city | 600 300 (2 rows)
مشروع صغير في آخر المحاضرة
حسّن استعلام بطيء
الداتا: 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
الخطوات
- الاستعلام: أسماء موظفي Sales.
- اعرض الـ plan قبل أي index.
- اعمل index على emp(dept_id) واعرض الـ plan تاني.
- اعرض عدد موظفي 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 فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.
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.