SQL Server view optimization help (repeated subqueries, case when, and so on...) -
i need optimizing mssql view is, honestly, little complex knowledge.
the view working rewrite using less subqueries or better structure in order simplify , use less server resources.
the main issue case when same subquery repeated 4 times... tried understand if can put in variable , use case instead of repeating query each time seems not possible me...
here's query
select dbo.macchine.id_macchina, [...] dbo.view_cantieri.indirizzo, (select top (1) data_fine dbo.manutenzioni (id_macchina = dbo.macchine.id_macchina) order data_fine desc) ultima_manutenzione, datediff(day, (select top (1) data_fine dbo.manutenzioni manutenzioni_1 (id_macchina = dbo.macchine.id_macchina) order data_fine desc), getdate()) data_diff, (case when stato = 0 'grey' when stato = 2 'black' when stato = 1 (case when datediff(day, (select top (1) data_fine dbo.manutenzioni manutenzioni_1 (id_macchina = dbo.macchine.id_macchina) order data_fine desc), getdate()) >= 90 'red' when datediff(day, (select top (1) data_fine dbo.manutenzioni manutenzioni_1 (id_macchina = dbo.macchine.id_macchina) order data_fine desc), getdate()) >= 80 'orange' when datediff(day, (select top (1) data_fine dbo.manutenzioni manutenzioni_1 (id_macchina = dbo.macchine.id_macchina) order data_fine desc), getdate()) >= 60 'yellow' when (select top (1) data_fine dbo.manutenzioni manutenzioni_1 (id_macchina = dbo.macchine.id_macchina) order data_fine desc) null 'red' else 'green' end) end) colore dbo.macchine inner join dbo.macchine_modelli on dbo.macchine.id_modello = dbo.macchine_modelli.id_modello inner join dbo.macchine_tipologie on dbo.macchine_modelli.id_tipologia = dbo.macchine_tipologie.id_tipologia inner join dbo.view_cantieri on dbo.macchine.id_cantiere = dbo.view_cantieri.id_cantiere inner join dbo.macchine_produttori on dbo.macchine_modelli.id_produttore = dbo.macchine_produttori.id_produttore inner join dbo.clienti on dbo.view_cantieri.id_cliente = dbo.clienti.id_cliente (dbo.macchine._del = 'n')
any suggestion appreciated, if need more information db try provide it...
i notice using top(1), must 2005 or above. can keep result of datediff in outer apply subquery evaluated once.
select m.id_macchina, [...], v.indirizzo, ma.data_fine ultima_manutenzione, ma.data_diff, case when stato = 0 'grey' when stato = 2 'black' when stato = 1 case when ma.data_diff >= 90 'red' when ma.data_diff >= 80 'orange' when ma.data_diff >= 60 'yellow' when ma.data_fine null 'red' else 'green' end end colore dbo.macchine m inner join dbo.macchine_modelli on m.id_modello = i.id_modello inner join dbo.macchine_tipologie t on i.id_tipologia = t.id_tipologia inner join dbo.view_cantieri v on m.id_cantiere = v.id_cantiere inner join dbo.macchine_produttori p on i.id_produttore = p.id_produttore inner join dbo.clienti c on v.id_cliente = c.id_cliente outer apply (select top (1) ma.data_fine, datediff(day, ma.data_fine, getdate()) data_diff dbo.manutenzioni ma ma.id_macchina = m.id_macchina order ma.data_fine desc) ma m._del = 'n'
Comments
Post a Comment