15902: Adjust order to avoid poorly optimized query.
authorTom Clegg <tom@tomclegg.ca>
Tue, 28 Jan 2020 20:05:02 +0000 (15:05 -0500)
committerTom Clegg <tom@tomclegg.ca>
Tue, 28 Jan 2020 20:05:02 +0000 (15:05 -0500)
When many requesting_container_uuid values are passed in this query,
the default order (["modified_at desc", "uuid"]) sometimes causes
PostgreSQL to begin with the timestamp index, which results in a slow
query. The requesting_container_uuid index makes the query much
faster.

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@tomclegg.ca>

apps/workbench/app/models/container_work_unit.rb

index faba062d469159748915444a5e9cfe583031b625..292bc3679bc3710dacf2b59077726fd77d89b5e1 100644 (file)
@@ -29,7 +29,7 @@ class ContainerWorkUnit < ProxyWorkUnit
       my_child_containers = my_children.map(&:container_uuid).compact.uniq
       grandchildren = {}
       my_child_containers.each { |c| grandchildren[c] = []} if my_child_containers.any?
-      reqs = ContainerRequest.select(cols).where(requesting_container_uuid: my_child_containers).with_count("none").results if my_child_containers.any?
+      reqs = ContainerRequest.select(cols).where(requesting_container_uuid: my_child_containers).order(["requesting_container_uuid", "uuid"]).with_count("none").results if my_child_containers.any?
       reqs.each {|cr| grandchildren[cr.requesting_container_uuid] << cr} if reqs
 
       my_children.each do |cr|