Improve response time for drawing histogram

This change reduces a query with the same data from 1m7s to 11s