0

複数のテーブルから複数のカウントが必要ですが、すべてのテーブルが同じテーブルに結合され、同じ where 句があります。正しい結果が得られますが、非常に大きくて遅いクエリで、これを改善する方法を学びたいです。

テーブルからカウントを取得する必要があります:

  • オコレンシアス
  • フォローアップ
  • PropostasComerciais
  • プロポタスデロカカオ
  • PedidosDeVendas

そして 3 SUM、すべて PedidosDeVendas テーブルから。

このクエリはすべて次のものと結合します: - Empresas - PedidosDeVendaXItens

すべてのクエリに同じ WHERE 句があります。 - WHERE SolicitanteID = {User ID} AND Data >= '{Initial Date}' AND Data <= '{Final Date}'

これは、必要なすべての結果を返すクエリです。

SELECT U.[ID] AS UniqKey, U.NomeCompleto, R.Regiao,    

(SELECT Count(*)    
FROM Ocorrencias O    
INNER JOIN Nomes N ON (O.ClientesID = N.Codigo AND O.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE O.UsuariosID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND O.Data >= '2012-12-01 00:00:00' AND O.Data <= '2012-12-31 23:59:59') AS Ocorrencias,    

(SELECT Count(*)    
FROM FollowUp F    
INNER JOIN Nomes N ON (F.ClienteID = N.Codigo AND F.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND Data >= '2012-12-01 00:00:00' AND Data <= '2012-12-31 23:59:59') AS FollowUps,    

(SELECT Count(*)    
FROM v_PropostaComercial PC    
INNER JOIN Nomes N ON (PC.ClienteID = N.Codigo AND PC.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE PC.SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND PC.Data >= '2012-12-01 00:00:00' AND PC.Data <= '2012-12-31 23:59:59') AS PropostasComerciais,                  

(SELECT Count(*)    
FROM PropostaDeLocacao PL    
INNER JOIN Nomes N ON (PL.ClienteID = N.Codigo AND PL.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE PL.ResponsavelID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND PL.Data >= '2012-12-01 00:00:00' AND PL.Data <= '2012-12-31 23:59:59') AS PropostasDeLocacao,    

(SELECT Count(*)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
WHERE PV.SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') AS PedidosDeVenda,                   

(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Empresas N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND MR.ID_Regiao = R.uniqKey
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalPedidosDeVenda,    

(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND N.TipoEmpresa = 'PU'    
AND PV.ClienteID <> U.ClienteID    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalVendasPublicas,    


(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND N.TipoEmpresa = 'PR'    
AND PV.ClienteID <> U.ClienteID    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalVendasPrivadas,    

(SELECT SUM(PVI.Valor)    
FROM PedidosDeVenda PV    
INNER JOIN Nomes N ON (PV.ClienteID = N.Codigo AND PV.SubCadastro = N.SubCadastro)    
INNER JOIN Municipios_Regiao MR ON N.ID_Municipio = MR.ID_Municipio    
INNER JOIN PedidosDeVendaXItens PVI ON PVI.PedidoDeVendaID = PV.[ID]    
INNER JOIN Modelos M ON PVI.ProdutoID = M.[ID]    
WHERE PV.SolicitanteID = U.[ID]    
AND PV.ClienteID = U.ClienteID    
AND MR.ID_Regiao = R.uniqKey    
AND PV.Data >= '2012-12-01 00:00:00' AND PV.Data <= '2012-12-31 23:59:59') As TotalVendasProprias    

FROM Usuarios U    
INNER JOIN Regioes_Usuario RU ON U.[ID] = RU.ID_Usuario    
INNER JOIN Regioes R ON RU.ID_Regiao = R.uniqKey    
WHERE U.Representante = 64        
ORDER BY R.Regiao, U.NomeCompleto;

クエリをより速く、より短くするためのトリックはありますか?

4

2 に答える 2

1

外側の結合が発生するたびにネストされた各クエリが実行されると思われるため、メイン クエリ内の select 内で集中的なネストされたクエリを実行しないでください。

代わりに、変数を宣言して最初に代入することを考えてください。

お気に入り

declare @outsidepredicate int, @Count1 int, @Count 2 int;

select @outsidepredicate = value from sharedtable for expressions

select @Count1 = count(*) 
from table1 t (nolock) 
 join table2 tt (nolock) on t.idshared = tt.idshared
where outsidepredicatecolumn = @outsidepredicate

select @Count2 = count(*) 
from table3 t (nolock) 
 join table4 tt (nolock) on t.idshared = tt.idshared
where outsidepredicatecolumn = @outsidepredicate

次に、次のように通常のテストを実行できます。

select 
    @Count1
,   @Count2
,   valuefrommain
from tableMain m (nolock) 
 join tableSide s (nolock) on m.idshared = s.idshared
于 2013-01-16T18:05:42.553 に答える