0% found this document useful (0 votes)
13 views4 pages

BD2 Text and Sol Draft

The document discusses different topics related to databases including active databases, concurrency control, XML, and physical databases. It provides examples and problems related to maintaining distances in a graph database, concurrency anomalies, querying XML data, and query planning and cost estimation. Detailed solutions are provided for the problems.

Uploaded by

SaladSlayer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

BD2 Text and Sol Draft

The document discusses different topics related to databases including active databases, concurrency control, XML, and physical databases. It provides examples and problems related to maintaining distances in a graph database, concurrency anomalies, querying XML data, and query planning and cost estimation. Detailed solutions are provided for the problems.

Uploaded by

SaladSlayer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DATA BASES 2 – JULY 12TH, 2019 – DURATION: 2H

PROF. SARA COMAI, PROF. DANIELE M. BRAGA


A. Active Databases (9 p.)
PAGE (PageID, Content, HomeY/N, MinDistanceFromHome)
HYPERLINK (FromPageID, ToPageID)
The relational schema above represents the map of a website, with pages and hyperlinks between them. For each page,
the minimum distance from the home page is computed (and not inserted by the client application) as the minimum number
of steps that are required to reach that page starting from the home page. Clearly, insertions and deletions of hyperlinks
may affect the distances in the whole graph. Design a set of triggers that, starting from an initially empty database,
correctly keep the distance from the home page automatically updated for each page when: (a) the home page is created;
(b) new hyperlinks are added. Notice that the map is a graph, and each page may have outgoing links towards several
other pages and incoming links from several other pages. Finally, discuss termination of the designed rule set.
If needed or convenient, you may possibly extend the schema with additional auxiliary attributes/tables.

B. Concurrency control (6 p.)


Three different clients send requests to access the same resource x and try to T1 T2 T3
execute transactions T1..T3 according to the order shown in the table aside. Each r3(x)
client can send a request only after the previous one has been executed or rejected; r1(x)
if a resource is locked, the client remains inactive until access is granted. In case of r2(x)
deadlocks, the first transaction that made the request is aborted and restarted. x=x+10
Consider a 2PL scheduler and, assuming that the initial value of x is 200, describe w1(x)
its behavior in the two following scenarios: commit
x=x+20
1. All transactions use the READ COMMITTED isolation level (i.e., strict 2PL for
write operations; read operations can see only data committed before the r3(x)
w2(x)
transaction began and can release the lock before acquiring other locks);
commit
2. All transactions use the SERIALIZABLE isolation level (strict 2PL both for read commit
and write operations).
Show in a table (similar to the one above) all the operations carried out by each transaction, always specifying the value
that is read or written. Finally, specify if anomalies occur.

C. XML (9 p.)
<!ELEMENT Club ( Member*, Model* )>
<!ELEMENT Member ( Name, City, Country, Motorbike+, … )>
<!ATTLIST Member Identifier ID #REQUIRED>
<!ELEMENT Motorbike ( PlateNo, ModelName, ProductionYear )>
<!ELEMENT Model ( Name, TechnicalSpecs, History, … )>
The DTD above describes an international Club of Moto Guzzi™ enthusiast owners and collectors of vintage models.
Unspecified elements only contain PCData or are immaterial to the assignment. Extract in XQuery:
(4 p.) 1. The city hosting the largest number of exemplars of the “Falcone” model.
(5 p.) 2. For every bike model, the percentage of exemplars hosted in each distinct country, listed in descending order.

D. Physical Databases (6 p.)


A table MOTORBIKE (PlateNo, Model, Year, OwnerId, Description) stores
240K tuples on 12K blocks in a primary entry-sequenced storage. A table select M.*
MEMBER (Id, Name, City, Country, Address) stores 160K tuples stored on 16K from MEMBER M join MOTORBIKE B
blocks in a primary hash built on the primary key with an average access cost on M.Id = B.OwnerId
of 1.25 i/o operations per lookup. We know that val(City)=8K and that only where M.City=“Milan” and B.Year = 1947
1% of the registered bikes were built in 1947.
Describe briefly (but precisely) a reasonable query plan for the boxed query and estimate its execution cost in the following
scenarios. Cost estimations provided without a clear description of the associated plan will not be considered.
(a) There are no secondary indexes;
(b) There are also a B+(City) index for MEMBER (F=55, 3 levels, 3K leaf nodes) and a B+(OwnerId) index for MOTORBIKE
(F=65, 3 levels, 4K leaf nodes).
SOLUTIONS

