IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement SQL Server Discussion :

Optimisation d'un Procédure Stockée


Sujet :

Développement SQL Server

  1. #1
    Candidat au Club
    Femme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Novembre 2012
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2012
    Messages : 1
    Points : 3
    Points
    3
    Par défaut Optimisation d'un Procédure Stockée
    Bonjour, ci dessous un PS que j'ai crée, et le temps d’exécution est + 10min, est ce que quelqu'un peut m'aider s'il vous plait
    Code : 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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
     
    USE [mabase]
    GO
     
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
     
    ALTER PROCEDURE [dbo].[mg_getPassagers]
    (                        
      @comm_numeroCommande		VARCHAR(30),
      @comm_dateDebutResa		INT,
      @comm_dateFinResa			INT,
      @comm_dateDebutDepart		INT,
      @comm_dateFinDepart		INT,
      @comm_client				INT,
      @comm_typeCommande		INT,
      @comm_statutresa			INT,
      @comm_statutpaiement		INT,
      @comm_statuttraitement	INT,
      @comm_statutfacturation	BIT,
      @prest_typePrestation		INT,
      @prest_statutresa			INT,
      @prest_fournisseur		INT,
      @paysDestination			INT,
      @codeAnalytique			VARCHAR(30) = '',
      @idApporteurAffaire		INT = NULL ,
      @prest_supplement			INT = 0,
      @idGroupProduit			INT = 0,
      @user						INT = 0,
      @prest_dateDebutPrest		INT = NULL,
      @prest_dateFinPrest		INT = NULL,
      @idCategoriePrestation	INT = 0,
      @codeProduit				VARCHAR(100) = '',
      @listeProduits			VARCHAR(400)='',
      @listeOffres				VARCHAR(400)='',
      @listeTarifsAdultes		VARCHAR(400)='',
      @listeTarifsEnfants		VARCHAR(400)=''
    )
    AS 
    BEGIN   
     
    DECLARE @psgListTmp TABLE  (id INT, civilite VARCHAR(10), libCivilite VARCHAR(100), commentaire VARCHAR(2000),datenaissance INT,groupe VARCHAR(100), nom VARCHAR(30), prenom VARCHAR(30), typePax INT
                       , numeroPiece VARCHAR(30), telephone VARCHAR(50), dateFinValidite INT,  idCommande INT, nomProduit VARCHAR(400), numeroCommande VARCHAR(30), referenceExterne VARCHAR(30)
    				   , dateDepart VARCHAR(30),dateDepartInt INT, statutConfirmation INT, categorie VARCHAR(200), dateCreation DATETIME, codeAnalytique VARCHAR(30), raisonSociale VARCHAR(100)
    				   , apporteurAffaire  VARCHAR(100), statutTraitement varchar(100), activite VARCHAR(400), typeLogement VARCHAR(400)
    				   , nomAgentClient VARCHAR(100), codeApporteurAffaire VARCHAR(100)
    				   , codeProduit VARCHAR(100)
    				   , prixParPersonne NUMERIC(18,3)
    				   , offreExiste BIT, tarifAdulteExiste BIT, tarifEnfantExiste BIT)
    DECLARE  @psgListCategorie TABLE (idCategorie INT, typeProduit INT, nomCategorie VARCHAR(400), idPassager INT, activite VARCHAR(100), typeLogement VARCHAR(400))
    DECLARE @tmpValeur VARCHAR(30)
    DECLARE @cmdTmpOffre TABLE(idCommande INT)
    DECLARE @cmdTarifAdulte TABLE(idCommande INT)
    DECLARE @cmdTarifEnfant TABLE(idCommande INT)
     
    DECLARE @countTableOffre BIT, @countTableAdulte BIT, @countTableEnfant BIT
    SET @countTableOffre = 0
    SET @countTableAdulte = 0
    SET @countTableEnfant = 0
     
    DECLARE  @cmdTmp TABLE (id INT, numero VARCHAR(50), client INT, idApporteurAffaire INT, statuTraitement INT, nomProduit VARCHAR(200), referenceExterne VARCHAR(100), dateDepart INT, dateCreation DATETIME, codeAnalytique VARCHAR(50), nomAgentClient VARCHAR(100), codeApporteurAffaire VARCHAR(100), codeProduit VARCHAR(100))
    INSERT INTO @cmdTmp (id, numero, client, idApporteurAffaire, statuTraitement, nomProduit, referenceExterne, dateDepart, dateCreation, codeAnalytique, nomAgentClient, codeApporteurAffaire, codeProduit)
    SELECT cmde.id, cmde.numero, cmde.client, cmde.idApporteurAffaire, cmde.statuTraitement, cmde.nomProduit, cmde.referenceExterne, cmde.dateDepart, cmde.dateCreation, cmde.codeAnalytique, cmde.nomAgentClient, cmde.codeApporteurAffaire, cmde.codeProduit
    FROM commande cmde WITH(NOLOCK)
    WHERE
    	(@comm_numeroCommande = ''  OR ( @comm_numeroCommande <> '' AND cmde.numero LIKE '%' + LTRIM(RTRIM(@comm_numeroCommande)) + '%'))       
    	AND (@comm_dateFinResa IS NULL  OR (cmde.dateCreation < DATEADD("D", 1, dbo.inttodate(@comm_dateFinResa)) AND @comm_dateFinResa IS NOT NULL))    
    	AND (@comm_dateDebutResa IS NULL OR (cmde.dateCreation >= dbo.inttodate(@comm_dateDebutResa)        AND @comm_dateDebutResa IS NOT NULL))      
    	AND (@comm_dateFinDepart IS NULL OR (cmde.dateDepart <= @comm_dateFinDepart      AND @comm_dateFinDepart IS NOT NULL))      
    	AND (@comm_dateDebutDepart IS NULL OR (cmde.dateDepart >= @comm_dateDebutDepart      AND @comm_dateDebutDepart IS NOT NULL))      
    	AND (@comm_typeCommande = 0   OR (@comm_typeCommande <> 0          AND cmde.type = @comm_typeCommande))      
    	AND (@comm_statutresa = 0   OR (@comm_statutresa <> 0          AND cmde.statuReservation = @comm_statutresa))      
    	AND (@comm_statutpaiement = 0  OR (@comm_statutpaiement <> 0         AND cmde.statuPayement = @comm_statutpaiement))      
    	AND (@comm_statuttraitement = 0  OR (@comm_statuttraitement <> 0         AND cmde.statuTraitement = @comm_statuttraitement))      
    	AND (@comm_statutfacturation IS NULL OR (statuFacturation = @comm_statutfacturation     AND @comm_statutfacturation IS NOT NULL))           
     
    	AND (@paysDestination IS NULL OR (cmde.idpaysDestination = @paysDestination AND @paysDestination IS NOT NULL))
    	AND (@codeAnalytique = ''  OR ( @codeAnalytique <> ''				AND cmde.codeAnalytique LIKE '%' +LTRIM(RTRIM(@codeAnalytique))+ '%' )) 
    	AND (COALESCE(@codeProduit, '') = ''  OR ( @codeProduit <> ''    AND cmde.codeProduit LIKE '%' +LTRIM(RTRIM(@codeProduit))+ '%' ))       
    	AND (@idApporteurAffaire is NULL   OR (@idApporteurAffaire is not null    AND cmde.idApporteurAffaire = @idApporteurAffaire))
    	AND (COALESCE(@user, 0) = 0 OR cmde.[USER] = @user)
     
    INSERT INTO @psgListTmp (id, civilite, libCivilite, commentaire,datenaissance,groupe, nom, prenom, typePax, numeroPiece, telephone, dateFinValidite, idCommande, nomProduit, numeroCommande, referenceExterne, dateDepart,dateDepartInt, statutConfirmation, dateCreation, codeAnalytique, raisonSociale, apporteurAffaire, statutTraitement, nomAgentClient, codeApporteurAffaire, codeProduit)
    SELECT DISTINCT  p.id AS id
    				,p.civilite AS civilite
    				,civ.valeur AS libCivilite
    				,p.commentaire AS commentaire
    				,p.dateNaissance AS datenaissance
    				,p.groupePassager AS groupe
    				,p.nom AS nom
    				,p.prenom AS prenom
    				,p.typePax AS typePax
    				,p.numeroPieceIdentite numeroPiece
    				,p.telephone
    				,COALESCE(p.dateFinValidite,0) 
    				,commandeprestation.idCommande
    				,cmd.nomProduit AS nomProduit
    				,cmd.numero AS numeroCommande
    				,cmd.referenceExterne AS referenceExterne
    				,( CONVERT (VARCHAR(30) ,dbo.intToDate(cmd.dateDepart), 103)) AS dateDepart
    				,cmd.dateDepart AS dateDepartInt
    				,commandeprestation.statutConfirmation
    				,cmd.dateCreation AS dateCreation
    				,cmd.codeAnalytique  AS codeAnalytique
    				,client.raisonSociale AS  raisonSociale
    				, Coalesce (apporteurAffaire.raisonSociale,'') AS apporteurAffaire
    				,ValTraitement.valeur
    				, cmd.nomAgentClient
    				, cmd.codeApporteurAffaire
    				, cmd.codeProduit
    FROM @cmdTmp cmd INNER JOIN commandePassager p WITH(NOLOCK)  ON (cmd.id = p.idCommande)
    				  LEFT JOIN client WITH(NOLOCK)  ON client.id = cmd.client
    				  LEFT JOIN client AS apporteurAffaire WITH(NOLOCK) ON apporteurAffaire.id = cmd.idApporteurAffaire
    				  LEFT JOIN commandeprestation WITH(NOLOCK)  ON commandeprestation.idCommande = cmd.id 
    				  LEFT JOIN produit WITH(NOLOCK) ON produit.id =commandePrestation.idProduit 
    				  LEFT JOIN fournisseur WITH(NOLOCK) ON  fournisseur.id =produit.idFournisseur
    				  LEFT JOIN produitPaysDestination pPD WITH(NOLOCK) ON pPD.idProduit=commandePrestation.idProduit
    				  LEFT JOIN masterParametreValeur ValTraitement WITH(NOLOCK) ON cmd.statuTraitement=ValTraitement.id
    				  LEFT JOIN masterParametreValeur civ WITH(NOLOCK) ON p.civilite = civ.id
    	WHERE    
    		(
    			(@prest_typePrestation = 0)  
    			OR 
    			(commandeprestation.typeProduit = @prest_typePrestation  AND @prest_typePrestation <> 0)
    		)
    	AND (COALESCE(@prest_supplement, 0) = 0 OR (@prest_supplement <> 0 AND commandePrestation.idSupplement = @prest_supplement))
    	AND (@comm_client = 0    OR (client.id = @comm_client AND @comm_client > 0) OR (@comm_client = -1 AND client.typeClient=12))
    	AND (@prest_statutresa = 0   OR (commandeprestation.statutConfirmation = @prest_statutresa AND @prest_statutresa <> 0))      
    	AND (@prest_fournisseur = 0   OR (fournisseur.id = @prest_fournisseur       AND @prest_fournisseur <> 0))  
    	AND (COALESCE(@idGroupProduit,0) = 0 OR (@idGroupProduit <> 0 AND produit.groupe = @idGroupProduit))
    	AND (@prest_dateDebutPrest IS NULL 
    		OR (@prest_dateDebutPrest IS NOT NULL AND @prest_dateDebutPrest <= commandeprestation.[date])
    		)
    	AND (@prest_dateFinPrest IS NULL 
    		OR (@prest_dateFinPrest IS NOT NULL AND @prest_dateFinPrest >= commandeprestation.[date])
    		)
    	AND (COALESCE(@idCategoriePrestation, 0) = 0 OR commandeprestation.categorie = @idCategoriePrestation)
    	AND (
    		COALESCE(@listeProduits, '') = ''
    		OR
    		(
    			CommandePrestation.idProduit IN (SELECT number FROM dbo.varcharToList(@listeProduits))
    		)
    	)
    ORDER BY dateDepartInt 
     
    INSERT INTO @psgListCategorie
    SELECT commandePrestation.categorie, commandePrestation.typeProduit, NULL, tableTmp.id, NULL,NULL
    FROM @psgListTmp tableTmp
    INNER JOIN commandePassager WITH(NOLOCK) ON commandePassager.id = tableTmp.id AND commandePassager.idCommande = tableTmp.idCommande
    INNER JOIN commandePresationConsommee WITH(NOLOCK) ON commandePresationConsommee.idPax = commandePassager.id
    INNER JOIN commandePrestation WITH(NOLOCK) ON commandePrestation.id = commandePresationConsommee.idPrestation
    WHERE 
    COALESCE(commandePrestation.categorie,0) <> 0 
    AND commandePrestation.typeProduit IN (29,30)
     
    UPDATE tmpCategorie
    SET tmpCategorie.nomCategorie = typeLogement.nom,
        tmpCategorie.typeLogement = typeLogement.nom
    FROM @psgListCategorie tmpCategorie
    INNER JOIN typeLogement ON typeLogement.id = tmpCategorie.idCategorie
    WHERE tmpCategorie.typeProduit = 29 
     
    UPDATE tmpCategorie
    SET tmpCategorie.nomCategorie = categorie.nom,
        tmpCategorie.activite = categorie.nom
    FROM @psgListCategorie tmpCategorie
    INNER JOIN categorie ON categorie.id = tmpCategorie.idCategorie
    WHERE tmpCategorie.typeProduit = 30 
     
    UPDATE tableTmp
    SET tableTmp.categorie = (SELECT  STUFF((SELECT DISTINCT ' , ' + nomCategorie   
    							FROM @psgListCategorie   
    							WHERE	idPassager  = tab.idPassager
    							FOR XML PATH ('')),1,2,'') AS categorie  
    							FROM @psgListCategorie tab
    							WHERE tab.idPassager = tableTmp.id
    							GROUP BY idPassager
    							)  
    FROM @psgListTmp tableTmp
     
    UPDATE tableTmp
    SET tableTmp.typeLogement = (SELECT  STUFF((SELECT DISTINCT ' , ' + typeLogement   
    							FROM @psgListCategorie   
    							WHERE	idPassager  = tab.idPassager
    							FOR XML PATH ('')),1,2,'') AS categorie  
    							FROM @psgListCategorie tab
    							WHERE tab.idPassager = tableTmp.id
    							GROUP BY idPassager
    							)  
    FROM @psgListTmp tableTmp
     
    UPDATE tableTmp
    SET tableTmp.activite = (SELECT  STUFF((SELECT DISTINCT ' , ' + activite   
    							FROM @psgListCategorie   
    							WHERE	idPassager  = tab.idPassager
    							FOR XML PATH ('')),1,2,'') AS categorie  
    							FROM @psgListCategorie tab
    							WHERE tab.idPassager = tableTmp.id
    							GROUP BY idPassager
    							)  
    FROM @psgListTmp tableTmp
     
    UPDATE tableTmp
    SET tableTmp.prixParPersonne = tarif.prixParPersonne 
    FROM @psgListTmp tableTmp
    CROSS APPLY (
    	SELECT SUM(commandeTarifVente.prixUnitaire) AS prixParPersonne
    	FROM commandeTarifVente
    	WHERE commandeTarifVente.identifiantCommande = tableTmp.idCommande
    	AND 
    	(
    		(
    			commandeTarifVente.codeTarif <> 29	
    			AND
    			(
    				(tableTmp.typePax = 64 AND commandeTarifVente.codeTarif = 30)
    				OR
    				(tableTmp.typePax = 65 AND commandeTarifVente.codeTarif = 79)
    				OR
    				(tableTmp.typePax = 66 AND commandeTarifVente.codeTarif = 80)
    			)
    		)
    		OR
    		(
    			commandeTarifVente.codeTarif = 29 
    		)
    	)
    ) AS tarif
     
    -- Offre
    IF(COALESCE(@listeOffres, '') <> '')
    BEGIN
     
    	SET @countTableOffre = 1
     
    	DECLARE curseur_liste CURSOR FOR
    	SELECT (CASE WHEN number = 10 THEN 'E7' WHEN number = 15 THEN 'E6' WHEN number = 20 THEN 'E5' WHEN number = 0 THEN 'PT' END ) FROM dbo.varcharToList(@listeOffres)
     
    	OPEN curseur_liste FETCH curseur_liste INTO @tmpValeur
    	WHILE @@FETCH_STATUS = 0
    	BEGIN
     
    		IF (@tmpValeur = 'PT')
    		BEGIN
    			INSERT INTO @cmdTmpOffre
    			SELECT DISTINCT t.idCommande
    			FROM @psgListTmp t 
    			WHERE COALESCE(t.codeProduit, '') NOT LIKE '%E5%'
    			AND COALESCE(t.codeProduit, '') NOT LIKE '%E6%'
    			AND COALESCE(t.codeProduit, '') NOT LIKE '%E7%' 	
    		END
    		ELSE
    		BEGIN
    			INSERT INTO @cmdTmpOffre
    			SELECT DISTINCT t.idCommande
    			FROM @psgListTmp t
    			WHERE COALESCE(t.codeProduit, '') LIKE CONCAT('%', @tmpValeur,'%') 
    		END
     
    		FETCH curseur_liste INTO @tmpValeur
    	END
     
    	CLOSE curseur_liste
    	DEALLOCATE curseur_liste
     
    	/*
    	DELETE FROM t1 
    	FROM @psgListTmp t1
    	WHERE t1.idcommande NOT IN (SELECT idCommande FROM @cmdTmpOffre)
     
    	DELETE FROM @cmdTmpOffre
    	*/
     
    END
     
    -- Tarif Adulte
    IF(COALESCE(@listeTarifsAdultes, '') <> '')
    BEGIN
     
    	SET @countTableAdulte = 1
     
    	DECLARE @Current VARCHAR(30) 
    	SET @Current = NULL
     
    	WHILE (LEN(@listeTarifsAdultes) > 0)
    	BEGIN
    		IF (Charindex(',', @listeTarifsAdultes) > 0)
    		BEGIN
    			SET @Current = LEFT(@listeTarifsAdultes, Charindex(',', @listeTarifsAdultes) - 1)
    			SET @listeTarifsAdultes = RIGHT(@listeTarifsAdultes, LEN(@listeTarifsAdultes) - Charindex(',', @listeTarifsAdultes))
    		END
    		ELSE
    		BEGIN
    			SET @Current = @listeTarifsAdultes 
    			SET @listeTarifsAdultes = NULL
    		END
     
    		INSERT INTO @cmdTarifAdulte
    		SELECT DISTINCT t.idCommande
    		FROM @psgListTmp t INNER JOIN commandeTarifVente WITH(NOLOCK) ON t.idCommande = commandeTarifVente.identifiantCommande
    		WHERE commandeTarifVente.prixUnitaire = CONVERT(NUMERIC(18,3), @Current)
    		AND commandeTarifVente.codeTarif = 30
     
    		SET @Current = NULL
     
    	END
     
     
    END
     
    -- Tarif Enfant
    IF(COALESCE(@listeTarifsEnfants, '') <> '')
    BEGIN
     
    	SET @countTableEnfant = 1
     
    	SET @Current = NULL
     
    	WHILE (LEN(@listeTarifsEnfants) > 0)
    	BEGIN
    		IF (Charindex(',', @listeTarifsEnfants) > 0)
    		BEGIN
    			SET @Current = LEFT(@listeTarifsEnfants, Charindex(',', @listeTarifsEnfants) - 1)
    			SET @listeTarifsEnfants = RIGHT(@listeTarifsEnfants, LEN(@listeTarifsEnfants) - Charindex(',', @listeTarifsEnfants))
    		END
    		ELSE
    		BEGIN
    			SET @Current = @listeTarifsEnfants 
    			SET @listeTarifsEnfants = NULL
    		END
     
    		INSERT INTO @cmdTarifEnfant
    		SELECT DISTINCT t.idCommande
    		FROM @psgListTmp t INNER JOIN commandeTarifVente WITH(NOLOCK) ON t.idCommande = commandeTarifVente.identifiantCommande
    		WHERE commandeTarifVente.prixUnitaire = CONVERT(NUMERIC(18,3), @Current)
    		AND commandeTarifVente.codeTarif = 79
     
    		SET @Current = NULL
     
    	END
     
     
    END
     
     
    --SELECT @countTableOffre, @countTableAdulte, @countTableEnfant
     
    UPDATE t1
    SET t1.offreExiste = 1
    FROM @psgListTmp t1
    INNER JOIN @cmdTmpOffre t2 ON t1.idCommande = t2.idCommande
     
    UPDATE t1
    SET t1.tarifAdulteExiste = 1
    FROM @psgListTmp t1
    INNER JOIN @cmdTarifAdulte t2 ON t1.idCommande = t2.idCommande
     
    UPDATE t1
    SET t1.tarifEnfantExiste = 1
    FROM @psgListTmp t1
    INNER JOIN @cmdTarifEnfant t2 ON t1.idCommande = t2.idCommande
     
     
    SELECT	id
    		, civilite
    		, libCivilite
    		, commentaire
    		, datenaissance
    		, groupe
    		, nom
    		, prenom
    		, typePax
    		, numeroPiece
    		, telephone
    		, dateFinValidite
    		, idCommande
    		, nomProduit
    		, numeroCommande
    		, referenceExterne
    		, dateDepart
    		, dateDepartInt
    		, statutConfirmation
    		, dateCreation
    		, codeAnalytique
    		, raisonSociale
    		, apporteurAffaire 
    		, categorie
    		, statutTraitement
    		, typeLogement
    		, activite
    		, nomAgentClient
    		, codeProduit
    		, codeApporteurAffaire
    		, prixParPersonne
    	FROM @psgListTmp t1
    	WHERE 
     
    	(COALESCE(@countTableOffre, 0) = 0 AND COALESCE(@countTableAdulte, 0) = 0 AND COALESCE(@countTableEnfant, 0) = 0)
    	OR
    	(COALESCE(@countTableOffre, 0) = 0 AND COALESCE(@countTableAdulte, 0) = 0 AND COALESCE(@countTableEnfant, 0) = 1		AND COALESCE(t1.tarifEnfantExiste, 0) = 1 )
    	OR
    	(COALESCE(@countTableOffre, 0) = 0 AND COALESCE(@countTableAdulte, 0) = 1 AND COALESCE(@countTableEnfant, 0) = 0		AND COALESCE(t1.tarifAdulteExiste, 0) = 1)
    	OR
    	(COALESCE(@countTableOffre, 0) = 0 AND COALESCE(@countTableAdulte, 0) = 1 AND COALESCE(@countTableEnfant, 0) = 1		AND COALESCE(t1.tarifAdulteExiste, 0) = 1 AND COALESCE(t1.tarifEnfantExiste, 0) = 1 )
    	OR
    	(COALESCE(@countTableOffre, 0) = 1 AND COALESCE(@countTableAdulte, 0) = 0 AND COALESCE(@countTableEnfant, 0) = 0		AND COALESCE(t1.offreExiste, 0) = 1)
    	OR
    	(COALESCE(@countTableOffre, 0) = 1 AND COALESCE(@countTableAdulte, 0) = 0 AND COALESCE(@countTableEnfant, 0) = 1		AND COALESCE(t1.offreExiste, 0) = 1 AND COALESCE(t1.tarifEnfantExiste, 0) = 1)
    	OR
    	(COALESCE(@countTableOffre, 0) = 1 AND COALESCE(@countTableAdulte, 0) = 1 AND COALESCE(@countTableEnfant, 0) = 0		AND COALESCE(t1.offreExiste, 0) = 1 AND COALESCE(t1.tarifAdulteExiste, 0) = 1)
    	OR
    	(COALESCE(@countTableOffre, 0) = 1 AND COALESCE(@countTableAdulte, 0) = 1 AND COALESCE(@countTableEnfant, 0) = 1		AND COALESCE(t1.offreExiste, 0) = 1 AND COALESCE(t1.tarifAdulteExiste, 0) = 1 AND COALESCE(t1.tarifEnfantExiste, 0) = 1)
     
    	ORDER BY dateDepartInt	
     
    END

  2. #2
    Expert éminent sénior

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 757
    Points : 10 541
    Points
    10 541
    Billets dans le blog
    21
    Par défaut
    Bonjour,

    Vu la complexité de la procédure stockée, il nous faut :
    • le MCD
    • les scripts DDL de génération des tables
    • un jeu de données
    • Une explication (avec des mots) de ce qu'est censé faire la requête. Une courte, pour les grandes lignes, et une autre, plus longue, qui rentre dans les détails
    François DORIN
    Consultant informatique : conception, modélisation, développement (C#/.Net et SQL Server)
    Site internet | Profils Viadéo & LinkedIn
    ---------
    Page de cours : fdorin.developpez.com
    ---------
    N'oubliez pas de consulter la FAQ C# ainsi que les cours et tutoriels

  3. #3
    Membre habitué Avatar de olivtone
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Octobre 2010
    Messages
    242
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2010
    Messages : 242
    Points : 153
    Points
    153
    Par défaut
    Il faudrait aussi le plan d'execution de la PS

    Question :

    - Il y a beaucoup de OR dans votre précidat WHERE, peut être vous êtes victime du KITCHEN SINK

    il y a un tres bon post de Sarah Besnard sur ce sujet :

    http://www.concatskills.com/2017/05/15/kitchen-sink/

    - Avez vous tester de changer la table variable par une table temporaire,c'est beaucoup plus fexible ( Creation d'index etc...), et les statistiques sur les tables variables ne sont pas a jour, a moins d'avoir activé le traceflag 2453(disponible a partir de SQL2012 SP2).

    J'en parle dans ma vidéo en ligne :



    - Quelle est l'interet du NOLOCK ? (cela me donne des boutons )

    Bonne journée

  4. #4
    Rédacteur

    Avatar de SQLpro
    Homme Profil pro
    Expert bases de données / SQL / MS SQL Server / Postgresql
    Inscrit en
    Mai 2002
    Messages
    21 768
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Expert bases de données / SQL / MS SQL Server / Postgresql
    Secteur : Conseil

    Informations forums :
    Inscription : Mai 2002
    Messages : 21 768
    Points : 52 719
    Points
    52 719
    Billets dans le blog
    5
    Par défaut
    En dehors de ce que l'on vous à déjà dit, je rajouterais :
    • Absence de SET NOCOUNT ON;
    • pas de préfixage des objets avec les schémas SQL
    • utilisation de variable table
    • utilisation du NOLOCK
    • utilisation de fonction scalaire sans doute parfaitement inutile comme "dbo.inttodate"
    • LIKE avec '%toto%'
    • utilisation probables de fonction table multi instruction comme "dbo.varcharToList"
    • utilisation d'un CROSS APPLY probablement injustifié
    • utilisation d'un curseur ouvert dans le pire mode qui soit


    Bref, commencez par une formation, parce que là on va pas en sortir !

    A +
    Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
    Le site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
    Blog SQL, SQL Server, SGBDR : http://blog.developpez.com/sqlpro
    Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
    Entreprise SQL SPOT : modélisation, conseils, audit, optimisation, formation...
    * * * * * Expertise SQL Server : http://mssqlserver.fr/ * * * * *

Discussions similaires

  1. [Transact-SQL] Optimisation d'une procédure stockée
    Par Shinn77 dans le forum MS SQL Server
    Réponses: 7
    Dernier message: 25/06/2007, 12h30
  2. [SQL SVR 2K]Optimisation procédure stockée
    Par Franck2mars dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 02/06/2006, 13h41
  3. Réponses: 6
    Dernier message: 21/06/2005, 15h06
  4. Réponses: 5
    Dernier message: 09/05/2005, 12h24
  5. Réponses: 5
    Dernier message: 04/10/2004, 18h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo