From c4febeaa910c89b428790612484ee9a098ed67c2 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 28 Oct 2019 09:40:40 +1100 Subject: [PATCH] feat: optimise latest_pact_consumer_version_orders --- .../latest_pact_consumer_version_orders.rb | 13 +++++++++++++ db/migrations/20180722_recreate_views.rb | 5 +---- .../20191029_optimise_latest_pact_cv_orders.rb | 13 +++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 db/ddl_statements/latest_pact_consumer_version_orders.rb create mode 100644 db/migrations/20191029_optimise_latest_pact_cv_orders.rb diff --git a/db/ddl_statements/latest_pact_consumer_version_orders.rb b/db/ddl_statements/latest_pact_consumer_version_orders.rb new file mode 100644 index 000000000..c7b77aee1 --- /dev/null +++ b/db/ddl_statements/latest_pact_consumer_version_orders.rb @@ -0,0 +1,13 @@ +def latest_pact_consumer_version_orders_v1(connection = nil) + "select provider_id, consumer_id, max(consumer_version_order) as latest_consumer_version_order + from all_pact_publications + group by provider_id, consumer_id" +end + +def latest_pact_consumer_version_orders_v2(connection = nil) + view = Sequel.as(:latest_pact_publication_ids_for_consumer_versions, :lp) + connection.from(view) + .select_group(:consumer_id, :provider_id) + .select_append{ max(order).as(latest_consumer_version_order) } + .join(:versions, { Sequel[:lp][:consumer_version_id] => Sequel[:cv][:id]}, { table_alias: :cv} ) +end diff --git a/db/migrations/20180722_recreate_views.rb b/db/migrations/20180722_recreate_views.rb index fe46ca200..f93196fe6 100644 --- a/db/migrations/20180722_recreate_views.rb +++ b/db/migrations/20180722_recreate_views.rb @@ -35,9 +35,6 @@ ) create_or_replace_view(:latest_pact_consumer_version_orders, - "select provider_id, consumer_id, max(consumer_version_order) as latest_consumer_version_order - from all_pact_publications - group by provider_id, consumer_id" - ) + latest_pact_consumer_version_orders_v1(self)) end end diff --git a/db/migrations/20191029_optimise_latest_pact_cv_orders.rb b/db/migrations/20191029_optimise_latest_pact_cv_orders.rb new file mode 100644 index 000000000..96124805c --- /dev/null +++ b/db/migrations/20191029_optimise_latest_pact_cv_orders.rb @@ -0,0 +1,13 @@ +require_relative '../ddl_statements' + +Sequel.migration do + up do + create_or_replace_view(:latest_pact_consumer_version_orders, + latest_pact_consumer_version_orders_v2(self)) + end + + down do + create_or_replace_view(:latest_pact_consumer_version_orders, + latest_pact_consumer_version_orders_v1(self)) + end +end