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 that exercise this flag
- [ ] Add docs explaining when it is OK to use this flag
WS doesn't hurt. I'd actually prefer $DISTINCT everywhere, being either "DISTINCT" or empty.