From 5a44b89fd9f4ef2688b91262736b0f61095ae99c Mon Sep 17 00:00:00 2001 From: "Eric Wong (Contractor, The Linux Foundation)" Date: Mon, 2 Apr 2018 00:04:56 +0000 Subject: [PATCH] over: speedup get_thread by avoiding JOIN 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 | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/PublicInbox/Over.pm b/lib/PublicInbox/Over.pm index c74072a2..3d285ac2 100644 --- a/lib/PublicInbox/Over.pm +++ b/lib/PublicInbox/Over.pm @@ -73,20 +73,31 @@ ORDER BY ts ASC } +sub nothing () { wantarray ? (0, []) : [] }; + 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); -SELECT * $cond -ORDER BY ts ASC +SELECT * $cond ORDER BY ts ASC return $msgs unless wantarray; -- 2.44.0