Home > Enterprise >  SQL Server slow query
SQL Server slow query

Time:12-02

i have a query that is more slow and i don't understand why. I think, there are the corrects indexes on the tables..

SELECT
    e.id, e.coordinate, e.lat AS latitudine, e.lon AS longitudine, e.dataora, e.indirizzo, e.dato, e.precisione_metri, e.precisione_secondi, e.precisione_invalid, e.distanza, e.velocita, es.descrizione AS evento, es.operazione, es.colore_shape, e.dato barcode, es.gestione_euristica, e.id_dispositivo  
FROM eventi_kml_polygon AS ekp
INNER JOIN eventi AS e
    ON e.id=ekp.id_evento
INNER JOIN sistema_eventi AS es
    ON es.evento=e.id_evento
INNER JOIN kml_polygon AS kp
    ON kp.id=ekp.id_kml_polygon
INNER JOIN kml AS k
    ON k.id=kp.id_kml
INNER JOIN waypoint AS w
    ON w.id_kml=k.id
INNER JOIN waypoint_periodi AS wp
    ON (wp.id_waypoint=w.id AND e.dataora BETWEEN wp.dataora_inizio AND wp.dataora_fine)
INNER JOIN modelli AS m
    ON m.id=wp.id_modello
WHERE m.id=224882
    AND es.operazione IN (8,15)

The execution plan doesn't propose any index suggestion.. The record on the affected huges tables are:

  • eventi: 12250946
  • waypoint_periodi: 650703
  • eventi_kml_polygon: 1500040
  • kml_polygon: 21870
  • kml: 9246

This is the execution plan: Execution plan brentozar

Who could help me please?

I tried to analize index, tables ecc.. but i didn't find the solution.. I image that there is an index that can help my query

CodePudding user response:

I'm curious...Try to bring this into your query designer(CTRL-SHIFT-Q) and click the columns you want to return and see if it runs.

SELECT        *
FROM            (SELECT        TOP (2147483647) *
                          FROM            eventi_kml_polygon AS ekp INNER JOIN
                                                    eventi AS e ON e.id = ekp.id_evento INNER JOIN
                                                    sistema_eventi AS es ON es.evento = e.id_evento INNER JOIN
                                                    waypoint_periodi AS wp ON e.dataora BETWEEN wp.dataora_inizio AND wp.dataora_fine INNER JOIN
                                                    modelli AS m ON m.id = wp.id_modello
                          WHERE        (m.id = 224882) AND (es.operazione IN (8, 15))) AS derivedtbl_1 INNER JOIN
                         kml AS k INNER JOIN
                         kml_polygon AS kp ON k.id = kp.id_kml INNER JOIN
                         waypoint AS w ON w.id_kml = k.id ON derivedtbl_1.id_kml_polygon = kp.id AND derivedtbl_1.id_waypoint = w.id

CodePudding user response:

I think the biggest problem is the key lookup caused because ix_eventi is not covering. Try modifying that index to include the columns that are referenced, such as lat, lon, dateora, and so forth. Of course you will need to determine if this will impede other queries on your server.

  • Related