A. TRIGGER

CREATE TRIGGER InsHomePage


FOR EACH ROW
AFTER INSERT ON PAGE
WHEN (NEW.HomeY/N)=”Y” // we can assume that other triggers do the needed check on the home page
BEGIN e.g., uniqueness etc.
UPDATE PAGE
SET MinDistanceFromHome = 0
WHERE PageID=NEW.PageID;
END;

CREATE TRIGGER InsHyperlink


AFTER INSERT ON HYPERLINK
FOR EACH ROW
WHEN (SELECT MinDistanceFromHome
FROM PAGE
WHERE PageID=new.FromPageID) + 1) <
(SELECT MinDistanceFromHome
FROM PAGE
WHERE PageID=new.ToPageID)
UPDATE PAGE
SET MinDistanceFromHome = 1+
(SELECT MinDistanceFromHome
FROM PAGE
WHERE PageID=new.FromHomeID)
WHERE PageID=NEW.ToPageID AND
P.MinDistanceFromHome > new.MinDistanceFromHome + 1;

CREATE TRIGGER UpdateDistance


AFTER UPDATE OF MinDistanceFromHome ON PAGE
FOR EACH ROW
WHEN EXISTS (SELECT *
FROM PAGE AS P JOIN HYPERLINK AS H ON
H.ToPageID=P.PageID
WHERE H.FromPageID=new.PageID AND
P.MinDistanceFromHome >
new.MinDistanceFromHome + 1)
UPDATE PAGE
SET MinDistanceFromHome = new.MinDistanceFromHome + 1
WHERE PageID IN (SELECT P.PageID
FROM PAGE AS P JOIN HYPERLINK AS H ON
H.ToPageID=P.PageID
WHERE H.FromPageID=new.PageID AND
P.MinDistanceFromHome >
new.MinDistanceFromHome + 1;
B. CONCURRENCY CONTROL

Scenario a)

T1 r1(x) 200 x=x+10 210 w1(x) 210 c


T2 r2(x) 200 x=x+20 220 w2(x) 220 c
T3 r3(x) 200 r3(x) 210 c

Lost update (T1 vs T2) and unrepeatable read (T3)

Scenario b)

T1 r1(x) 200 x=x+10 210 w1(x) wait c


T2 r2(x) 200 x=x+20 220 w2(x) wait c
T3 r3(x) 200 r3(x) 200 c

DEADLOCK  T1 is aborted and T2 gets the W-Lock

T1 r1(x) 200 x=x+10 210 w1(x) ABORT! C


T2 r2(x) 200 x=x+20 220 w2(x) 220 c
T3 r3(x) 200 r3(x) 200 c

Transaction T1 restarts: if it starts before T2/T3 commit it waits unit they commit, otherwise it can be
immediately executed. In any case, it is executed after T2 and T3 end.

T1 RESTART!! r1(x) 220 x=x+10 230 w1(x) 230 c

C. XML

1. The city hosting the largest number of exemplars of the “Falcone” model.

LET $maxExemplars := max(FOR $c in distinct-values(//City)


RETURN count(//Motorbike[../City=$c AND ModelName=”Falcone”])
FOR $c in distinct-values(//City)
WHERE count(//Motorbike[../City=$c AND ModelName=”Falcone”]) = $maxExemplars
RETURN $c //it returns the city (or, possibly, cities) with the maximum number of exemplars

2. For every bike model, the percentage of exemplars present in each distinct country, listed in descending order.

FOR $mod IN //Model


RETURN {$mod,
FOR $co IN distinct-values(//Country)
LET $total := count(//Motorbike[ModelName=$mod]) //ok also if you interpret the text with total by country
LET $totalExemplars := count(//Motorbike[..Country=$co AND ModelName=$mod ])
LET $perc := $totalExemplars / $total
ORDER BY $perc
RETURN {$co, $perc}
}

D. PHYSICAL DB

a)

full scan of Motorbike and lookup on the selected 1947 bikes:

12K + 240K ∙ 1% ∙ 1.25 = 15K

b)

B+ using “Milan” to find 160K/8K=20 members in Milan, 20 pointers and 20 lookups onto the B+ for Motorbike

3+20+20 ∙ ( 3 + 240K/160K ) = 23 + 20 ∙ 4.5 = 113

You might also like