كل المقررات

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

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

Disk, blocks and indexes

على طول: الداتا متخزنة إزاي على الهارد، وليه الـ index بيسرّع البحث.

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

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

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

رسوم متحركة

البحث بـ index مقابل بدون index

أمثلة SQL

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

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

المثال ١: بدون index

Sample output (press Run to produce your own):
id | parent | notused | detail     
---+--------+---------+------------
2  | 0      | 0       | SCAN person
(1 row)

المثال ٢: بعد CREATE INDEX

Sample output (press Run to produce your own):
id | parent | notused | detail                                     
---+--------+---------+--------------------------------------------
3  | 0      | 0       | SEARCH person USING INDEX idx_city (city=?)
(1 row)
n  
---
250
(1 row)

المثال ٣: الـ primary key فيه index جاهز

Sample output (press Run to produce your own):
id | parent | notused | detail                                           
---+--------+---------+--------------------------------------------------
2  | 0      | 0       | SEARCH person USING INTEGER PRIMARY KEY (rowid=?)
(1 row)
id  | name    | city 
----+---------+------
500 | user500 | Cairo
(1 row)

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

اختبار قصير

اختبر فهمك

1. The main cost of a query on disk is:

2. A heap file is:

3. An index costs:

4. A dense index has an entry for:

5. Which columns are good for an index?

تمارين برمجة

جرّب بإيدك

التمرين 1

اعمل index على name وشوف الـ plan لـ WHERE name = 'user77'.

Create an index on name and see the plan for WHERE name = 'user77'.

(output appears here)

التمرين 2

اعمل index مركّب على (city, name) وشوف الـ plan لـ city = 'Cairo' AND name = 'user8'.

Create a composite index on (city, name) and see the plan for city = 'Cairo' AND name = 'user8'.

(output appears here)

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

قيّم أثر الـ index على جدول 1000 صف

الداتا: 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. الجدول بيتولّد في الـ starter (1000 صف).
  2. اعرض الـ plan لبحث عن city = 'Luxor' قبل أي index: لازم يكون SCAN.
  3. اعمل index على city واعرض الـ plan تاني: لازم يبقى SEARCH بـ index.
  4. عدّ الصفوف. المتوقع: 250.
Expected 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=?)
(1 row)
n  
---
250
(1 row)

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.