Docsity

Join Size Prediction Concurrency for Database Design | CMSC 424, Study notes of Principles of Database Management

Partial preview of the text

Download Join Size Prediction Concurrency for Database Design | CMSC 424 and more Study notes Principles of Database Management in PDF only on Docsity!

CMSC 424 – Database design

Lecture 20

Join size prediction

Concurrency/recovery

Mihai Pop

Admin

Statistical Information for Cost Estimation

  • n r : number of tuples in a relation r.
  • b r : number of blocks containing tuples of r.
  • l r : size of a tuple of r.
  • f r : blocking factor of r — i.e., the number of tuples of r that fit into one block.
  • (^) V(A, r): number of distinct values that appear in r for attribute A; same as the size of ∏ A ( r ).
  • (^) If tuples of r are stored together physically in a file, then:
  • (^) Also: instead of V(A,r) can store a histogram           = r f r n r b

Selection Size Estimation

  • σ A=v ( r )
    • nr / V(A,r)^ : number of records that will satisfy the selection
    • (^) Equality condition on a key attribute: size estimate = 1
  • σ A ≤ V ( r ) (case of σ A ≥ V ( r ) is symmetric)
    • (^) Let c denote the estimated number of tuples satisfying the condition.
    • (^) If min(A,r) and max(A,r) are available in catalog
      • c = 0 if v < min(A,r)
      • (^) c =
    • (^) If histograms available, can refine above estimate
    • In absence of statistical information c is assumed to be nr / 2. n r . v −min  A , r  max A , r −min  A , r

Join Operation: Running Example

Running example: depositor customer Catalog information for join examples:

  • ncustomer =^ 10,000.
  • f customer = 25, which implies that b customer =10000/25 = 400.
  • n depositor = 5000.
  • fdepositor = 50, which implies that b depositor = 5000/50 = 100.
  • (^) V(customer_name, depositor) = 2500, which implies that , on average, each customer has two accounts. - (^) Also assume that customer_name in depositor is a foreign key on customer. - (^) V(customer_name, customer) = 10000 (primary key!) h

Estimation of the Size of Joins

  • The Cartesian product^ r^ x^ s^ contains^ n r .n s tuples; each tuple occupies _s r
  • s s_ bytes.
  • (^) If R ∩ S = ∅, then r s is the same as r x s.
  • (^) If R ∩ S is a key for R , then a tuple of s will join with at most one tuple from r - (^) therefore, the number of tuples in r s is no greater than the number of tuples in s.
  • (^) If R ∩ S in S is a foreign key in S referencing R, then the number of tuples in r s is exactly the same as the number of tuples in s. - (^) The case for R ∩ S being a foreign key referencing S is symmetric.
  • (^) In the example query depositor customer, customer_name in depositor is a foreign key of customer - hence, the result has exactly^ n depositor tuples, which is 5000

Estimation of the Size of Joins (Cont.)

  • (^) Compute the size estimates for depositor customer without using information about foreign keys: - (^) V(customer_name, depositor) = 2500, and V(customer_name, customer) = 10000 - (^) The two estimates are 5000 * 10000/2500 - 20,000 and 5000 * 10000/10000 = 5000 - (^) We choose the lower estimate, which in this case, is the same as our earlier computation using foreign keys.

Size Estimation for Other Operations

  • Projection: estimated size of^ ∏ A ( r ) = V ( A , r )
  • Aggregation : estimated size of^ A g F ( r ) = V ( A,r )
  • (^) Set operations
    • (^) For unions/intersections of selections on the same relation: rewrite and use size estimate for selections - E.g.^ σθ 1 ( r )^ ∪^ σθ 2 ( r ) can be rewritten as^ σθ 1 σθ 2 ( r )
    • (^) For operations on different relations:
      • (^) estimated size of r ∪ s = size of r + size of s.
      • (^) estimated size of r ∩ s = minimum size of r and size of s.
      • (^) estimated size of r – s = r.
      • (^) All the three estimates may be quite inaccurate, but provide upper bounds on the sizes.

Overview

  • (^) Transaction : A sequence of database actions enclosed within special tags
  • (^) Properties:
    • (^) Atomicity : Entire transaction or nothing
    • (^) Consistency : Transaction, executed completely, takes database from one consistent state to another
    • Isolation : Concurrent transactions appear to run in isolation
    • (^) Durability : Effects of committed transactions are not lost
  • (^) Consistency: Transaction programmer needs to guarantee that
    • (^) DBMS can do a few things, e.g., enforce constraints on the data
  • (^) Rest: DBMS guarantees

Assumptions and Goals

  • (^) Assumptions:
    • (^) The system can crash at any time
    • (^) Similarly, the power can go out at any point
      • (^) Contents of the main memory won’t survive a crash, or power outage
    • (^) BUT… disks are durable. They might stop, but data is not lost
    • (^) Disks only guarantee atomic sector writes, nothing more
    • (^) Transactions are by themselves consistent
  • (^) Goals:
    • (^) Guaranteed durability, atomicity
    • (^) As much concurrency as possible, while not compromising isolation and/or consistency - (^) Two transactions updating the same account balance… NO - (^) Two transactions updating different account balances… YES

Transaction states

Shadow Copy

  • (^) Make updates on a copy of the database.
  • (^) Switch pointers atomically after done.
    • (^) Some text editors work this way

Next…

  • (^) Concurrency control schemes
    • (^) A CC scheme is used to guarantee that concurrency does not lead to problems
    • (^) For now, we will assume durability is not a problem
      • (^) So no crashes
      • (^) Though transactions may still abort
  • (^) Schedules
  • (^) When is concurrency okay?
    • (^) Serial schedules
    • (^) Serializability

A Schedule

T read(A) A = A - write(A) read(B) B=B+ write(B) T read(A) tmp = A*0. A = A – tmp write(A) read(B) B = B+ tmp write(B) Transactions: T1: transfers $50 from A to B T2: transfers 10% of A to B Database constraint: A + B is constant ( checking+saving accts) Effect: Before After A 100 45 B 50 105 Each transaction obeys the constraint. This schedule does too.