فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة 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)
اختبار قصير
اختبر فهمك
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)
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_name ON person(name);
EXPLAIN QUERY PLAN SELECT * FROM person WHERE name = 'user77';
''')
Output
id | parent | notused | detail ---+--------+---------+-------------------------------------------- 3 | 0 | 0 | SEARCH person USING INDEX idx_name (name=?) (1 row)
التمرين 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)
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_name ON person(city, name);
EXPLAIN QUERY PLAN SELECT * FROM person WHERE city = 'Cairo' AND name = 'user8';
''')
Output
id | parent | notused | detail ---+--------+---------+--------------------------------------------------------------------- 2 | 0 | 0 | SEARCH person USING COVERING INDEX idx_city_name (city=? AND name=?) (1 row)
مشروع صغير في آخر المحاضرة
قيّم أثر الـ 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
الخطوات
- الجدول بيتولّد في الـ starter (1000 صف).
- اعرض الـ plan لبحث عن city = 'Luxor' قبل أي index: لازم يكون SCAN.
- اعمل index على city واعرض الـ plan تاني: لازم يبقى SEARCH بـ index.
- عدّ الصفوف. المتوقع: 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.