From: lpsolit%gmail.com <> Date: Mon, 8 Sep 2008 21:23:46 +0000 (+0000) Subject: Bug 453743: Decrease the number of calls to the DB about flags when viewing a bug... X-Git-Tag: bugzilla-3.2rc2~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd7db23ce85712480bc04d439b0a7fecbc3755df;p=thirdparty%2Fbugzilla.git Bug 453743: Decrease the number of calls to the DB about flags when viewing a bug - Patch by Frédéric Buclin r/a=mkanat --- diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 37e4ff5bc2..5ba9d9a462 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -2228,8 +2228,19 @@ sub attachments { return $self->{'attachments'} if exists $self->{'attachments'}; return [] if $self->{'error'}; - $self->{'attachments'} = - Bugzilla::Attachment->get_attachments_by_bug($self->bug_id); + my $attachments = Bugzilla::Attachment->get_attachments_by_bug($self->bug_id); + $_->{'flags'} = [] foreach @$attachments; + my %att = map { $_->id => $_ } @$attachments; + + # Retrieve all attachment flags at once for this bug, and group them + # by attachment. We populate attachment flags here to avoid querying + # the DB for each attachment individually later. + my $flags = Bugzilla::Flag->match({ 'bug_id' => $self->bug_id, + 'target_type' => 'attachment' }); + + push(@{$att{$_->attach_id}->{'flags'}}, $_) foreach @$flags; + + $self->{'attachments'} = [sort {$a->id <=> $b->id} values %att]; return $self->{'attachments'}; } @@ -2343,14 +2354,20 @@ sub flag_types { 'product_id' => $self->{'product_id'}, 'component_id' => $self->{'component_id'} }); - foreach my $flag_type (@$flag_types) { - $flag_type->{'flags'} = Bugzilla::Flag->match( - { 'bug_id' => $self->bug_id, - 'type_id' => $flag_type->{'id'}, - 'target_type' => 'bug' }); - } + $_->{'flags'} = [] foreach @$flag_types; + my %flagtypes = map { $_->id => $_ } @$flag_types; + + # Retrieve all bug flags at once for this bug and group them + # by flag types. + my $flags = Bugzilla::Flag->match({ 'bug_id' => $self->bug_id, + 'target_type' => 'bug' }); + + # Call the internal 'type_id' variable instead of the method + # to not create a flagtype object. + push(@{$flagtypes{$_->{'type_id'}}->{'flags'}}, $_) foreach @$flags; - $self->{'flag_types'} = $flag_types; + $self->{'flag_types'} = + [sort {$a->sortkey <=> $b->sortkey || $a->name cmp $b->name} values %flagtypes]; return $self->{'flag_types'}; }