From a94e48aafacd0b9340e4a186e164483eb3107b19 Mon Sep 17 00:00:00 2001 From: Konrad Kleine Date: Wed, 26 Sep 2018 07:51:23 +0200 Subject: [PATCH] Use existing number sequences instead of looking them up again In order to avoid a sequential table scan on the work_items DB table we take the values for the new number_sequences table from the old work_item_number_sequences. See https://github.com/fabric8-services/fabric8-wit/issues/2291 --- migration/sql-files/106-number-sequences.sql | 11 +++++------ migration/sql-test-files/106-number-sequences.sql | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/migration/sql-files/106-number-sequences.sql b/migration/sql-files/106-number-sequences.sql index 00103fe399..5290c57692 100644 --- a/migration/sql-files/106-number-sequences.sql +++ b/migration/sql-files/106-number-sequences.sql @@ -6,9 +6,6 @@ CREATE TABLE number_sequences ( PRIMARY KEY (space_id, table_name) ); --- Delete old number table -DROP TABLE work_item_number_sequences; - -- Update existing iterations and areas with new "number" column and fill in the -- numbers in the order iterations and areas have been created. ALTER TABLE iterations ADD COLUMN number INTEGER; @@ -32,11 +29,13 @@ ALTER TABLE areas ADD CONSTRAINT areas_space_id_number_idx UNIQUE (space_id, num -- Update the number_sequences table with the maximum for each table type INSERT INTO number_sequences (space_id, table_name, current_val) - SELECT space_id, 'work_items' "table_name", MAX(number) - FROM work_items - WHERE number IS NOT NULL + SELECT space_id, 'work_items' "table_name", current_val + FROM work_item_number_sequences GROUP BY 1,2; +-- Delete old number table +DROP TABLE work_item_number_sequences; + INSERT INTO number_sequences (space_id, table_name, current_val) SELECT space_id, 'iterations' "table_name", MAX(number) FROM iterations diff --git a/migration/sql-test-files/106-number-sequences.sql b/migration/sql-test-files/106-number-sequences.sql index febc78d30b..49b52c9e32 100644 --- a/migration/sql-test-files/106-number-sequences.sql +++ b/migration/sql-test-files/106-number-sequences.sql @@ -39,6 +39,11 @@ VALUES (current_setting('wi3.id')::uuid, current_setting('wit.id')::uuid, current_setting('sp2.id')::uuid, '{"system.title":"WI3"}'::json, 1, '2018-09-17 12:01'), (current_setting('wi4.id')::uuid, current_setting('wit.id')::uuid, current_setting('sp2.id')::uuid, '{"system.title":"WI4"}'::json, 2, '2018-09-17 17:01'); +INSERT INTO work_item_number_sequences (space_id, current_val) +VALUES + (current_setting('sp1.id')::uuid, 2), + (current_setting('sp2.id')::uuid, 2); + INSERT INTO iterations (id, name, path, space_id, created_at) VALUES (current_setting('iter1.id')::uuid, 'iteration 1', replace(current_setting('iter1.id'), '-', '_')::ltree, current_setting('sp1.id')::uuid, '2018-09-17 16:01'),