Permission queries hurt performance
This is a follow up to https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/23952#note_128755290 where we discovered that the subqueries we perform to figure out permissions and visibility actually make it hard to optimize the full query (see the thread in https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/23952#note_128755290).
This issue covers improving those queries if possible. It may pay off in other places as well, not only in snippets search.