Query Processingمعالجة الاستعلامات

الشريحة 1 من 14

مقدمة الفصل

ماذا ستتعلم في هذا الفصل

  1. The basic steps of query processing and how query cost is measured.
  2. Translating SQL queries into relational algebra (query blocks).
  3. External sorting with merge sort and its cost.
  4. Catalog information, selectivity and the algorithms for selection operations.
  5. Materialization versus pipelining.
  6. Join algorithms: nested-loop, block nested-loop, indexed nested-loop and sort-merge join, with their costs.
تفاصيل المصدرالفصل 4 · الشريحة ⁦3⁩
المصدر
الفصل 4 · الشريحة ⁦3⁩

Basic steps in query processingخطوات معالجة الاستعلام

Query processing steps and query cost

  1. 1

    Parsing and translation: the parser checks syntax and semantic, verifies relations, and translates the query into its relational algebra form.

  2. 2

    Query optimization: choosing a suitable execution strategy — amongst all equivalent evaluation plans choose the one with lowest cost.

  3. 3

    Evaluation: the query evaluation engine takes a query execution plan, executes it, and returns the answers.

من الشرائح

  • select emp_name from Employee where salary > 10000 → π emp_name (σ salary>10000 (Employee)); an annotated relational algebra expression with a detailed evaluation strategy is an **evaluation-plan**.

In which step of query processing is the cheapest of all equivalent evaluation plans chosen?

تفاصيل المصدرالفصل 4 · الشرائح ⁦3, 5, 6⁩
المصدر
الفصل 4 · الشرائح ⁦3, 5, 6⁩

Measures of query costقياس التكلفة

Query processing steps and query cost

  • Query cost = total elapsed time to answer a query (disk access, CPU, network communication).
  • Disk access is typically the predominant cost and relatively easy to estimate.
  • For simplicity the cost measure is the number of block transfers from disk.
  • Costs depend on the size of the buffer in main memory: more memory reduces the need for disk access.
تفاصيل المصدرالفصل 4 · الشريحة ⁦7⁩
المصدر
الفصل 4 · الشريحة ⁦7⁩

Translating SQL into relational algebraالترجمة إلى الجبر العلائقي

Translating SQL queries into relational algebra

Query block
The basic unit that can be translated into the algebraic operators and optimized: a single SELECT-FROM-WHERE expression, with GROUP BY and HAVING if part of the block. Nested queries are separate query blocks.
SELECT LNAME, FNAME FROM EMPLOYEE WHERE SALARY > (SELECT MAX (SALARY) FROM EMPLOYEE WHERE DNO = 5) (slide 9)

Inner block

  • SELECT MAX (SALARY) FROM EMPLOYEE WHERE DNO = 5
  • ℱ MAX SALARY (σ DNO=5 (EMPLOYEE)) → C

Outer block

  • SELECT LNAME, FNAME FROM EMPLOYEE WHERE SALARY > C
  • π LNAME, FNAME (σ SALARY>C (EMPLOYEE))

A query has a nested SELECT in its WHERE clause. How many query blocks is it translated into?

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

External sorting: the sorting phaseالفرز الخارجي

External sorting: merge sort

  • Sorting is needed in order by, join, union, intersection, distinct, …; quicksort for relations that fit in memory.
  • External sorting: for large files on disk that do not fit in main memory — merge sort is a good choice.
  • Two steps: a partial sort step (runs) and a merge step.

المسألة

Example (slide 12): nB = 5 blocks and file size b = 1024 blocks.

المعطيات

nB (buffer pages)
5
b (file blocks)
1024

الصيغة

nR = ⌈b / nB⌉; sorting cost = 2b

خطوات الحل

  1. nR = ⌈1024 / 5⌉ = 205
  2. Each initial run is 5 blocks
  3. Sorting phase reads b blocks and writes b blocks

النتيجة

205 initial sorted runs; the sorting phase costs 2b = 2048 block transfers. (Slide 12; the 2048 is 2b computed from slide 11.)

A file of b = 1024 blocks is sorted with nB = 5 buffer pages. How many initial sorted runs are created?

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

External sorting: merging and total costالدمج والتكلفة

External sorting: merge sort

