From dc5302ca0896394c4020b46f82102d6465f4ca54 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 25 Oct 2018 11:33:14 +1100 Subject: [PATCH] Speed up database queries and make them scale This commit makes use of three ActiveRecord features: 1. Using `select` instead of `all.map` enables ActiveRecord to nest one select into the other, resulting in one more efficient query instead of two. 2. Using `find_each` saves memory by loading records in batches. https://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each 3. Using `pluck` creates only an array, avoiding loading all the other columns of the records into objects. Running this on the current Canadian database, fixes the following variant overrides: ``` [] [] [] [] [] [] [925, 924, 966, 965] [] [] [] [] [462, 863, 464, 822, 949, 947, 944, 939, 942, 946, 945, 943, 438, 937, 938, 941, 940, 467, 952, 875, 453, 953, 454, 951, 487, 460, 457, 528, 527, 486, 459, 458, 461, 529, 530, 950, 642, 384, 380, 643, 385, 381, 644, 386, 382, 960, 959, 379, 640, 377, 375, 532, 639, 376, 374, 646, 390, 389, 637, 406, 408, 647, 391, 393, 633, 396, 400, 398, 645, 388, 387, 648, 394, 392, 536, 632, 399, 397, 395, 634, 403, 401, 635, 404, 402, 636, 407, 405, 535, 534, 638, 410, 409, 948, 533, 537, 531, 877, 880, 894, 893, 672, 671, 673, 674, 703, 714, 715, 716, 717, 862, 864, 879, 876, 865, 881, 878, 463, 954, 866, 823, 957, 958, 955, 956, 899, 897] [] [969] ``` --- ...020103501_revoke_variant_overrideswithout_permissions.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/db/migrate/20181020103501_revoke_variant_overrideswithout_permissions.rb b/db/migrate/20181020103501_revoke_variant_overrideswithout_permissions.rb index bd2d0e750b..d8140c9bf3 100644 --- a/db/migrate/20181020103501_revoke_variant_overrideswithout_permissions.rb +++ b/db/migrate/20181020103501_revoke_variant_overrideswithout_permissions.rb @@ -2,11 +2,11 @@ class RevokeVariantOverrideswithoutPermissions < ActiveRecord::Migration def up # This process was executed when the permission_revoked_at colum was created (see AddPermissionRevokedAtToVariantOverrides) # It needs to be repeated due to #2739 - variant_override_hubs = Enterprise.where(id: VariantOverride.all.map(&:hub_id).uniq) + variant_override_hubs = Enterprise.where(id: VariantOverride.select(:hub_id).uniq) - variant_override_hubs.each do |hub| + variant_override_hubs.find_each do |hub| permitting_producer_ids = hub.relationships_as_child - .with_permission(:create_variant_overrides).map(&:parent_id) + .with_permission(:create_variant_overrides).pluck(:parent_id) variant_overrides_with_revoked_permissions = VariantOverride.for_hubs(hub) .joins(variant: :product).where("spree_products.supplier_id NOT IN (?)", permitting_producer_ids)