12511: readable_by filters on is_trashed directly
Fix performance problem in the negative join NOT EXISTS(...) used to filter out
trashed items for admin users. The clause
(uuid = target_uuid OR owner_uuid = target_uuid) doesn't use the index
efficiently and results in a very expensive seq scan.
However, the "OR" in the negative clause is not necessary. We only need
materialized_permission_view to know if "owner_uuid" is trashed (directly or
indirectly), not "uuid". If the record type has an is_trashed flag (currently
only collections and groups) then it is more efficient to filter on that
directly.
This commit refactors the readable_by query to only check owner_uuid, and
additionally filter on "is_trashed" for collections and groups.
As an additional cleanup, because "readable_by" now explicitly knows to filter
on "is_trashed" this makes the default_scope in Collections redundant. This
commit also removes default_scope & various uses of unscoped reduces complexity.
Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <pamstutz@veritasgenetics.com>