Skip to content

Commit

Permalink
fix(matrix): fix performance issue when querying matrix with one sele…
Browse files Browse the repository at this point in the history
…ctor

Change left outer join that had an OR that spanned both sides of the join into a union of two queries with only one condition.
  • Loading branch information
bethesque committed Apr 8, 2022
1 parent 729e767 commit 3a08d12
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
20 changes: 19 additions & 1 deletion lib/pact_broker/matrix/every_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,43 @@ class EveryRow < PactBroker::Matrix::QuickRow
Sequel[:p][:created_at].as(:consumer_version_created_at),
Sequel[:p][:id].as(:pact_order)
]

VERIFICATION_COLUMNS = [
Sequel[:v][:id].as(:verification_id),
Sequel[:v][:provider_version_id],
Sequel[:v][:created_at].as(:provider_version_created_at)
]

JOINED_VERIFICATION_COLUMNS = [:id, :pact_version_id, :provider_id, :provider_version_id, :created_at]

ALL_COLUMNS = PACT_COLUMNS + VERIFICATION_COLUMNS

SELECT_ALL_COLUMN_ARGS = [:select_all_columns] + ALL_COLUMNS

dataset_module do
select(*SELECT_ALL_COLUMN_ARGS)

def join_verifications
left_outer_join(:verifications, P_V_JOIN, { table_alias: :v } )
end

def inner_join_verifications
join(:verifications, P_V_JOIN, { table_alias: :v } )
end

def inner_join_verifications_matching_one_selector_provider_or_provider_version(query_ids)
verifications = db[:verifications]
.select(*JOINED_VERIFICATION_COLUMNS)
.where {
QueryBuilder.provider_or_provider_version_matches(query_ids)
}

join(verifications, P_V_JOIN, { table_alias: :v } )
end

def verifications_for(query_ids)
db[:verifications]
.select(:id, :pact_version_id, :provider_id, :provider_version_id, :created_at)
.select(*JOINED_VERIFICATION_COLUMNS)
.where {
Sequel.&(
QueryBuilder.consumer_in_pacticipant_ids(query_ids),
Expand Down
30 changes: 27 additions & 3 deletions lib/pact_broker/matrix/quick_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class QuickRow < Sequel::Model(Sequel.as(:latest_pact_publication_ids_for_consum
Sequel[:v][:verification_id],
Sequel[:v][:created_at].as(:provider_version_created_at)
]

JOINED_VERIFICATION_COLUMNS = [
:verification_id,
:provider_version_id,
:pact_version_id,
:provider_id,
:created_at
]

LAST_ACTION_DATE = Sequel.lit("CASE WHEN (provider_version_created_at IS NOT NULL AND provider_version_created_at > consumer_version_created_at) THEN provider_version_created_at ELSE consumer_version_created_at END").as(:last_action_date)

ALL_COLUMNS = PACT_COLUMNS + VERIFICATION_COLUMNS
Expand Down Expand Up @@ -146,10 +155,15 @@ def default_scope
# When we have one selector, we need to join ALL the verifications to find out
# what integrations exist
def matching_one_selector(selectors)
join_verifications
query_ids = QueryIds.from_selectors(selectors)
rows_where_selector_matches_consumer_cols = join_verifications
.where {
QueryBuilder.consumer_or_consumer_version_or_provider_or_provider_or_provider_version_match(QueryIds.from_selectors(selectors), :p, :v)
QueryBuilder.consumer_or_consumer_version_matches(query_ids, :p)
}

rows_where_selector_matches_provider_cols = inner_join_verifications_matching_one_selector_provider_or_provider_version(query_ids)

rows_where_selector_matches_consumer_cols.union(rows_where_selector_matches_provider_cols)
end

def pacticipant_ids_matching_one_selector_optimised(selectors)
Expand Down Expand Up @@ -229,9 +243,19 @@ def join_verifications_for(query_ids)
left_outer_join(verifications_for(query_ids), LP_LV_JOIN, { table_alias: :v } )
end

def inner_join_verifications_matching_one_selector_provider_or_provider_version(query_ids)
verifications = db[LV]
.select(*JOINED_VERIFICATION_COLUMNS)
.where {
QueryBuilder.provider_or_provider_version_matches(query_ids)
}

join(verifications, LP_LV_JOIN, { table_alias: :v } )
end

def verifications_for(query_ids)
db[LV]
.select(:verification_id, :provider_version_id, :pact_version_id, :provider_id, :created_at)
.select(*JOINED_VERIFICATION_COLUMNS)
.where {
Sequel.&(
QueryBuilder.consumer_in_pacticipant_ids(query_ids),
Expand Down

0 comments on commit 3a08d12

Please sign in to comment.