Degree of merging dM
min(nB − 1, nR) — one buffer block per run being merged, and one block for the merged result.
Number of passes nP
⌈log_dM(nR)⌉ — repeated passes until all runs are merged into one.
Total cost
2b + 2b × nP = 2b (⌈log_dM(nR)⌉ + 1) — every pass reads and writes every block once.
  • Example (slide 14): dM = min(5−1, 205) = 4; nP = ⌈log_4(205)⌉ = 4: 205 → 52 → 13 → 4 → 1 runs.

المسألة

Exercise (slide 17): a file of 10,000 pages and 3 buffer pages.

With nB = 5 buffer pages and nR = 205 runs, what is the degree of merging dM?

تفاصيل المصدرالفصل 4 · الشرائح ⁦13, 14, 16, 17⁩
المصدر
الفصل 4 · الشرائح ⁦13, 14, 16, 17⁩

Catalog information and selectivityمعلومات الفهرس والانتقائية

Catalog information, scans and selectivity

Catalog information (slides 18, 20)
SymbolMeaning (slides 18, 20)
r# of records in the file
Rrecord size
b# of blocks in the file
bfrblocking factor
x# of levels of a multilevel index
bI1# of first-level index blocks
d# of distinct values of an attribute
slselectivity: records satisfying the condition ÷ total records
sselection cardinality = sl × r

Key attribute

  • d = r
  • sl = 1/r
  • s = 1

Nonkey attribute (uniform distribution)

  • sl = 1/d
  • s = r/d
  • File scan: search algorithms that locate and retrieve records fulfilling a selection condition. Index scan: search algorithms that use an index (condition on the index search-key).
  • Cost estimate C = # of disk blocks scanned.

A file has r = 10,000 records. A nonkey attribute has d = 50 distinct values, uniformly distributed. What is the selection cardinality s of an equality condition on it?

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

Simple selection: S1–S5خوارزميات الاختيار

Selection algorithms and costs (slides 21–25)
AlgorithmWhenCost C
S1 linear search
  • Any condition
  • nonkey attribute
b
S1 linear searchEquality on a key attributeb/2 if found, else b
S2 binary searchEquality on the attribute the file is ordered on⌈log2 b⌉
S3 primary indexEquality on a keyx + 1
S3 hash indexEquality on a key
  • 1 (static or linear hashing)
  • 2 (extendable hashing)
S4 primary indexRange on a key (>, ≥, <, ≤)x + b/2
S5 clustered indexEquality on a nonkeyx + ⌈s/bfr⌉
  • S2 needs an equality comparison on the attribute on which the file is ordered (blocks stored contiguously).
  • S3 primary index: 1 more block than the # of index levels.
  • S4: find the first matching record with the index, then retrieve all subsequent (or preceding) records of the ordered file.
  • S5: records are on consecutive blocks.

An equality selection on a key uses a primary index with x = 3 levels. What is the cost C?

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

Complex selections and negationالاختيار المركب والنفي

Complex selections (S7–S10) and negation

  1. S7Conjunctive, one indexPick the condition and algorithm with the least cost; test the other conditions after fetching the tuple into memory.
  2. S8Conjunctive, composite indexEquality conditions on two or more attributes with a composite index (or hash structure) on the combined field: use it directly.
  3. S9Conjunctive, intersection of record pointersOne index per condition; intersect the sets of record pointers, then fetch the records; test unindexed conditions in memory.
  4. S10Disjunctive, union of identifiersOnly if **all** conditions have indices (otherwise linear scan); take the union of the record pointers.
Negation σ ¬θ (r)
Use a linear scan on the file; if very few records satisfy ¬θ and an index applies, find them with the index and fetch them.

A disjunctive selection (θ1 OR θ2 OR θ3) has indices only on θ1 and θ2. What should the query processor do?

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

Materialization vs pipeliningالتجسيد وخط الأنابيب

Materialization and pipelining

Materialized vs pipelined evaluation (slides 31–33)
MaterializationPipelining
HowOne operation at a time, from the lowest levelSeveral operations simultaneously
Intermediate resultsStored as temporary relationsPassed directly to the next operation
CostWriting temporary files to disk is time consumingMuch cheaper: no temporary relation on disk
Limitsلا تذكر الشرائح شيئًا هنا.Not always possible (e.g., sort, hash-join)

من الشرائح

  • Pipelining: don't store the result of σ balance<2500 (account); pass tuples directly to the join, and the join's tuples directly to the projection.

Why is pipelined evaluation much cheaper than materialization?

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

Nested-loop joinالربط بالحلقات المتداخلة

