Bonjour,

Nous sommes sur un serveur SQL 2008 10.0.5500

Voici une requête qu'on m'a donné :

Code MÉTHODE 1 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
with 
Factures_CAP as 
    (select 
           aph.APVNDR  as  No_Fourn_,
           aph.APINV  as  No_Facture,
           aph.APGLDT as Date_Transa,
           apl.PLSOUR  as  No_Journal,
           apl.PLAMT  as  Mt_Paye___Dev_,
           aph.PHTXBA  as  Mt_Taxes___Dev_
     from 
           lassonde.dbo.aph aph,
           lassonde.dbo.apl apl
     where 
           ((((aph.APVNDR = apl.PLVNDR) and (aph.APINV = apl.PLINV)) and (aph.APCMPY = apl.PLCMPY)) and (aph.PHDCSQ = apl.PLDCSQ))
           --and aph.APGLDT >= 20130701 and apl.PLSOUR like 'RP%'
    ),
Regroupement_Transport as 
    (select 
           ltrh.RHRGTR  as  Code_Regroupement,
           ltrh.RHCARR  as  Code_Transporteur,
           ltrc.RCREF1  as  No_Commande,
           ltrc.RCINVV  as  No_Facture_Fourn_,
           ltrc.RCEAMT  as  Mt_Trsp_Estime,
           ltrc.RCPAMT  as  Mt_Trsp_Paye,
           ltca.CANAME  as  Nom_Transporteur,
           ltca.CAVEND  as  No_Fourn_,
           ltrc.RCWGHT  as  Poids_Commande
     from 
           lassonde.dbo.ltrc ltrc,
           lassonde.dbo.LTRH ltrh,
           lassonde.dbo.ltca ltca
     where 
           ((ltrh.RHRGTR = ltrc.RCRGTR) and (ltrh.RHSUFF = ltrc.RCSUFF)) and 
           (ltrh.RHCARR = ltca.CACARR)
    )
select 
       Factures_CAP.No_Fourn_  as  No_Fourn_,
       avm.VNDNAM  as  VNDNAM,
       avm.VCURR  as  VCURR,
       Factures_CAP.No_Journal  as  No_Journal,
       Factures_CAP.Date_Transa  as  Date_Transa,
       Regroupement_Transport.Code_Transporteur  as  Code_Transporteur,
       Regroupement_Transport.Nom_Transporteur  as  Nom_Transporteur,
       Regroupement_Transport.No_Facture_Fourn_  as  No_Facture_Fourn_,
       Regroupement_Transport.Code_Regroupement  as  Code_Regroupement --,
 from 
       Factures_CAP,
       lassonde.dbo.avm avm,
       Regroupement_Transport
 where 
       (Factures_CAP.No_Journal like 'RP%') and 
       (Regroupement_Transport.Code_Transporteur <> 'UTRUCK') and 
       (Factures_CAP.Date_Transa >= 20130701) and 
       ((Regroupement_Transport.No_Fourn_ = Factures_CAP.No_Fourn_) and (Regroupement_Transport.No_Facture_Fourn_ = Factures_CAP.No_Facture)) and 
       (Factures_CAP.No_Fourn_ = avm.VENDOR)
 group by 
       Factures_CAP.No_Fourn_,
       avm.VNDNAM,
       avm.VCURR,
       Factures_CAP.No_Journal,
       Factures_CAP.Date_Transa,
       Regroupement_Transport.Code_Transporteur,
       Regroupement_Transport.Nom_Transporteur,
       Regroupement_Transport.No_Facture_Fourn_,
       Regroupement_Transport.Code_Regroupement
 order by 
       No_Fourn_ asc,
       VNDNAM asc,
       VCURR asc,
       No_Journal asc,
       Date_Transa asc,
       Code_Transporteur asc,
       Nom_Transporteur asc,
       No_Facture_Fourn_ asc,
       Code_Regroupement asc



Hier l'exécution de cette requête n'aboutissait pas du tout !! Elle roulait indéfiniement... Les deux requêtes des WITH retournent chacune environ un million et demie de records.... je sais, c'est beaucoup, mais si on éxécutait chaque with à part, chaque requête prenait que une ou deux secondes, alors qu'ensemble, on obtenait aucun résultat.

Nous avons donc modifié un peu la structure et avons changé les WITH par des tables temporaires, comme suit :

