Vue SQL Server avec un 'select où x n'est pas nul' prend un certain time à compléter

J'ai une vue complexe, qui est décrite ici: Vue de plusieurs tables. Besoin d'enlever "double" défini par 1 table

J'ai utilisé un Cross Apply dedans, et le code est le suivant: (veuillez vérifier l'URL ci-dessus pour comprendre la vue)

  SELECT dbo.InstellingGegevens.INST_SUBTYPE , dbo.InstellingGegevens.INST_BRON , dbo.InstellingGegevens.INST_INSTELLINGSNUMMER , dbo.InstellingGegevens.INST_NAAM , dbo.InstellingGegevens.INST_KORTENAAM , dbo.InstellingGegevens.INST_VESTIGINGSNAAM , dbo.InstellingGegevens.INST_ROEPNAAM , dbo.InstellingGegevens.INST_STATUUT , dbo.InstellingGegevens.ONDERWIJSNIVEAU_REF , dbo.InstellingGegevens.ONDERWIJSSOORT_REF , dbo.InstellingGegevens.DATUM_TOT , dbo.InstellingGegevens.DATUM_VAN , dbo.InstellingGegevens.VERBOND_REF , dbo.InstellingGegevens.VSKO_LID , dbo.InstellingGegevens.NET_REF , dbo.Instellingen.Inst_ID , dbo.Instellingen.INST_TYPE , dbo.Instellingen.INST_REF , dbo.Instellingen.INST_LOC_REF , dbo.Instellingen.INST_LOCNR , dbo.Instellingen.Opt_KalStandaard , dbo.InstellingTelecom.INST_TEL , dbo.InstellingTelecom.INST_FAX , dbo.InstellingTelecom.INST_EMAIL , dbo.InstellingTelecom.INST_WEB , dbo.InstellingAdressen.SOORT , dbo.InstellingAdressen.STRAAT , dbo.InstellingAdressen.POSTCODE , dbo.InstellingAdressen.GEMEENTE , dbo.InstellingAdressen.GEM_REF , dbo.InstellingAdressen.FUSIEGEM_REF , dbo.InstellingAdressen.FUSIEGEM , dbo.InstellingAdressen.ALFA_G , dbo.InstellingAdressen.PROVINCIE , dbo.InstellingAdressen.BISDOM , dbo.InstellingAdressen.ARRONDISSEMENT , dbo.InstellingAdressen.GEWEST , dbo.InstellingContPersDirecteurs.AANSPREKING , dbo.InstellingContPersDirecteurs.CONTACTPERSOON , dbo.InstellingContPersDirecteurs.FUNCTIE , InstellingLogin.Inst_Gebruikersnaam , InstellingLogin.Inst_Concode , InstellingLogin.Inst_DirCode , InstellingLogin.DOSSNR , InstellingLogin.Instelling_ID FROM dbo.InstellingGegevens RIGHT OUTER JOIN dbo.Instellingen ON dbo.InstellingGegevens.INST_TYPE = dbo.Instellingen.INST_TYPE AND dbo.InstellingGegevens.INST_REF = dbo.Instellingen.INST_REF AND dbo.InstellingGegevens.INST_LOC_REF = dbo.Instellingen.INST_LOC_REF AND dbo.InstellingGegevens.INST_LOCNR = dbo.Instellingen.INST_LOCNR LEFT OUTER JOIN dbo.InstellingTelecom ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingTelecom.INST_TYPE AND dbo.InstellingGegevens.INST_REF = dbo.InstellingTelecom.INST_REF AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingTelecom.INST_LOC_REF LEFT OUTER JOIN dbo.InstellingAdressen ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingAdressen.INST_TYPE AND dbo.InstellingGegevens.INST_REF = dbo.InstellingAdressen.INST_REF AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingAdressen.INST_LOC_REF LEFT OUTER JOIN dbo.InstellingContPersDirecteurs ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingContPersDirecteurs.INST_TYPE AND dbo.InstellingGegevens.INST_REF = dbo.InstellingContPersDirecteurs.INST_REF AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingContPersDirecteurs.INST_LOC_REF CROSS APPLY (SELECT TOP (1) * FROM InstellingLogin AS il WHERE Instellingen.INST_LOC_REF = il.Inst_Loc_REF AND Instellingen.INST_LOCNR = il.Inst_Loc_Nr AND Instellingen.INST_REF = il.Inst_InstellingIKON_REF AND Instellingen.INST_TYPE = il.Inst_InstellingIKONType ORDER BY CASE WHEN il.datum_tot IS NULL THEN 0 ELSE 1 END , il.datum_tot DESC) InstellingLogin 

Cette vue me renvoie environ 5.5k lignes, en environ 1s. C'est rapide!

Toutefois!

Lorsque j'appelle cette vue avec une clause where:

 SELECT * FROM [Tink].[dbo].[InstellingAlleDetails] where gemeente is not null and (DATUM_TOT is null or DATUM_TOT > GETDATE()) order by GEMEENTE, POSTCODE,STRAAT, INST_NAAM 

il faut 1min 20s pour returnner toutes les lignes.

Quand je gemeente is not null tomber la gemeente is not null partie gemeente is not null , cela prend encore 1s.

Gemeente est un varchar (255). Je l'ai également essayé avec Inst_Naam is not null et cela a pris aussi environ 1min 30s.

Pourquoi ce is not null prendre autant de time? Et plus important encore: comment puis-je résoudre ce problème?

Je ne sais pas pourquoi. SQL Server propose probablement un plan de requête qui n'est pas très bon.

Vous pouvez essayer d'exécuter d'abord la requête sans que gemeente is not null et placer le résultat dans une table temporaire, puis interroger la table temporaire avec gemeente is not null .

 select * into #TempTable from YourView select * from #TempTable where gemeente is not null drop table #TempTable 

Vérifiez d'abord les plans d'exécution sur la requête avec et sans qui n'est pas null et voyez les différences.

BTW est l'une de ces jointures à d'autres vues? Cela peut causer d'énormes problèmes de performance.