Given following SQL query
SELECT dd.* FROM devices_deviceconfiguration_sensors dds
JOIN devices_latest_config dlc ON dlc.config_id = dds.device_configuration_id
JOIN devices_deviceconfiguration dd ON dd.id = dlc.config_id
WHERE dds.sensor_id = $1
what is the right way to set indexes to speed up this query? Do I need dds.sensor_id
indexed or (dds.sensor_id, dds.device_configuration_id)
?
Query analysis output gives. Avg execution time is 8ms, which is slower than the average queries that we have
dds(sensor_id, device_configuration_id)
,dlc(config_id)
anddd(id)
, it seems. Also I'd recommend you to use notJOIN dlc
but WHERE EXISTS.SELECT dd.* FROM dd JOIN dds ON dd.id = dds.device_configuration_id WHERE dds.sensor_id = $1 AND EXISTS (SELECT NULL FROM dlc WHERE dlc.config_id = dds.device_configuration_id)