Skip to content

Commit

Permalink
Use existing number sequences instead of looking them up again
Browse files Browse the repository at this point in the history
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 fabric8-services#2291
  • Loading branch information
kwk committed Sep 26, 2018
1 parent 07f931d commit a94e48a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 5 additions & 6 deletions migration/sql-files/106-number-sequences.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions migration/sql-test-files/106-number-sequences.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit a94e48a

Please sign in to comment.