Databases Reference
In-Depth Information
index, recursive questions such as “which customers bought this product who also
bought that product?” quickly become prohibitively expensive as the degree of re‐
cursion increases.
Relational databases struggle with highly connected domains. To understand the cost
of performing connected queries in a relational database, we'll look at some simple and
not-so-simple queries in a social network domain.
Figure 2-2 shows a simple join-table arrangement for recording friendships.
Figure 2-2. Modeling friends and friends-of-friends in a relational database
Asking “who are Bob's friends?” is easy, as shown in Example 2-1 .
Example 2-1. Bob's friends
SELECT p1 . Person
FROM Person p1 JOIN PersonFriend
ON PersonFriend . FriendID = p1 . ID
JOIN Person p2
ON PersonFriend . PersonID = p2 . ID
WHERE p2 . Person = 'Bob'
Based on our sample data, the answer is Alice and Zach . This isn't a particularly ex‐
pensive or difficult query, because it constrains the number of rows under consideration
using the filter WHERE Person.person='Bob' .
Friendship isn't always a reflexive relationship, so in Example 2-2 , we ask the reciprocal
query, which is, “who is friends with Bob?”
Example 2-2. Who is friends with Bob?
SELECT p1 . Person
FROM Person p1 JOIN PersonFriend
ON PersonFriend . PersonID = p1 . ID
JOIN Person p2
ON PersonFriend . FriendID = p2 . ID
WHERE p2 . Person = 'Bob'
 
Search WWH ::




Custom Search