Set session variable sort_buffer_size to try and speed up distinct queries.