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:
- [x] Add tests that exercise this flag
- [ ] Add docs explaining when it is OK to use this flag
For the record I don't fancy using all capital leters in variables and especially in case of sql exactly to not make it easy to confuse with actual query. That being said, it's so minor thing that don't bother changing.