Skip to content

Commit

Permalink
Merge pull request #273 from 18F/staging
Browse files Browse the repository at this point in the history
[Tech Debt] Refactor domain queries for performance -- Production
  • Loading branch information
levinmr authored Aug 21, 2024
2 parents 6b0b498 + b85d8fc commit 8166c1d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions migrations/20240819182419_add_jsonb_data_index_ua.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex

Check warning on line 2 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description

Check warning on line 2 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description

Check warning on line 2 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description

Check warning on line 2 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description
* @returns { Promise<void> }
*/
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

Check warning on line 12 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description

Check warning on line 12 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description

Check warning on line 12 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description

Check warning on line 12 in migrations/20240819182419_add_jsonb_data_index_ua.js

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "knex" description
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.table("analytics_data", (table) => {
table.dropIndex("analytics_data_gin_jsonb");
});
};
4 changes: 3 additions & 1 deletion src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 8166c1d

Please sign in to comment.