]> Sergey Matveev's repositories - public-inbox.git/commitdiff
over: speedup get_thread by avoiding JOIN
authorEric Wong (Contractor, The Linux Foundation) <e@80x24.org>
Mon, 2 Apr 2018 00:04:56 +0000 (00:04 +0000)
committerEric Wong (Contractor, The Linux Foundation) <e@80x24.org>
Mon, 2 Apr 2018 00:05:43 +0000 (00:05 +0000)
JOIN operations on SQLite can be disasterously slow.
This reduces per-message pages with the thread overview
at the bottom of those pages from over 800ms to ~60ms.
In comparison, the v1 code took around 70-80ms using
Xapian on my machine.

lib/PublicInbox/Over.pm

index c74072a26c063ea3c8acaebf1b06f9da34ba03a8..3d285ac265183b3cf396bd4038d8baf19ddb5ae2 100644 (file)
@@ -73,20 +73,31 @@ ORDER BY ts ASC
 
 }
 
 
 }
 
+sub nothing () { wantarray ? (0, []) : [] };
+
 sub get_thread {
        my ($self, $mid, $opts) = @_;
        my $dbh = $self->connect;
 sub get_thread {
        my ($self, $mid, $opts) = @_;
        my $dbh = $self->connect;
-       my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $mid);
-SELECT tid,sid FROM over
-LEFT JOIN id2num ON over.num = id2num.num
-LEFT JOIN msgid ON id2num.id = msgid.id
-WHERE msgid.mid = ? AND over.num > 0
-LIMIT 1
+
+       my $id = $dbh->selectrow_array(<<'', undef, $mid);
+SELECT id FROM msgid WHERE mid = ? LIMIT 1
+
+       defined $id or return nothing;
+
+       my $num = $dbh->selectrow_array(<<'', undef, $id);
+SELECT num FROM id2num WHERE id = ? AND num > 0
+ORDER BY num ASC LIMIT 1
+
+       defined $num or return nothing;
+
+       my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
+SELECT tid,sid FROM over WHERE num = ? LIMIT 1
+
+       defined $tid or return nothing; # $sid may be undef
 
        my $cond = 'FROM over WHERE (tid = ? OR sid = ?) AND num > 0';
        my $msgs = do_get($self, <<"", $opts, $tid, $sid);
 
        my $cond = 'FROM over WHERE (tid = ? OR sid = ?) AND num > 0';
        my $msgs = do_get($self, <<"", $opts, $tid, $sid);
-SELECT * $cond
-ORDER BY ts ASC
+SELECT * $cond ORDER BY ts ASC
 
        return $msgs unless wantarray;
 
 
        return $msgs unless wantarray;