DEV Community

Cover image for Optimisation SQL Server : Vues, Procédures Stockées et Index
mohamed ait sidi hou
mohamed ait sidi hou

Posted on • Edited on

1 1 1 1 1

Optimisation SQL Server : Vues, Procédures Stockées et Index

En entreprise, une requête lente peut coûter cher !!🔴

🔑 Les 3 Clés de l'Optimisation

1. Les Vues (Views)

À quoi ça sert ?

Simplifier les requêtes complexes et sécuriser l'accès aux données.

CREATE VIEW VentesFrance AS
SELECT 
    c.NomClient,
    SUM(m.Montant) AS CA_Total,
    COUNT(DISTINCT m.CommandeID) AS NbCommandes
FROM Clients c
JOIN Commandes m ON c.ClientID = m.ClientID
WHERE c.Pays = 'France'
GROUP BY c.NomClient;
Enter fullscreen mode Exit fullscreen mode

Avantages :

  • Réutilisation du code: Temps dev÷2\text{Temps dev} \div 2
  • Sécurisation des données sensibles
  • Abstraction de la logique métier

2. Les Procédures Stockées

Performance boost : Exécution plan en cache !

CREATE PROCEDURE GetClientActivity 
    @ClientID INT,
    @Annee INT
AS
BEGIN
    SELECT 
        MONTH(DateCommande) AS Mois,
        SUM(Montant) AS TotalAchats
    FROM Commandes
    WHERE ClientID = @ClientID 
      AND YEAR(DateCommande) = @Annee
    GROUP BY MONTH(DateCommande)
END;
Enter fullscreen mode Exit fullscreen mode

Gain de performance :

Ttotal=Treque^teN+ϵvsTproceˊdure+δ T_{total} = \frac{T_{requête}}{N} + \epsilon \quad \text{vs} \quad T_{procédure} + \delta

Où :

  • NN : Nombre d'exécutions.
  • ϵ\epsilon : Temps de compilation à chaque exécution.
  • δ\delta : Temps d'accès au cache (~0.2s).

Bref, la formule devient :

Temps exeˊcution=Temps requeˆte brute3+0.2s (cache) \text{Temps exécution} = \frac{\text{Temps requête brute}}{3} + 0.2s\ (\text{cache})

3. Les Indexes

Mathématiquement, un bon index réduit la complexité :

O(n)O(logn) O(n) \to O(\log n)

Complexcité

Types d'index essentiels :

-- Index cluster (tri physique)
CREATE CLUSTERED INDEX IDX_Commandes_Date
ON Commandes (DateCommande);

-- Index non-cluster
CREATE NONCLUSTERED INDEX IDX_Clients_Pays
ON Clients (Pays) 
INCLUDE (NomClient, Ville);
Enter fullscreen mode Exit fullscreen mode

Équation d'efficacité :

Gain=Full Table ScanIndex Seek×100% \text{Gain} = \frac{\text{Full Table Scan}}{\text{Index Seek}} \times 100\%

Algo Index

🧪 Cas Réel d'Optimisation


    A[Requête lente: 15s] --> B{Analyse Execution Plan}
    B --> C[Création index couvrant]
    C --> D[Factorisation en procédure stockée]
    D --> E[Resultat: 1.2s]
Enter fullscreen mode Exit fullscreen mode
  • Toujours tester avec SET STATISTICS TIME ON
  • Utiliser WITH SCHEMABINDING pour les vues critiques

📈 Résultats Obtenus

  • Réduction du temps moyen des requêtes : 82%82\%
  • Diminution des IO disque : 45%45\%
  • Simplification du code métier

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay