SELECT *
FROM vehicles t1
WHERE (SELECT COUNT(*) FROM vehicles t2
WHERE t1.pump_number = t2.pump_number
AND t1.updated_at < t2.updated_at
) < 4
AND t1.updated_at >= ?
And I supply '1970-01-01 00:00:00.000000'
for the parameter ?
.
I have around 10k records in the vehicles
table and no index is added. Above query takes around 10-20 seconds in execution.
How I can optimize it to decrease execution time?
t1.updated_at >= ?
to apply to the count in the subquery as well? Because it doesn't as it is. – Erwin Brandstetter Dec 16 '14 at 12:12