Join algorithms (slide 34)

  • Nested-loop join
  • Block nested-loop join
  • Indexed nested-loop join
  • Merge-join
  • For each tuple tr of the outer relation r, test every tuple ts of the inner relation s; add matching pairs to the result.
  • Requires no indices and can be used with any kind of join condition, but is expensive: it examines every pair of tuples.
  • Worst case: C = bR + (rR × bS) + bRS. If the smaller relation fits entirely in memory, use it as the inner relation: bR + bS + bRS.

المسألة

Slide 40: customer (10,000 records, 400 blocks) ⋈ depositor (5,000 records, 100 blocks), worst case.

customer has 10,000 records in 400 blocks; depositor has 5,000 records in 100 blocks. In the worst-case nested-loop join, what is the cost (without bRS) with depositor as the outer relation?

تفاصيل المصدرالفصل 4 · الشرائح ⁦34, 35, 36, 37, 39, 40⁩
المصدر
الفصل 4 · الشرائح ⁦34, 35, 36, 37, 39, 40⁩

Block nested-loop and indexed nested-loop joinالحلقات على الكتل والمفهرسة

Nested-loop joins

Block nested-loop join

  • Every block of the inner relation is paired with every block of the outer relation
  • Worst case: C = bR + (bR × bS) + bRS
  • Smaller relation in memory as inner: bR + bS + bRS
  • If neither fits in memory, choose the smaller relation for the outer loop

Indexed nested-loop join (J2)

  • Index lookups replace file scans when an index exists on the inner relation's join attribute
  • For each outer tuple, use the index to retrieve matching inner tuples
  • Indices on both: use the relation with fewer tuples as the outer
  • Primary index: bR + (rR × (x + 1)) + bRS

المسألة

Slides 46–47: depositor (outer, 5,000 records, 100 blocks) ⋈ customer (400 blocks) with a primary B+-tree index on customer-name: height 4 + 1 data access = 5.

depositor (5,000 records, 100 blocks) is the outer relation; customer has a primary B+-tree index needing 5 accesses per lookup. What is the indexed nested-loop cost (without bRS)?

تفاصيل المصدرالفصل 4 · الشرائح ⁦41, 43, 44, 45, 46, 47⁩
المصدر
الفصل 4 · الشرائح ⁦41, 43, 44, 45, 46, 47⁩

Sort-merge joinالربط بالفرز والدمج

  1. 1

    Sort both relations on their join attribute (if not already sorted).

  2. 2

    Merge the sorted relations (like the merge stage of external sort-merge), matching every pair with the same join value (duplicates).

  • Can be used only for equi-joins and natural joins.
  • Each block needs to be read only once: bR + bS + sorting cost (if not sorted) + bRS.

For which joins can sort-merge join be used?

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

الخلاصة

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

خريطة الفصل

    1. 1مقدمة الفصل
  1. Query processing steps and query costخطوات معالجة الاستعلام وتكلفته

    1. 2Basic steps in query processingخطوات معالجة الاستعلام
    2. 3Measures of query costقياس التكلفة
  2. Translating SQL queries into relational algebraترجمة SQL إلى الجبر العلائقي

    1. 4Translating SQL into relational algebraالترجمة إلى الجبر العلائقي
  3. External sorting: merge sortالفرز الخارجي: الفرز بالدمج

    1. 5External sorting: the sorting phaseالفرز الخارجي
    2. 6External sorting: merging and total costالدمج والتكلفة
  4. Catalog information, scans and selectivityمعلومات الفهرس والمسح والانتقائية

    1. 7Catalog information and selectivityمعلومات الفهرس والانتقائية
  5. Algorithms for simple selection (S1–S5)خوارزميات الاختيار البسيط (S1–S5)

    1. 8Simple selection: S1–S5خوارزميات الاختيار
  6. Complex selections (S7–S10) and negationالاختيار المركب (S7–S10) والنفي

    1. 9Complex selections and negationالاختيار المركب والنفي
  7. Materialization and pipeliningالتجسيد وخط الأنابيب

    1. 10Materialization vs pipeliningالتجسيد وخط الأنابيب
  8. Nested-loop joinsالربط بالحلقات المتداخلة

    1. 11Nested-loop joinالربط بالحلقات المتداخلة
    2. 12Block nested-loop and indexed nested-loop joinالحلقات على الكتل والمفهرسة
    1. 13Sort-merge joinالربط بالفرز والدمج
    1. 14الخلاصة