From f244b58916334a38151181918cace632b0becabe Mon Sep 17 00:00:00 2001 From: Tomasz Szuba Date: Mon, 13 Feb 2023 18:19:37 +0100 Subject: [PATCH 1/2] Add new index to techinsights This improved query for latest facts from several seconds to milliseconds on our instance Signed-off-by: Tomasz Szuba --- .changeset/grumpy-bikes-begin.md | 5 +++ plugins/tech-insights-backend/knexfile.js | 26 ++++++++++++++ .../20230213170839_latest-facts-index.js | 36 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 .changeset/grumpy-bikes-begin.md create mode 100644 plugins/tech-insights-backend/knexfile.js create mode 100644 plugins/tech-insights-backend/migrations/20230213170839_latest-facts-index.js diff --git a/.changeset/grumpy-bikes-begin.md b/.changeset/grumpy-bikes-begin.md new file mode 100644 index 0000000000..a98e6651f5 --- /dev/null +++ b/.changeset/grumpy-bikes-begin.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-tech-insights-backend': patch +--- + +Add DB index to improve latency of latest fact query diff --git a/plugins/tech-insights-backend/knexfile.js b/plugins/tech-insights-backend/knexfile.js new file mode 100644 index 0000000000..b771c202f5 --- /dev/null +++ b/plugins/tech-insights-backend/knexfile.js @@ -0,0 +1,26 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// This file makes it possible to run "yarn knex migrate:make some_file_name" +// to assist in making new migrations +module.exports = { + client: 'better-sqlite3', + connection: ':memory:', + useNullAsDefault: true, + migrations: { + directory: './migrations', + }, +}; diff --git a/plugins/tech-insights-backend/migrations/20230213170839_latest-facts-index.js b/plugins/tech-insights-backend/migrations/20230213170839_latest-facts-index.js new file mode 100644 index 0000000000..94a589f59d --- /dev/null +++ b/plugins/tech-insights-backend/migrations/20230213170839_latest-facts-index.js @@ -0,0 +1,36 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const indexName = 'fact_id_entity_timestamp_idx'; +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = async knex => { + await knex.schema.alterTable('facts', table => { + table.index(['id', 'entity', 'timestamp'], indexName); + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async knex => { + await knex.schema.alterTable('facts', table => { + table.dropIndex(indexName); + }); +}; From 207d8df70300f794310562208a9d0f667c0ec0aa Mon Sep 17 00:00:00 2001 From: Tomasz Szuba Date: Tue, 14 Feb 2023 10:51:33 +0100 Subject: [PATCH 2/2] Remove knexfile.js from tech-insights-backend Signed-off-by: Tomasz Szuba --- plugins/tech-insights-backend/knexfile.js | 26 ----------------------- 1 file changed, 26 deletions(-) delete mode 100644 plugins/tech-insights-backend/knexfile.js diff --git a/plugins/tech-insights-backend/knexfile.js b/plugins/tech-insights-backend/knexfile.js deleted file mode 100644 index b771c202f5..0000000000 --- a/plugins/tech-insights-backend/knexfile.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2023 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// This file makes it possible to run "yarn knex migrate:make some_file_name" -// to assist in making new migrations -module.exports = { - client: 'better-sqlite3', - connection: ':memory:', - useNullAsDefault: true, - migrations: { - directory: './migrations', - }, -};