Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VinayPrajapati's avatar

Query Optimization

I run this query it't take 7-8 second there are more than 1 million data in cars and cars stocks

SELECT cars.id, cars.name, car_stocks.created_at FROM carsLEFT JOIN car_stocks ON car_stocks.car_id = cars.id and date(car_stocks.created_at) = '2022-01-18'

Please suggest if you have a more optimized/less time consuming query

0 likes
10 replies
Sinnbeck's avatar

Use explain to see what index is used. If no indexes exist, you should really make some :)

VinayPrajapati's avatar

@Sinnbeck Please check actual scenario on my project Main Table This is an image Child Table This is an image Result with secound This is an image Extra detail This is an image

tykus's avatar

Using a function here is non-ideal:

date(car_stocks.created_at)

Consider:

SELECT cars.id, cars.name, car_stocks.created_at
FROM cars
LEFT JOIN car_stocks
  ON car_stocks.car_id = cars.id
  AND car_stocks.created_at BETWEEN '2022-01-18' AND '2022-01-19';

Please or to participate in this conversation.