From 4ff16744b99d06a1820f8e99f778f6cfa7818419 Mon Sep 17 00:00:00 2001 From: Svetlin Zarev Date: Thu, 28 Nov 2024 11:50:15 +0200 Subject: [PATCH] Replace the unused GIN index with a BTREE index when searching by price, because the `->` operator is not accelerated by the GIN index --- lessons/227/README.md | 2 +- lessons/227/client/store.go | 2 +- lessons/227/migration/0-sql.yaml | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lessons/227/README.md b/lessons/227/README.md index cbbf9c643..f418b422f 100644 --- a/lessons/227/README.md +++ b/lessons/227/README.md @@ -10,7 +10,7 @@ CREATE TABLE product ( jdoc jsonb ); -CREATE INDEX product_inx ON product USING GIN (jdoc); +CREATE INDEX idx__product__price ON product using BTREE(((jdoc -> 'price')::NUMERIC)); INSERT INTO product(jdoc) VALUES ('{"name": "Shampoo", "price": 7.90, "stock": 100}'); INSERT INTO product(jdoc) VALUES ('{"name": "Hairspray", "price": 11.50, "stock": 100}'); diff --git a/lessons/227/client/store.go b/lessons/227/client/store.go index 0dbdf0f25..d0d5a9cd0 100644 --- a/lessons/227/client/store.go +++ b/lessons/227/client/store.go @@ -34,7 +34,7 @@ func (p *product) create(pg *postgres, mg *mongodb, db string, m *metrics) (err if db == "pg" { b, err := json.Marshal(p) - fail(err, "json.Marshal(p) filaed") + fail(err, "json.Marshal(p) failed") err = pg.dbpool.QueryRow(pg.context, `INSERT INTO product(jdoc) VALUES ($1) RETURNING id`, b).Scan(&p.PostgresId) diff --git a/lessons/227/migration/0-sql.yaml b/lessons/227/migration/0-sql.yaml index adb721220..692dbf29b 100644 --- a/lessons/227/migration/0-sql.yaml +++ b/lessons/227/migration/0-sql.yaml @@ -24,13 +24,13 @@ data: COMMIT; - VACUUM full; - -- Create Tables CREATE TABLE product ( id SERIAL PRIMARY KEY, jdoc jsonb ); - - CREATE INDEX product_inx ON product USING GIN (jdoc); + + CREATE INDEX idx__product__price ON product using BTREE(((jdoc -> 'price')::NUMERIC)); + + VACUUM full;