فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: Range query بـ B-tree
Sample output (press Run to produce your own): id | parent | notused | detail ---+--------+---------+-------------------------------------------------------------- 2 | 0 | 0 | SEARCH person USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (1 row) id | name ----+-------- 100 | user100 101 | user101 102 | user102 103 | user103 104 | user104 105 | user105 (6 rows)
المثال ٢: الترتيب مجاناً من الـ index
Sample output (press Run to produce your own): id | parent | notused | detail ---+--------+---------+------------------------------------------ 4 | 0 | 0 | SCAN person USING COVERING INDEX idx_name (1 row) name ------- user1 user10 user100 (3 rows)
المثال ٣: LIKE مقابل مدى (range) على الـ index
Sample output (press Run to produce your own): id | parent | notused | detail ---+--------+---------+------------ 2 | 0 | 0 | SCAN person (1 row) id | parent | notused | detail ---+--------+---------+------------------------------------------------------- 3 | 0 | 0 | SEARCH person USING INDEX idx_city (city>? AND city<?) (1 row)
اختبار قصير
اختبر فهمك
1. Each node of a B-tree is usually:
2. When a B-tree leaf is full, we:
3. Why are B+ trees good for ranges?
4. Hashing is best for:
5. Where do all B-tree leaves sit?
تمارين برمجة
جرّب بإيدك
التمرين 1
اعرض الـ plan لـ id > 990 وبعدين اعرض الصفوف.
Show the plan for id > 990 and then show the rows.
(output appears here)
sql('''
CREATE TABLE person (id INTEGER PRIMARY KEY, name TEXT, city TEXT);
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM n WHERE i < 1000)
INSERT INTO person SELECT i, 'user' || i, CASE i % 4 WHEN 0 THEN 'Cairo' WHEN 1 THEN 'Giza' WHEN 2 THEN 'Alex' ELSE 'Luxor' END FROM n;
EXPLAIN QUERY PLAN SELECT id FROM person WHERE id > 990;
SELECT id FROM person WHERE id > 990;
''')
Output
id | parent | notused | detail ---+--------+---------+-------------------------------------------------- 2 | 0 | 0 | SEARCH person USING INTEGER PRIMARY KEY (rowid>?) (1 row) id ---- 991 992 993 994 995 996 997 998 999 1000 (10 rows)
التمرين 2
قارن الـ plan بين city LIKE 'Gi%' وبين المدى city >= 'Gi' AND city < 'Gj' مع index على city. هتلاقي الأول SCAN لأن LIKE في SQLite مش بيفرّق بين الحروف الكبيرة والصغيرة.
Compare the plan of city LIKE 'Gi%' with the range city >= 'Gi' AND city < 'Gj', with an index on city. In SQLite LIKE ignores letter case, so the first one scans.
(output appears here)
sql('''
CREATE TABLE person (id INTEGER PRIMARY KEY, name TEXT, city TEXT);
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM n WHERE i < 1000)
INSERT INTO person SELECT i, 'user' || i, CASE i % 4 WHEN 0 THEN 'Cairo' WHEN 1 THEN 'Giza' WHEN 2 THEN 'Alex' ELSE 'Luxor' END FROM n;
CREATE INDEX idx_city ON person(city);
EXPLAIN QUERY PLAN SELECT * FROM person WHERE city LIKE 'Gi%';
EXPLAIN QUERY PLAN SELECT * FROM person WHERE city >= 'Gi' AND city < 'Gj';
''')
Output
id | parent | notused | detail ---+--------+---------+------------ 2 | 0 | 0 | SCAN person (1 row) id | parent | notused | detail ---+--------+---------+------------------------------------------------------- 3 | 0 | 0 | SEARCH person USING INDEX idx_city (city>? AND city<?) (1 row)
مشروع صغير في آخر المحاضرة
اختار الـ indexes المناسبة لتقرير
الداتا: Generated sample data: 1000 people in 4 cities. Written for this course (our own data, free to reuse).
License / source: Our own sample data; SQLite is in the public domain. Dataset page
الخطوات
- الاستعلام: كل الناس في Cairo مرتبين بالاسم.
- اعرض الـ plan من غير index.
- اعمل index مركّب (city, name) واعرض الـ plan.
- اعرض أول 3 أسماء. المتوقع: user100 و user1000 و user104.
Expected output (press Run to produce your own): id | parent | notused | detail ---+--------+---------+----------------------------- 3 | 0 | 0 | SCAN person 11 | 0 | 0 | USE TEMP B-TREE FOR ORDER BY (2 rows) id | parent | notused | detail ---+--------+---------+---------------------------------------------------------- 3 | 0 | 0 | SEARCH person USING COVERING INDEX idx_city_name (city=?) (1 row) name -------- user100 user1000 user104 (3 rows)
مشروع نهاية الفصل
ملف المشروع العملي للفصل 8
ملف 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 file organization, indexing and hashing.