optimized data query for charts