Query optimizations

Thanks to #1516, I got nerd sniped a bit and did some query-optimization.  Should hopefully speed up the dashboard a bit.  Depends on #1502, mostly just to avoid merge conflicts.
(Accidentally commented in main conversation; moved to line note)
Looks like a .distinct() has been removed. Intended? (The return value should be different now, although since .distinct() has been added to the function which processes dashboard stats, I would expect the dashboard itself to look the same.)