Skip to content

Commit 011d7c9

Browse files
committed
feat(matrix): speed up query to refresh index
1 parent 61b8dfb commit 011d7c9

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

DEVELOPER_DOCUMENTATION.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,31 @@ Domain classes are found in `lib/pact_broker/domain`. Many of these classes are
5858

5959
* `matrix` - The matrix of every pact publication and verification. Includes every pact revision (eg. publishing to the same consumer version twice, or using PATCH) and every verification (including 'overwritten' ones. eg. when the same provider build runs twice.)
6060

61-
* `latest_matrix` - This view is a subset of, and has the same columns as, the `matrix`. It removes 'overwritten' pacts and verifications from the matrix (ie. only show latest pact revision for each consumer version and latest verification for each provider version)
61+
* `latest_matrix_for_consumer_version_and_provider_version` - This view is a subset of, and has the same columns as, the `matrix`. It removes 'overwritten' pacts and verifications from the matrix (ie. only show latest pact revision for each consumer version and latest verification for each provider version)
62+
63+
### Materialized Views
64+
65+
We can't use proper materialized views because we have to support MySQL :|
66+
67+
So as a hacky solution, there are two tables which act as materialized views to speed up the performance of the matrix and index queries. These tables are updated after any resource is published with a `consumer_name`, `provider_name` or `pacticipant_name` in the URL (see lib/pact_broker/api/resources/base_resource.rb#finish_request).
68+
69+
* `materialized_matrix` table - is populated from the `matrix` view.
70+
71+
* `materialized_head_matrix` table - is populated from `head_matrix` view, and is based on `materialized_matrix`.
72+
73+
### Dependencies
74+
75+
materialized_head_matrix table (is populated from...)
76+
= head_matrix view
77+
-> latest_matrix_for_consumer_version_and_provider_version view
78+
-> materialized_matrix table (is populated from...)
79+
= matrix view
80+
-> verifications table
81+
-> versions table
82+
-> all_pact_publications view
83+
-> pact_versions table
84+
-> pact_publications table
85+
-> pacticipants table
86+
-> versions table
87+
-> latest_verification_id_for_consumer_version_and_provider_version view
88+
-> latest_pact_publication_revision_numbers view

db/migrations/20180210_fix_latest_matrix_for_cv_and_pv_again.rb

+15-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@
88
# lose the unverified pacts.
99
# This view used to be (stupidly) called latest_matrix
1010

11-
# Fixes mistakenly copied definition in 20180209_recreate_latest_matrix_for_cv_and_pv_union_all.rb
12-
# which missed the join to latest_pact_publication_revision_numbers
11+
# Fix mistakenly copied definition in 20180209_recreate_latest_matrix_for_cv_and_pv_union_all.rb
12+
# which missed the join to latest_pact_publication_revision_numbers.
13+
14+
# Change this view to be based on materialized_matrix instead of matrix
15+
# to speed it up.
16+
# Note! This does mean there is a dependency on having updated
17+
# materialized_matrix FIRST that may cause problems. Will see how it goes.
18+
19+
alter_table(:materialized_matrix) do
20+
add_index [:verification_id], name: 'ndx_mm_verif_id'
21+
add_index [:pact_revision_number], name: 'ndx_mm_pact_rev_num'
22+
end
23+
1324
create_or_replace_view(:latest_matrix_for_consumer_version_and_provider_version,
14-
"SELECT matrix.* FROM matrix
25+
"SELECT matrix.* FROM materialized_matrix matrix
1526
inner join latest_pact_publication_revision_numbers lr
1627
on matrix.consumer_id = lr.consumer_id
1728
and matrix.provider_id = lr.provider_id
@@ -24,7 +35,7 @@
2435
2536
UNION ALL
2637
27-
select matrix.* from matrix
38+
select matrix.* from materialized_matrix matrix
2839
inner join latest_pact_publication_revision_numbers lr
2940
on matrix.consumer_id = lr.consumer_id
3041
and matrix.provider_id = lr.provider_id

0 commit comments

Comments
 (0)