2010-11-24 04:27:50 +08:00
|
|
|
#!/usr/bin/perl
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
use strict;
|
2012-07-05 09:47:49 +08:00
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
# make sure we are in a sane environment.
|
|
|
|
use DBI();
|
|
|
|
use DBD::Pg();
|
|
|
|
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
|
|
|
|
use Getopt::Std;
|
|
|
|
|
|
|
|
my %opt;
|
|
|
|
getopts('d:b:s:veorauc', \%opt);
|
|
|
|
|
2012-07-05 09:47:49 +08:00
|
|
|
if (!(scalar %opt && defined $opt{s}))
|
|
|
|
{
|
2001-01-12 08:16:26 +08:00
|
|
|
print <<EOT;
|
|
|
|
Usage:
|
|
|
|
$0 -d DATABASE -s SECTIONS [-b NUMBER] [-v] [-e] [-o] [-r] [-a] [-u]
|
2010-11-24 04:27:50 +08:00
|
|
|
-d DATABASE -DATABASE
|
|
|
|
-b NUMBER -number of repeats
|
|
|
|
-s SECTIONS -sections, format sid1[,sid2[,sid3[...]]]]
|
|
|
|
-v -verbose (show SQL)
|
2001-01-12 08:16:26 +08:00
|
|
|
-e -show explain
|
|
|
|
-r -use RD-tree index
|
|
|
|
-a -AND section
|
|
|
|
-o -show output
|
|
|
|
-u -unique
|
2010-11-24 04:27:50 +08:00
|
|
|
-c -count
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
EOT
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$opt{d} ||= '_int4';
|
2012-07-05 09:47:49 +08:00
|
|
|
my $dbi = DBI->connect('DBI:Pg:dbname=' . $opt{d});
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
my %table;
|
|
|
|
my @where;
|
|
|
|
|
2012-07-05 09:47:49 +08:00
|
|
|
$table{message} = 1;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2012-07-05 09:47:49 +08:00
|
|
|
if ($opt{a})
|
|
|
|
{
|
|
|
|
if ($opt{r})
|
|
|
|
{
|
2001-01-12 08:16:26 +08:00
|
|
|
push @where, "message.sections @ '{$opt{s}}'";
|
2012-07-05 09:47:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach my $sid (split(/[,\s]+/, $opt{s}))
|
|
|
|
{
|
2005-10-03 22:41:42 +08:00
|
|
|
push @where, "message.mid = msp$sid.mid";
|
|
|
|
push @where, "msp$sid.sid = $sid";
|
2012-07-05 09:47:49 +08:00
|
|
|
$table{"message_section_map msp$sid"} = 1;
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
}
|
2012-07-05 09:47:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($opt{r})
|
|
|
|
{
|
2001-01-12 08:16:26 +08:00
|
|
|
push @where, "message.sections && '{$opt{s}}'";
|
2012-07-05 09:47:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-01-12 08:16:26 +08:00
|
|
|
$table{message_section_map} = 1;
|
|
|
|
push @where, "message.mid = message_section_map.mid";
|
|
|
|
push @where, "message_section_map.sid in ($opt{s})";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $outf;
|
2012-07-05 09:47:49 +08:00
|
|
|
if ($opt{c})
|
|
|
|
{
|
|
|
|
$outf =
|
|
|
|
($opt{u}) ? 'count( distinct message.mid )' : 'count( message.mid )';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$outf = ($opt{u}) ? 'distinct( message.mid )' : 'message.mid';
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
2012-07-05 09:47:49 +08:00
|
|
|
my $sql =
|
2014-05-07 00:12:18 +08:00
|
|
|
"select $outf from "
|
2012-07-05 09:47:49 +08:00
|
|
|
. join(', ', keys %table)
|
|
|
|
. " where "
|
|
|
|
. join(' AND ', @where) . ';';
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2012-07-05 09:47:49 +08:00
|
|
|
if ($opt{v})
|
|
|
|
{
|
2001-01-12 08:16:26 +08:00
|
|
|
print "$sql\n";
|
|
|
|
}
|
|
|
|
|
2012-07-05 09:47:49 +08:00
|
|
|
if ($opt{e})
|
|
|
|
{
|
2017-05-18 07:01:23 +08:00
|
|
|
my @plan =
|
|
|
|
map { "$_->[0]\n" } @{ $dbi->selectall_arrayref("explain $sql") };
|
2016-08-18 03:51:10 +08:00
|
|
|
print @plan;
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2012-07-05 09:47:49 +08:00
|
|
|
my $t0 = [gettimeofday];
|
|
|
|
my $count = 0;
|
|
|
|
my $b = $opt{b};
|
|
|
|
$b ||= 1;
|
2001-01-12 08:16:26 +08:00
|
|
|
my @a;
|
2012-07-05 09:47:49 +08:00
|
|
|
foreach (1 .. $b)
|
|
|
|
{
|
|
|
|
@a = exec_sql($dbi, $sql);
|
|
|
|
$count = $#a;
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
2012-07-05 09:47:49 +08:00
|
|
|
my $elapsed = tv_interval($t0, [gettimeofday]);
|
|
|
|
if ($opt{o})
|
|
|
|
{
|
|
|
|
foreach (@a)
|
|
|
|
{
|
2001-01-12 08:16:26 +08:00
|
|
|
print "$_->{mid}\t$_->{sections}\n";
|
|
|
|
}
|
2010-11-24 04:27:50 +08:00
|
|
|
}
|
2012-07-05 09:47:49 +08:00
|
|
|
print sprintf(
|
|
|
|
"total: %.02f sec; number: %d; for one: %.03f sec; found %d docs\n",
|
|
|
|
$elapsed, $b, $elapsed / $b,
|
|
|
|
$count + 1);
|
|
|
|
$dbi->disconnect;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2012-07-05 09:47:49 +08:00
|
|
|
sub exec_sql
|
|
|
|
{
|
|
|
|
my ($dbi, $sql, @keys) = @_;
|
|
|
|
my $sth = $dbi->prepare($sql) || die;
|
|
|
|
$sth->execute(@keys) || die;
|
|
|
|
my $r;
|
|
|
|
my @row;
|
|
|
|
while (defined($r = $sth->fetchrow_hashref))
|
|
|
|
{
|
|
|
|
push @row, $r;
|
|
|
|
}
|
|
|
|
$sth->finish;
|
|
|
|
return @row;
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|