]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Msgmap.pm
msgmap: speed up minmax with separate queries
[public-inbox.git] / lib / PublicInbox / Msgmap.pm
index f5f88431bbfa82d1e7af847126d14441f6409864..feef8ba79ab9a657d8135b0c65ca0186dd7bed51 100644 (file)
@@ -138,10 +138,14 @@ sub num_for {
 sub minmax {
        my ($self) = @_;
        my $dbh = $self->{dbh};
-       my $sth = $self->{num_minmax} ||=
-               $dbh->prepare('SELECT MIN(num),MAX(num) FROM msgmap');
+       # breaking MIN and MAX into separate queries speeds up from 250ms
+       # to around 700us with 2.7million messages.
+       my $sth = $dbh->prepare_cached('SELECT MIN(num) FROM msgmap', undef, 1);
        $sth->execute;
-        $sth->fetchrow_array;
+       my $min = $sth->fetchrow_array;
+       $sth = $dbh->prepare_cached('SELECT MAX(num) FROM msgmap', undef, 1);
+       $sth->execute;
+       ($min, $sth->fetchrow_array);
 }
 
 sub mid_prefixes {