I have in my table:
ID Name Age 1 James 15 2 James 16 3 Joseph 16 4 Joseph 18 5 Steve 4
and I want to return:
5 Steve 4 1 James 15 3 Joseph 16
ie. Unique entries based on the Name row (choose the one with lowest ID when there are multiple of the same Name), and ordered by Age
What would be the correct SQL statement?
Select * FROM table Where True GROUP BY Name ORDER BY Age
It seems to be returning a lot of unique results, but it's too slow (table has >250,000 entries and it takes >30seconds to process), I wonder if there is a faster way to do this?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `table` ADD INDEX `table_idx_name` (`Name`);
SELECT * FROM table WHERE True GROUP BY table.Name ORDER BY table.Age