Optimizing mysqli query for calculating Median using GROUP BY & ORDER BY
Optimizing mysqli query for calculating Median using GROUP BY & ORDER BY Please see below for a sample portion of my MySQL table: table name: eb_tickets I want to output the response like so, calculating the median value with distinct values ASC: Currently I am achieving this with the following query: Unfortunately this query is taking around 1 second per record, and I am querying thousands of records with the possibility of 20 different values for the ticket_type column. I am asking for help in seeing if it is possible to optimize my current query in attempt to make the query perform faster. Thank you for your help! 1 Answer 1 Could you try like this ? SELECT counter.ticket_type,AVG(time_first_response) AS median FROM ( SELECT IF(@type = type, @ctr := @ctr + 1, @ctr := 1) AS rownum, @type := ticket_type AS ticket_type, time_first_response FROM eb_tickets ORDER BY...