I have a query query that is taking too long to execute (almost 2min) and needs to be optimized:
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
Tables template :
ExecutionPlain:
Note:DoyouhaveanyotherwaytogetExecutionPlain?IntextformatIhadtroublepostinghere!
This query consists of grouping Employees with a given History by their Positions in each City . However, it needs to be filtered by:
where Funcionario.EmpresaId = @EmpresaId
); and Cidade.Uf = @Uf
); and Cidade.Id in ( {1} )
) - this {1}
is a string that will be changed by the list of Ids
of desired cities And there are also some conditions to be observed about the History of each Employee that will enter the count:
and FH.TipoHistoricoId = @TipoHistoricoId
); and year(FH.DataRegistro) = @AnoBase
); p>
- So from the subquery that gets the Id of the last Employee history record with the History Type and the Base Year .
Lastly, and also the reason for the second subquery, is that the Employee can not have any of the other History Types that will be informed after the Type of History that is searched for in it. An Example: The Employee can not have the History Type "Transferred" after searching the History Type "Changed Position."
This is the whole question and I ask you what could be done to optimize query response time?