]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Msgmap.pm
Merge remote-tracking branch 'origin/master' into v2
[public-inbox.git] / lib / PublicInbox / Msgmap.pm
index f5f88431bbfa82d1e7af847126d14441f6409864..ec3d4f9d8101fe0e226895207f2a486016e8a223 100644 (file)
@@ -78,7 +78,7 @@ sub meta_accessor {
        $prev = $dbh->selectrow_array($sql, undef, $key);
 
        if (defined $prev) {
-               $sql = 'UPDATE meta SET val = ? WHERE key = ? LIMIT 1';
+               $sql = 'UPDATE meta SET val = ? WHERE key = ?';
                $dbh->do($sql, undef, $value, $key);
        } else {
                $sql = 'INSERT INTO meta (key,val) VALUES (?,?)';
@@ -108,10 +108,10 @@ sub created_at {
 sub mid_insert {
        my ($self, $mid) = @_;
        my $dbh = $self->{dbh};
-       my $sql = 'INSERT OR IGNORE INTO msgmap (mid) VALUES (?)';
-       my $sth = $self->{mid_insert} ||= $dbh->prepare($sql);
-       $sth->bind_param(1, $mid);
-       return if $sth->execute == 0;
+       my $sth = $dbh->prepare_cached(<<'');
+INSERT OR IGNORE INTO msgmap (mid) VALUES (?)
+
+       return if $sth->execute($mid) == 0;
        $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
 }
 
@@ -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;
+       my $min = $sth->fetchrow_array;
+       $sth = $dbh->prepare_cached('SELECT MAX(num) FROM msgmap', undef, 1);
        $sth->execute;
-        $sth->fetchrow_array;
+       ($min, $sth->fetchrow_array);
 }
 
 sub mid_prefixes {