I have a large MySQL table, even when properly indexed it can take 1 second for each query (doesn't sound like much but it is run for thousands of servers). Right now, I have four queries going through to get 95th percentile inbound, 95th percentile outbound, and the sum of both.
Query 1: To get number of rows to get 95th percentile row
SELECT round(count(*)*.95 FROM traffic WHERE server_id = 1;
Query 2&3 To get 95th percentile
SELECT inbound FROM traffic WHERE server_id = 1 ORDER BY inbound ASC LIMIT {95th},1
SELECT outbound FROM traffic WHERE server_id = 1 ORDER BY outbound ASC LIMIT {95th},1
Query 4 Get sum of traffic
SELECT sum(inbound+outbound) FROM traffic WHERE server_id = 1;
Can you think of any way I could combine these? I am challenged to think of a way since I need to get the 95th percentile, which is calculated by selecting a specific row based on the count. For example, if there are 10000 rows, then you order them ascending and select the 9500th row.