Storage and Access Methods

  • Rows of object tables carry an object identifier (OID) and REF attributes store these OIDs; they are followed by dereferencing.
  • Collections may be stored inside the row (small) or in a separate nested table (large).
  • Large objects (BLOB, CLOB) are kept apart from the row and fetched on demand, often through a locator.
  • Access methods: B-tree indexes on attributes of structured types (address.city), function-based indexes on a method result, indexes on path expressions, and special indexes such as R-trees for spatial types.

Query Processing and Optimisation

A user-defined function (UDF) can be very expensive (image comparison), so the old rule "apply selections as early as possible" is not enough. The optimiser must use the cost and selectivity of each predicate. For predicates evaluated one after the other on the same rows, the best order has the smallest rank = (selectivity - 1) / cost, that is, cheap predicates that remove many rows go first. Other techniques are caching the results of UDFs, predicate migration (moving an expensive predicate after a join), rewriting path expressions as joins on references, and collecting statistics on UDFs.

Advanced Querying

  • User-defined types: create a type with a constructor and use it as a column type: CREATE TABLE emp (name VARCHAR(20), addr Address).
  • Manipulating object tables: CREATE TABLE person_t OF Person; rows are added with a constructor, INSERT INTO person_t VALUES (Person('Asha', Address('MG Road', 'Delhi'))); attributes are read with dot notation (p.addr.city), references with REF(p) and DEREF.
  • Object views: an ordinary relational table is shown as objects with CREATE VIEW emp_obj OF Emp_type WITH OBJECT IDENTIFIER (id) AS SELECT ...; existing relational data can then be used with object applications.

Definition: Selectivity. The fraction of rows that satisfy a predicate; a selectivity of 0.1 means that 10 percent of the rows pass.

Table 2.2: ORDBMS access and optimisation

Technique Purpose Example
Function-based index Fast search on a method result Index on upper(name)
Path index Follow references quickly Index on emp.dept.name
UDF caching Avoid repeated calls Image match results
Predicate ordering Run cheap filters first Rank order

Example 2.2: Ordering two predicates

Problem: A table has 1000 rows. Predicate P1 costs 1 ms per row and has selectivity 0.5; the user-defined predicate P2 costs 100 ms per row and has selectivity 0.1. Compare the total cost of the orders P1 then P2 and P2 then P1.

Solution:

  1. Order P1 then P2: 1000 x 1 = 1,000 ms for P1; 500 rows remain, so P2 costs 500 x 100 = 50,000 ms. Total = 51,000 ms.
  2. Order P2 then P1: 1000 x 100 = 100,000 ms for P2; 100 rows remain, so P1 costs 100 x 1 = 100 ms. Total = 100,100 ms.
  3. The ranks are (0.5 - 1) / 1 = -0.5 for P1 and (0.1 - 1) / 100 = -0.009 for P2; the smaller rank goes first.

Result: P1 first costs 51,000 ms against 100,100 ms, so it is almost twice as fast.

rows = 1000
preds = {"P1": (1, 0.5), "P2": (100, 0.1)}   # cost, selectivity
def total(order):
    left, cost = rows, 0
    for p in order:
        c, s = preds[p]
        cost += left * c
        left *= s
    return cost
print("P1 then P2:", total(["P1", "P2"]), "ms")
print("P2 then P1:", total(["P2", "P1"]), "ms")
rank = {p: round((s - 1) / c, 4) for p, (c, s) in preds.items()}
print("Ranks:", rank)
print("Best order:", sorted(rank, key=rank.get))

Output

P1 then P2: 51000.0 ms
P2 then P1: 100100.0 ms
Ranks: {'P1': -0.5, 'P2': -0.009}
Best order: ['P1', 'P2']

Common mistakes

  • Evaluating the most expensive user-defined predicate first.
  • Forgetting that large objects are stored apart and fetched on demand.
  • Treating an object view as a physical copy; it is a stored query.
  • Calling a method in a query without an index or cache, so it runs for every row.

Key takeaways

  • OIDs and REF attributes link rows of object tables.
  • Rank = (selectivity - 1) / cost; the smallest rank goes first.
  • Function-based and path indexes speed up UDF and reference queries.
  • Object views present relational data as objects.

Practice questions

  1. What does REF store?
  2. Why is predicate order important with UDFs?
  3. What is an object view?

Answers to the practice questions

  1. A reference (OID) to a row of an object table.
  2. An expensive predicate should run on as few rows as possible.
  3. A view that presents relational data as objects of a user-defined type.

Sources

  • Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit II