From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 1CFFE1FAE7 for ; Mon, 2 Apr 2018 00:04:58 +0000 (UTC) From: "Eric Wong (Contractor, The Linux Foundation)" To: meta@public-inbox.org Subject: [PATCH 5/5] over: speedup get_thread by avoiding JOIN Date: Mon, 2 Apr 2018 00:04:56 +0000 Message-Id: <20180402000456.13446-6-e@80x24.org> In-Reply-To: <20180402000456.13446-1-e@80x24.org> References: <20180402000456.13446-1-e@80x24.org> List-Id: 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 c74072a..3d285ac 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; -- EW