From 5e9b80d3481d3b893da43e4493b7767dcb0ffc39 Mon Sep 17 00:00:00 2001 From: Michael Levin Date: Mon, 19 Aug 2024 13:22:28 -0400 Subject: [PATCH 1/2] [Tech Debt] Refactor domain queries for performance --- src/db.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/db.js b/src/db.js index b336c1d..fc8b1df 100644 --- a/src/db.js +++ b/src/db.js @@ -62,7 +62,9 @@ const queryDomain = ( if (reportName == "download") { mainQuery.whereRaw("data->> 'page' like ?", [`%${domain}%`]); } else { - mainQuery.whereRaw("data->> 'domain' = ?", [domain]); + mainQuery.whereRaw( + `data @> '${JSON.stringify({ domain }).replaceAll("'", "''").replaceAll("?", "\\?")}'::jsonb`, + ); } return ( From b2082358256764b1c6c99e7f226bed8f2edd59d6 Mon Sep 17 00:00:00 2001 From: Michael Levin Date: Mon, 19 Aug 2024 14:25:29 -0400 Subject: [PATCH 2/2] [Tech Debt] Add jsonb index on the v1.1 (UA) table --- .../20240819182419_add_jsonb_data_index_ua.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 migrations/20240819182419_add_jsonb_data_index_ua.js diff --git a/migrations/20240819182419_add_jsonb_data_index_ua.js b/migrations/20240819182419_add_jsonb_data_index_ua.js new file mode 100644 index 0000000..2c07039 --- /dev/null +++ b/migrations/20240819182419_add_jsonb_data_index_ua.js @@ -0,0 +1,19 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function (knex) { + return knex.schema.raw( + "CREATE INDEX analytics_data_ua_gin_jsonb ON analytics_data USING gin(data jsonb_path_ops)", + ); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function (knex) { + return knex.schema.table("analytics_data", (table) => { + table.dropIndex("analytics_data_gin_jsonb"); + }); +};