Best Practice Juin 2008 / June 2008
Ce mois ci, je vous donne deux brefs exemples pour vous inspirer lors de la création de vos objets/requêtes. This month, here are two simple examples to inspire you during the creation of your objects/queries.
Le premier exemple concerne l’utilisation de UNION au lieu de CASE (se retrouve très souvent). The first example is how you can use a UNION select instead of CASE (which I see over-used in many environments).
transformez vos CASE... /tranform the CASE...
SELECT CHB_NUMERO, 
CASE CHB_ETAGE
WHEN 'RDC' THEN 0 
WHEN '1er' THEN 1 
WHEN '2e' THEN 2 
END AS ETAGE, CHB_COUCHAGE 
FROM T_CHAMBRE 
ORDER BY ETAGE, CHB_COUCHAGE 
...en UNION / ...into UNION
SELECT CHB_NUMERO, 0 AS ETAGE, CHB_COUCHAGE 
FROM T_CHAMBRE 
WHERE CHB_ETAGE = 'RDC'
UNION SELECT CHB_NUMERO, 1 AS ETAGE, CHB_COUCHAGE 
FROM T_CHAMBRE 
WHERE CHB_ETAGE = '1er'
UNION SELECT CHB_NUMERO, 2 AS ETAGE, CHB_COUCHAGE 
FROM T_CHAMBRE 
WHERE CHB_ETAGE = '2e'
ORDER BY ETAGE, CHB_COUCHAGE
La performance sera nettement améliorée si la colonne CHB_ETAGE a un index. 
Of course, you want to make sure there is an index on CHB_ETAGE field to obtain the best performance (unless your table is insignificant in size).
Le deuxième exemple concerne le remplissage d’une table temporaire : 
The second example involves the desired use of temporary tables (avoiding into #temp as much as possible) :
Create table #tempZ
 (
  col1 DataType
  ,col2 DataType
  ,col3 DataType
 )
INSERT #tempZ
 (
  col1
  ,col2 -- éviter les NULLs avec ISNULL(col,0)
  ,col3
 ) 
EXECUTE NomBD.dbo.cp_MonPrpc parm1, param2 
Afin d’éviter des procédures stockées qui dépassent 500 lignes. On met le Select en procédure stockée. 
To avoid very long stored procedures I recommend placing the select statement that matches the insert by using execution of a stored procedure to populate temporary table. 
No comments:
Post a Comment