I have a Postgres table with more than 8 million rows. Given the following two ways of doing the same query via DBD::Pg
, I get wildly different results.
$q .= '%';
## query 1
my $sql = qq{
SELECT a, b, c
FROM t
WHERE Lower( a ) LIKE '$q'
};
my $sth1 = $dbh->prepare($sql);
$sth1->execute();
## query 2
my $sth2 = $dbh->prepare(qq{
SELECT a, b, c
FROM t
WHERE Lower( a ) LIKE ?
});
$sth2->execute($q);
query 2 is at least an order of magnitude slower than query 1... seems like it is not using the indexes, while query 1 is using the index.
Would love hear why.
use Benchmark;
– Recur$q
before the first line? – Success$sth2->bind_param(1, $q, { pg_type => PG_TEXT }); $sth2->execute();
(You will need touse DBD::Pg qw(:pg_types);
at the top to getPG_TEXT
.) – Howundt
? From the looks of it, you should have an index overLower(a)
(in contrast to just indexing overa
). – Howund$q
and use a where clause likeWHERE Lower( a ) LIKE ? || '%'
? – Hillside