Docsity

Query Processing in Relational Databases, Apuntes de Sistemas de Gestión de Bases de Datos

Vista previa parcial del texto

¡Descarga Query Processing in Relational Databases y más Apuntes en PDF de Sistemas de Gestión de Bases de Datos solo en Docsity!

Query Processing

Query Processing

§ Query processing

  • Measures of Query Cost

§ Select Operation

§ Sorting

§ Join Operation

§ Other Operations

§ Evaluation of Expressions

§ Single relational algebra expression

~ many equivalent expressions

§ Different algorithms to evaluate relational algebra

operations

§ We create:

  • Evaluation primitives : A relational-algebra ops. annotated with instructions on how to evaluate them
  • Evaluation (or execution ) plan : Sequence of primitives to evaluate a query § Processed by the query-evaluation engine

§ Query Optimization : Among all equivalent

evaluation plans , choose the one with lowest cost.

Cost is estimated using statistical info. from DB catalog

Query Processing: Optimization

s salary < 75000 (Õ salary ( instructor)) Õ salary (s salary < 75000 ( instructor))

Query Processing: Optimization

§ Cost in terms of disk access (I/O), CPU , and

network communication

With HDD, I/O cost dominated CPU cost. Not longer the case with SDDs and large RAM.

§ For simplicity, we only annotate I/O cost:

Real systems do use CPU cost, and, in parallel DBs, network costs are basic

  • No. blocks transferred (b): Assuming write cost=read cost § tT : block-transfer time
  • No. random I/O accesses (S): disk seek § tS : block-access time (disk seek time + rotational latency)
  • Cost of an operation that transfer b blocks and performs S accesses b * tT + S * tS
  • Estimate tS and tT values within DB installation (depend on storage type) or specified by users in configuration files § Magnetic disk: tS = 4 ms ; tT = 0.1 ms § SSD: tS = 20- 90 μs ; tT = 2- 10 μs (^4)

Algorithms for Select Operation

§ Retrieve record(s) that satisfies an equality condition,

s A = V ( r )

§ Search with clustering index (equality) [A2]

  • Cost = hi * ( tT + tS ) + b * tT + tS where b is no. blocks (contiguous) with records with the specified search-key and hi is the depth of the index tree ( hi=1 to denote that the index is in memory)
  • If index’s search-key is a candidate key of the relation , returns a single record: § Cost = ( hi + 1) * ( tT + tS )

§ Search with secondary index (equality) [A3]

  • Cost = ( hi + b ) * ( tT + tS ) >>> Can be very expensive! <<< where b is no. blocks (non-contiguous) with records with the specified search-key and hi is the depth of the index tree ( hi=1 to denote that the index is in memory)
  • If index’s search-key is a candidate key of the relation , returns a single record: § Cost = ( hi + 1) * ( tT + tS ) (^6)

Algorithms for Select Operation

§ Retrieve record(s) that satisfies a comparison,

s A £ V ( r ) or s A ³ V ( r )

§ Search with clustering index (comparison) [A4]

  • s A ³ V(r) : use index to find first index entry ³ v and go on sequentially on the file
  • s A £ V ( r ): scan file sequentially until a record > v. § Cost = hi * ( tT + tS ) + b * tT + tS (same as before) Index not used!!

§ Search with secondary index (comparison) [A5]

  • s A ³ V(r) : use index to find first index entry ³ v, scan index sequentially from there, and retrieve pointers to records
  • s A £ V ( r ): scan index’s leaves until first entry > v, to find pointers to records + retrieve pointed records § Cost = ( hi + n ) * ( tT + tS ) (same as before) >>> Can be very expensive! <<<

External Sort-Merge Sorting

g a d 31 c 33 b 14 e 16 r 16 d 21 m 3 p 2 d 7 a 14 a 14 a 19 b 14 c 33 d 7 d 21 d 31 e 16 g 24 m 3 p 2 r 16 a 19 b 14 c 33 d 31 e 16 g 24 a 14 d 7 d 21 m 3 p 2 r 16 a 19 d 31 g 24 b 14 c 33 e 16 d 21 m 3 r 16 a 14 d 7 p 2 initial relation create runs merge pass– merge pass– runs runs sorted output 24 19 Two steps: 1.Divide the relation into small ordered pieces (called runs ) 2.Combine (merge) the runs in a sorted way

