Add new index to techinsights

This improved query for latest facts from several seconds to milliseconds on our instance

Signed-off-by: Tomasz Szuba <tszuba@box.com>
This commit is contained in:
Tomasz Szuba
2023-02-13 18:19:37 +01:00
parent fe7b17351b
commit f244b58916
3 changed files with 67 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-tech-insights-backend': patch
---
Add DB index to improve latency of latest fact query
+26
View File
@@ -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',
},
};
@@ -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<void> }
*/
exports.up = async knex => {
await knex.schema.alterTable('facts', table => {
table.index(['id', 'entity', 'timestamp'], indexName);
});
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async knex => {
await knex.schema.alterTable('facts', table => {
table.dropIndex(indexName);
});
};