Oracle – Why Is the Second Query More Expensive Than the First?

indexoracleperformancequery-performance

While reading Graph Databases book, I came across this excerpt:

enter image description here

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?

Best Answer

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

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

Then the second comes back as

  1. {INDEX UNIQUE SCAN Person on Person => 'Bob' get back PersonID}
  2. {TABLE ACCESS FULL PersonFriend FOR FriendID of Bob get back PersonID}
  3. {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.