From: "Eric Wong (Contractor, The Linux Foundation)" <e@80x24.org>
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 [thread overview]
Message-ID: <20180402000456.13446-6-e@80x24.org> (raw)
In-Reply-To: <20180402000456.13446-1-e@80x24.org>
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
prev parent reply other threads:[~2018-04-02 0:04 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-02 0:04 [PATCH 0/5] v2: drop Xapian skeleton for SQLite overview DB Eric Wong (Contractor, The Linux Foundation)
2018-04-02 0:04 ` [PATCH 1/5] replace Xapian skeleton with " Eric Wong (Contractor, The Linux Foundation)
2018-04-05 8:59 ` Eric Wong
2018-04-02 0:04 ` [PATCH 2/5] v2writable: simplify barrier vs checkpoints Eric Wong (Contractor, The Linux Foundation)
2018-04-02 0:04 ` [PATCH 3/5] t/over: test empty Subject: line matching Eric Wong (Contractor, The Linux Foundation)
2018-04-02 0:04 ` [PATCH 4/5] www: rework query responses to avoid COUNT in SQLite Eric Wong (Contractor, The Linux Foundation)
2018-04-02 0:04 ` Eric Wong (Contractor, The Linux Foundation) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180402000456.13446-6-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).