Code MÉTHODE 2 : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
drop table #Factures_CAP  
 
select 
           aph.APVNDR  as  No_Fourn_,
           aph.APINV  as  No_Facture,
           aph.APGLDT as Date_Transa,
           apl.PLSOUR  as  No_Journal,
           apl.PLAMT  as  Mt_Paye___Dev_,
           aph.PHTXBA  as  Mt_Taxes___Dev_
     into #Factures_CAP   
     from 
           lassonde.dbo.aph aph,
           lassonde.dbo.apl apl
     where 
           ((((aph.APVNDR = apl.PLVNDR) and (aph.APINV = apl.PLINV)) and (aph.APCMPY = apl.PLCMPY)) and (aph.PHDCSQ = apl.PLDCSQ))
           --and aph.APGLDT >= 20130701 and apl.PLSOUR like 'RP%'
 
 
 select 
           ltrh.RHRGTR  as  Code_Regroupement,
           ltrh.RHCARR  as  Code_Transporteur,
           ltrc.RCREF1  as  No_Commande,
           ltrc.RCINVV  as  No_Facture_Fourn_,
           ltrc.RCEAMT  as  Mt_Trsp_Estime,
           ltrc.RCPAMT  as  Mt_Trsp_Paye,
           ltca.CANAME  as  Nom_Transporteur,
           ltca.CAVEND  as  No_Fourn_,
           ltrc.RCWGHT  as  Poids_Commande
     into #Regroupement_Transport     
     from 
           lassonde.dbo.ltrc ltrc,
           lassonde.dbo.LTRH ltrh,
           lassonde.dbo.ltca ltca
     where 
           ((ltrh.RHRGTR = ltrc.RCRGTR) and (ltrh.RHSUFF = ltrc.RCSUFF)) and 
           (ltrh.RHCARR = ltca.CACARR)          
 
 select 
       Factures_CAP.No_Fourn_  as  No_Fourn_,
       avm.VNDNAM  as  VNDNAM,
       avm.VCURR  as  VCURR,
       Factures_CAP.No_Journal  as  No_Journal,
       Factures_CAP.Date_Transa  as  Date_Transa,
       Regroupement_Transport.Code_Transporteur  as  Code_Transporteur,
       Regroupement_Transport.Nom_Transporteur  as  Nom_Transporteur,
       Regroupement_Transport.No_Facture_Fourn_  as  No_Facture_Fourn_,
       Regroupement_Transport.Code_Regroupement  as  Code_Regroupement 
 
 from 
       #Factures_CAP Factures_CAP,
       lassonde.dbo.avm avm,
       #Regroupement_Transport Regroupement_Transport
 where 
       (Factures_CAP.No_Journal like 'RP%') and 
       (Regroupement_Transport.Code_Transporteur <> 'UTRUCK') and 
       (Factures_CAP.Date_Transa >= 20130701) and 
       ((Regroupement_Transport.No_Fourn_ = Factures_CAP.No_Fourn_) and (Regroupement_Transport.No_Facture_Fourn_ = Factures_CAP.No_Facture)) and 
       (Factures_CAP.No_Fourn_ = avm.VENDOR)
 group by 
       Factures_CAP.No_Fourn_,
       avm.VNDNAM,
       avm.VCURR,
       Factures_CAP.No_Journal,
       Factures_CAP.Date_Transa,
       Regroupement_Transport.Code_Transporteur,
       Regroupement_Transport.Nom_Transporteur,
       Regroupement_Transport.No_Facture_Fourn_,
       Regroupement_Transport.Code_Regroupement
 order by 
       No_Fourn_ asc,
       VNDNAM asc,
       VCURR asc,
       No_Journal asc,
       Date_Transa asc,
       Code_Transporteur asc,
       Nom_Transporteur asc,
       No_Facture_Fourn_ asc,
       Code_Regroupement asc

Et là, l'exécution était quasi instantanée.

Mais le plus étrange, et c'est là que je veux en venir, c'est qu'après avoir fait rouler notre method 2, la méthode 1 s'est mise à rouler rapidement aussi. Nous n'avons rien changé au niveau du serveur ? Pas touché aux index, ni à la mémoire...

J'aimerais comprendre pourquoi, tout à coup, la requête s'exécute en 2 secondes, alors que quelques minutes avant, elle ne finissait plus... Car, je crains que ça recommence....

Merci,