perf(sql): allows removing DISTINCT from some MySQL queries

(freshly rebased for 1.x - @Srokap give this a look)

We add the "distinct" option to a few query functions to allow devs to
remove DISTINCT clauses from queries where they are unnecessary and worsen
performance. We also set the option in some core queries where it's unlikely
to cause trouble.

Fixes #4594

TODOs:
- [ ] Add tests
Should this be `SELECT count({$distinct_str}e.guid)`