Le reste de la formation est réservé aux inscrits.
S'inscrire gratuitementCREATE TABLE produits (id SERIAL PRIMARY KEY, nom TEXT, attributs JSONB);
INSERT INTO produits (nom, attributs) VALUES ('T-shirt', '{"couleur":"rouge","tailles":["S","M","L"],"prix":19.99,"en_stock":true}');
-- Extraire une valeur
SELECT nom, attributs->>'couleur' AS couleur FROM produits;
-- Filtrer sur JSONB
SELECT * FROM produits WHERE attributs @> '{"couleur":"rouge"}';
-- Index GIN pour JSONB
CREATE INDEX idx_produits_attributs ON produits USING GIN (attributs);| JSON | JSONB | |
|---|---|---|
| Stockage | Texte brut (préserve l'ordre, espaces) | Binaire décomposé (plus rapide) |
| Indexable | Non | Oui (GIN) |
| Performance | Plus lent à l'extraction | Plus rapide, mais INSERT plus lent |
| Usage | Logging, stockage brut | Données à interroger (recommandé) |