optimized data set analysis SQL query (eliminating linear performance decrease)