Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Performance problem with more than one COUNT(DISTINCT ...) in a query

$
0
0

Hi,

(I hope this is the good forum).

In the following query, I have 2 Count Distinct on 2 different fields of the same table.  Execution time is okay (2 s) with one or the other COUNT(DISCTINCT ...) in the SELECT clause, but is not tolerable (12 s) with both together in the query! I have a similar case with 3 counts: 4 s each, 36 s when together!

I've looked at the execution plan, and it seems that with two count distinct, SQL server sorts the table twice before joining the results.

I do not have much experience with SQL server optimization, and I don't know what to improve and how. The SQL is generated by Business Objects, I have few possibilities to tune it. The most direct way would be to execute 2 different queries, but I'd like to avoid it.

Any advice?

SELECT
  DIM_MOIS.DATE_DEBUT_MOIS,
  DIM_MOIS.NUM_ANNEE_MOIS,
  DIM_DEMANDE_SCD.CAT_DEMANDE,
  DIM_APPLICATION.LIB_APPLICATION,
  DIM_DEMANDE_SCD.CAT_DEMANDE ,
  count(distinct FAITS_DEMANDE.NB_DEMANDE_FLUX),
  count(distinct FAITS_DEMANDE.NB_DEMANDE_RESOL_NIV1)

FROM
  ALIM_SID.DIM_MOIS INNER JOIN ALIM_SID.DIM_JOUR ON (DIM_JOUR.SEQ_MOIS=DIM_MOIS.SEQ_MOIS)
   INNER JOIN ALIM_SID.FAITS_DEMANDE ON (FAITS_DEMANDE.SEQ_JOUR=DIM_JOUR.SEQ_JOUR)
   INNER JOIN ALIM_SID.DIM_APPLICATION ON (FAITS_DEMANDE.SEQ_APPLICATION=DIM_APPLICATION.SEQ_APPLICATION)
   INNER JOIN ALIM_SID.DIM_DEMANDE_SCD ON (FAITS_DEMANDE.SEQ_DEMANDE_SCD=DIM_DEMANDE_SCD.SEQ_DEMANDE_SCD)
 
WHERE
  ( ( DIM_MOIS.NUM_ANNEE_MOIS ) >201301
  )
GROUP BY
  DIM_MOIS.DATE_DEBUT_MOIS,
  DIM_MOIS.NUM_ANNEE_MOIS,
  DIM_DEMANDE_SCD.CAT_DEMANDE,
  DIM_APPLICATION.LIB_APPLICATION


Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>