Why is this second query more expensive than the first? While reading Graph Databases book, I came across this excerpt:


  


I can't figure out why the second query is more expensive than the first.
Indeed, both filter on the Bob person.

May anyone explain it?
The book is assuming that PersonFriend is indexed on PersonID, but not on FriendID.  It also seems to assume that Person indexes PersonID and Person independently.

If this is the case, the first query comes back as 


{INDEX UNIQUE SCAN Person on Person => 'Bob' get back PersonID}
{INDEX RANGE SCAN PersonFriend on PersonID => PersonIDs for Alice
and Zack get back FriendID}
{LOOP {INDEX UNIQUE SCAN Person on PersonID} for Alice and Zack}


Then the second comes back as


{INDEX UNIQUE SCAN Person on Person => 'Bob' get back PersonID}
{TABLE ACCESS FULL PersonFriend FOR FriendID of Bob get back
PersonID}
{LOOP {INDEX UNIQUE SCAN Person on PersonID} for friend(s)}


This would be an elementary relational database design error.  With a relationship table like this you would always index all keys, and the performance profile for both queries would be indistinguishable (ignoring variations in value distributions, etc).

At this point it sounds like the author is trying to show that relational databases are intrinsically deficient.