External Sort-Merge Sorting

1. Create sorted runs.

Repeat until the end of the relation ( i=0 ):

1. Read M blocks of relation into memory
2. Sort the in-memory blocks
3. Write sorted data to run Ri ; i++

2. Merge the runs : when N < M ,

1. Use N buffer blocks for input runs, and 1 block for output
2. Read the first block of each run into its buffer block
3. Repeat until all input buffer blocks are empty
  1. Select from buffer the first record in sort ordering
  2. Delete it from its buffer input block, and write it to the output buffer 3. If output buffer is full then write it to disk and free output buffer. 4. If a buffer input block is empty then read next block (if any) of its run M ≡ no. blocks (buffer size) N ≡ no. runs

Cost of External Sort-Merge

§ Cost of block transfers :

  • First step needs 2br block transfers (br: no. blocks in relation) § Load all blocks and write them into runs. Initial no. runs: é br /M ù
  • Use buffers of bb blocks per run to reduce no. accesses § Merges ë M/bb û–1 runs in each pass
  • No. passes required: élog (^) ë M/b b û–^1 ( br /M) ù
  • Total no. block transfers: B r ( 2 élog (^) ë M/b b û–^1 ( br /M) ù+1)

§ Cost of seeks :

  • First step: 2 random I/O accesses per run (read and write), 2 é br /M ù
  • Second step: Need 2 é br /bb ù seeks per merge pass
  • Total no. random I/O accesses : 2 é br /M ù + é br /bb ù ( 2 élogë M/b b û–^1 ( br /M) ù - 1)

Algorithms for Join Operation

§ To implement equi-joins ( r ⨝r.A=s.B s ), several

algorithms:

1. Nested-loop join
2. Block nested-loop join
3. Indexed nested-loop join
4. Merge-join
5. Hash-join

§ We use cost estimates to choose an algorithm

Running example : student ⨝ takes student takes No. records: 5,000 10, No. blocks: 100 400

Algorithms for Join: 1. Nested-Loop

§ Cost:

  • Worst case : only one block per relation fits in memory
no. block transfers: nr * bs + br ; no. I/O accesses: nr + br

where nr and br are the no. records and no. blocks of relation r (same for s ) § E.g., with student as outer relation : 5000 * 400 + 100 = 2,000,100 block transfers 5000 + 100 = 5100 I/O accesses with takes as outer relation : 1,000,400 block transfers 10,400 I/O accesses

  • Best case : both relations fit completely in memory no. block transfers: bs + br ; no. I/O accesses: 2 § If only one relation fits in memory, by using it as the inner relation, you obtain best-case cost § E.g., If student fits in memory: 500 block transfers 2 I/O accesses Order matters! To decide the order, consider times tT & tS

Algorithms for Join: 2. Block Nested-Loop

§ If no relation fits in memory, use a per-block basis:

  • Every block of inner relation is paired with every block of outer relation for each block Br of r for each block Bs of s for each tuple tr in Br for each tuple ts in Bs if pair ( tr,ts ) satisfies condition q then result += ( tr • ts ) >> Concatenate tr and ts end if end for end for end for end for Variant of nested-loop join , share most of its properties

Algorithms for Join: 1-2 (Block) Nested-Loop

§ Improvements:

  • If the join attribute is a key on inner relation, stop inner loop on first match of the join condition
  • If the joint attribute has an index on inner relation, use efficient index lookups
  • Scan inner loop forward and backward alternately, to make use of the blocks remaining in buffer

Algorithms for Join: 3. Merge-Join

1. Sort both relations on their join attribute (if required)

2. Join them by merging the sorted relations

Similar to merge stage of sort-merge algorithm

1. Use a pointer on each relation’s tuples, pr and ps
2. For each value av of the joint attribute A (as ordered in s ),

repeat:

  1. Collect in Ss all tuples ts in s with value ts[A]=av This set must fit in memory!
  2. For any tuple tr in r with tr[A]=av For any tuple ts in Ss result += ( tr • ts )

§ Can be used for equi-joins

and natural joins

a 3 b 1 d 8 d 13 f 7 m (^5) q (^6) a A b G c L d N m B a1 a2 a1 a pr ps r s