-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a utility database function to search entities by ID. Because we store entity properties as JSON, we add a GIN index. Testing with 50.000 entities we get the following performance: ``` Nested Loop (cost=30.39..109.45 rows=5 width=87) (actual time=0.290..0.292 rows=1 loops=1) -> Bitmap Heap Scan on properties p (cost=30.10..67.90 rows=5 width=16) (actual time=0.216..0.218 rows=1 loops=1) " Recheck Cond: (value @> '{""value"": ""MBymWMNbcv"", ""version"": ""v1""}'::jsonb)" Filter: (key = 'upstream_id'::text) Heap Blocks: exact=1 -> Bitmap Index Scan on idx_properties_value_gin (cost=0.00..30.10 rows=10 width=0) (actual time=0.174..0.175 rows=1 loops=1) " Index Cond: (value @> '{""value"": ""MBymWMNbcv"", ""version"": ""v1""}'::jsonb)" -> Index Scan using entity_instances_pkey on entity_instances ei (cost=0.29..8.31 rows=1 width=87) (actual time=0.069..0.069 rows=1 loops=1) Index Cond: (id = p.entity_id) Filter: (entity_type = 'repository'::entities) Planning Time: 2.365 ms Execution Time: 0.590 ms ``` Compared to not using the index: ``` Nested Loop (cost=0.29..3165.63 rows=5 width=87) (actual time=0.117..42.194 rows=1 loops=1) -> Seq Scan on properties p (cost=0.00..3124.07 rows=5 width=16) (actual time=0.032..42.108 rows=1 loops=1) " Filter: ((value @> '{""value"": ""MBymWMNbcv"", ""version"": ""v1""}'::jsonb) AND (key = 'upstream_id'::text))" Rows Removed by Filter: 100004 -> Index Scan using entity_instances_pkey on entity_instances ei (cost=0.29..8.31 rows=1 width=87) (actual time=0.082..0.082 rows=1 loops=1) Index Cond: (id = p.entity_id) Filter: (entity_type = 'repository'::entities) Planning Time: 1.104 ms Execution Time: 42.300 ms ``` Related: #4179
- Loading branch information
Showing
10 changed files
with
262 additions
and
1 deletion.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
database/migrations/000102_properties_upstream_id_index.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- Copyright 2023 Stacklok, Inc | ||
-- | ||
-- 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. | ||
|
||
BEGIN; | ||
|
||
DROP INDEX IF EXISTS idx_properties_value_gin; | ||
|
||
COMMIT; |
20 changes: 20 additions & 0 deletions
20
database/migrations/000102_properties_upstream_id_index.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- Copyright 2023 Stacklok, Inc | ||
-- | ||
-- 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. | ||
|
||
BEGIN; | ||
|
||
-- Create index on properties for upstream_id | ||
CREATE INDEX idx_properties_value_gin ON properties USING GIN (value jsonb_path_ops); | ||
|
||
COMMIT; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters