6
I have a query query that is taking too long to run (almost 2min) and need to optimize it:
select Cidade.Id as CidadeId, Cidade.Nome as CidadeNome,
Cargo.Descricao as CargoDescricao, '{0}' as Grupo, Count(*) as Qtd
from Funcionario
inner join Cargo on Cargo.Id = Funcionario.CargoId
inner join Bairro on Bairro.Id = Funcionario.BairroId
inner join Cidade on Cidade.Id = Bairro.CidadeId
inner join FuncionarioHistorico on FuncionarioHistorico.Id in (
select Top 1 FuncionarioHistorico.Id from FuncionarioHistorico FH
where FH.FuncionarioId = Funcionario.Id
and FH.EmpresaId = Funcionario.EmpresaId
and year(FH.DataRegistro) = @AnoBase
and FH.TipoHistoricoId = @TipoHistoricoId
and FH.Id not in ( select NP.Id from FuncionarioHistorico NP
where NP.FuncionarioId = Funcionario.Id
and NP.EmpresaId = Funcionario.EmpresaId
and NP.DataRegistro >= FH.DataRegistro
and year(NP.DataRegistro) = year(FH.DataRegistro)
and NP.TipoHistoricoId in ({2}) )
order by FH.DataRegistro desc )
inner join TipoHistorico on TipoHistorico.Id = FuncionarioHistorico.TipoHistoricoId
where Funcionario.EmpresaId = @EmpresaId
and Cargo.Id <> @CargoId
and Cidade.Id in ( {1} )
and Cidade.Uf = @Uf
group by Cidade.Id, Cidade.Nome, Cargo.Descricao
order by Cidade.Nome
Table model:
Execution Plain:
Note: Is there any other way to get the Execution Plain? In text format I had problems posting here!
Such consultation shall consist of grouping the amount of Employees, who own a particular Historic, for their Posts in each City. However, it needs to be filtered by:
- Enterprise (
where Funcionario.EmpresaId = @EmpresaId
); - State (
and Cidade.Uf = @Uf
); - Cities to be researched (
and Cidade.Id in ( {1} )
) - that{1}
is a string that will be changed by the list ofIds
of the desired cities.
And there are also some conditions to be observed about the Historic of each Official that will enter the countdown:
- It’s for a Type of History specific (
and FH.TipoHistoricoId = @TipoHistoricoId
); The Official will need to have the Type of History informed on Year you want to perform the search (
and year(FH.DataRegistro) = @AnoBase
);- That’s why the subconsultation that gets the Id of the last history record of the Official with the Type of History desired and the Base Year.
Finally, and also the motive for the second sub-concession, is that the Employee cannot have any of the other Types of History that will be informed after the Type of History that is sought in it. An Example: The Employee may not have the Type of History "Transferred" after searching the Type of History "Office Altered".
That is the whole question and I ask you what could be done to optimize the response time of the consultation?
Posting the table structure and Execution Plan will surely help :)
– gmsantos