كل المقررات

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

الفصل 8 (الجزء 2): Data Structures: Files and Indexes

B-trees and hashing

على طول: الـ B-tree هو الشجرة اللي ورا أغلب الـ indexes، والـ hashing للبحث بالمساواة.

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

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

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

رسوم متحركة

البحث في B-tree

أمثلة 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)

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

اختبار قصير

اختبر فهمك

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)

التمرين 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)

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

اختار الـ 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

الخطوات

  1. الاستعلام: كل الناس في Cairo مرتبين بالاسم.
  2. اعرض الـ plan من غير index.
  3. اعمل index مركّب (city, name) واعرض الـ plan.
  4. اعرض أول 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 فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (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.