Query Optimizationتحسين الاستعلامات

الشريحة 1 من 11

مقدمة الفصل

تفاصيل المصدرالفصل 5 · الشريحة ⁦2⁩
المصدر
الفصل 5 · الشريحة ⁦2⁩

Relational algebra: 5 basic operationsالعمليات الأساسية

Relational algebra: the five basic operations

Relational algebra
A formal language used to query and manipulate relational databases — selection, projection, union, join, … — very useful for representing execution plans.
Relational algebra: 5 basic operations (slide 3)
OperationSymbolWhat it does (slide 3)
SelectionσSelects a subset of rows from a relation (horizontal)
ProjectionπRetains the required columns from a relation (vertical)
Cross-product×Allows us to combine two relations
Set-difference−Tuples in r1, but not in r2
Union∪Tuples in r1 and/or in r2
  • Each operation returns a relation, so operations can be composed — the algebra is "closed".

Which relational algebra operation retains only the required columns of a relation?

تفاصيل المصدرالفصل 5 · الشرائح ⁦2–3⁩
المصدر
الفصل 5 · الشرائح ⁦2–3⁩

Selection and projectionالاختيار والإسقاط

Selection, projection and nesting (sailing database)

S2 (slide 4)
sidsnameratingage
28yuppy935.0
31lubber855.5
44guppy535.0
58rusty1035.0
Selection
Rows that satisfy the condition; schema of the result is the same as the input. σ rating>8 (S2) = yuppy (9), rusty (10).
Projection
Exactly the fields in the projection list; has to eliminate duplicates (real systems typically don't, unless the user asks). π age (S2) = 35.0, 55.5.
Nesting
An operator's result is a relation, so it can feed another: π sname,rating (σ rating>8 (S2)) = (yuppy, 9), (rusty, 10).

S2 has ages 35.0, 55.5, 35.0, 35.0. How many tuples does π age (S2) contain?

تفاصيل المصدرالفصل 5 · الشرائح ⁦4–8⁩
المصدر
الفصل 5 · الشرائح ⁦4–8⁩

Union, set difference and cross-productالاتحاد والفرق والضرب

S1 (slide 9)
sidsnameratingage
22dustin745.0
31lubber855.5
58rusty1035.0
  • S1 ∪ S2: dustin, lubber, rusty, guppy, yuppy — 5 tuples (common tuples appear once).
  • S1 − S2 = (22, dustin, 7, 45.0). S2 − S1 = (28, yuppy), (44, guppy).
  • S1 × R1: each row of S1 paired with each row of R1.

S1 has 3 tuples and R1 has 2 tuples. How many rows does S1 × R1 have?

تفاصيل المصدرالفصل 5 · الشرائح ⁦9–12⁩
المصدر
الفصل 5 · الشرائح ⁦9–12⁩

Joinsالربط

Natural join R ⋈ S, conceptually (slide 13)

  1. 1

    Compute R × S.

  2. 2

    Select rows where attributes that appear in both relations have equal values.

  3. 3

    Project all unique attributes and one copy of each of the common ones.

من الشرائح

  • S1 ⋈ R1 = (22, dustin, 7, 45.0, 101, 10/10/96), (58, rusty, 10, 35.0, 103, 11/12/96).
  • Theta join S1 ⋈ S1.sid<R1.sid R1 = (22, dustin, …, 58, 103, 11/12/96), (31, lubber, …, 58, 103, 11/12/96).
  • Joins are compound operators (cross product, selection, sometimes projection) — usually done much more efficiently than this.
  • Useful for putting "normalized" relations back together.

Conceptually, what is the second step of computing a natural join R ⋈ S?

تفاصيل المصدرالفصل 5 · الشرائح ⁦13–15⁩
المصدر
الفصل 5 · الشرائح ⁦13–15⁩

Equivalent expressionsالتعبيرات المتكافئة

Equivalent relational expressions

Equivalent
Two relational algebra expressions that generate the same set of tuples on every legal database instance (order of tuples is irrelevant); they have the same set of attributes and the same set of tuples.
Equivalence rule
Says that expressions of two forms are equivalent: replace the first form by the second, or vice versa.
تفاصيل المصدرالفصل 5 · الشرائح ⁦16–17⁩
المصدر
الفصل 5 · الشرائح ⁦16–17⁩

Equivalence rules 1–6القواعد 1–6

تذكّر القاعدة قبل الكشف.

  1. 1Conjunctive selection
  2. 2Selection is commutative
  3. 3Sequence of projections
  4. 4Selection with × and theta join
  5. 5Joins are commutative
  6. 6Joins are associative

π sname (π sname,rating (π sname,rating,age (S2))) can be simplified, by rule 3, to:

تفاصيل المصدرالفصل 5 · الشرائح ⁦18–19⁩
المصدر
الفصل 5 · الشرائح ⁦18–19⁩

Equivalence rules 7–12القواعد 7–12

تذكّر القاعدة قبل الكشف.

  1. 7Selection distributes over the theta join
  2. 8Projection distributes over the theta join
  3. 9Union and intersection are commutative
  4. 10Union and intersection are associative
  5. 11Selection distributes over ∪, ∩ and −
  6. 12Projection distributes over union

σ branch_city="Brooklyn" (branch ⋈ account): the condition uses only attributes of branch. Which equivalent form does rule 7a give?

تفاصيل المصدرالفصل 5 · الشرائح ⁦21–23⁩
المصدر
الفصل 5 · الشرائح ⁦21–23⁩

Transformation examplesأمثلة التحويل

  • Schema: branch(branch_name, branch_city, assets); account(account_number, branch_name, balance); depositor(customer_name, account_number); …

المسألة

Names of all customers with an account at some branch located in Brooklyn (slides 25–26).

خطوات الحل

  1. π customer_name (σ branch_city="Brooklyn" (branch ⋈ (account ⋈ depositor)))
  2. Rule 7a: the condition involves only branch's attributes

النتيجة

π customer_name ((σ branch_city="Brooklyn" (branch)) ⋈ (account ⋈ depositor)) — performing the selection as early as possible reduces the size of the relation to be joined.

المسألة

… and whose account balance is over $1000 (slides 27–28).

خطوات الحل

  1. π customer_name (σ branch_city="Brooklyn" ∧ balance>1000 (branch ⋈ (account ⋈ depositor)))
  2. Rule 6a (join associativity): π customer_name ((σ branch_city="Brooklyn" ∧ balance>1000 (branch ⋈ account)) ⋈ depositor)
  3. Now "perform selections early" applies

النتيجة

σ branch_city="Brooklyn" (branch) ⋈ σ balance>1000 (account) — a sequence of transformations can be useful.

Why is π customer_name ((σ branch_city="Brooklyn" (branch)) ⋈ (account ⋈ depositor)) preferred to the original expression?

تفاصيل المصدرالفصل 5 · الشرائح ⁦24–28⁩
المصدر
الفصل 5 · الشرائح ⁦24–28⁩

Join orderingترتيب الربط

Join ordering
For all relations r1, r2, r3: (r1 ⋈ r2) ⋈ r3 = r1 ⋈ (r2 ⋈ r3). If r2 ⋈ r3 is quite large and r1 ⋈ r2 is small, we choose (r1 ⋈ r2) ⋈ r3, so that we compute and store a smaller temporary relation.

r2 ⋈ r3 is quite large and r1 ⋈ r2 is small. Which join order should be chosen?

تفاصيل المصدرالفصل 5 · الشريحة ⁦30⁩
المصدر
الفصل 5 · الشريحة ⁦30⁩

الخلاصة

تفاصيل المصدر
المصدر
الفصل 5

خريطة الفصل

    1. 1مقدمة الفصل
  1. Relational algebra: the five basic operationsالجبر العلائقي: العمليات الخمس الأساسية

    1. 2Relational algebra: 5 basic operationsالعمليات الأساسية
  2. Selection, projection and nesting (sailing database)الاختيار والإسقاط والتداخل (قاعدة الإبحار)

    1. 3Selection and projectionالاختيار والإسقاط
    1. 4Union, set difference and cross-productالاتحاد والفرق والضرب
  3. Joins: natural join and theta joinالربط: الطبيعي وربط ثيتا

    1. 5Joinsالربط
  4. Equivalent relational expressionsالتعبيرات العلائقية المتكافئة

    1. 6Equivalent expressionsالتعبيرات المتكافئة
    1. 7Equivalence rules 1–6القواعد 1–6
    1. 8Equivalence rules 7–12القواعد 7–12
  5. Transformation examples and join orderingأمثلة التحويل وترتيب الربط

    1. 9Transformation examplesأمثلة التحويل
    2. 10Join orderingترتيب الربط
    1. 11الخلاصة