From abe71a767b266a157836c0fd45330ec415720bef Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 12 Mar 2012 19:42:59 +0100 Subject: [PATCH] Disable prepared statements completely Because of the way DBIx::Class does prepared statements, even innocuous queries such $c->model('DB::Builds)->search({finished => 0}) can be extremely slow. This is because DBIx::Class prepares a PostgreSQL statement select ... from Builds where finished = ? and since Builds is very large and there is a large fraction of rows with "finished = 1", the PostgreSQL query planner decides to implement this query with a sequential scan of the Builds table (despite the existence of an index on "finished"), which is extremely slow. It would be nice if we could tell DBIx::Class that constants should be part of the prepared statement, i.e. select ... from Builds where finished = 0 but AFAIK we can't. --- src/lib/Hydra/Controller/Root.pm | 14 +++++--------- src/lib/Hydra/Model/DB.pm | 5 ++++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/lib/Hydra/Controller/Root.pm b/src/lib/Hydra/Controller/Root.pm index 5e2ff399..598df5fb 100644 --- a/src/lib/Hydra/Controller/Root.pm +++ b/src/lib/Hydra/Controller/Root.pm @@ -95,15 +95,11 @@ sub timeline :Local { sub status :Local { my ($self, $c) = @_; - $c->model('DB')->storage->dbh_do(sub { - my (undef, $dbh) = @_; - local $dbh->{pg_server_prepare} = 0; - $c->stash->{steps} = [ $c->model('DB::BuildSteps')->search( - { 'me.busy' => 1, 'build.finished' => 0, 'build.busy' => 1 }, - { join => [ 'build' ] - , order_by => [ 'machine' ] - } ) ]; - }); + $c->stash->{steps} = [ $c->model('DB::BuildSteps')->search( + { 'me.busy' => 1, 'build.finished' => 0, 'build.busy' => 1 }, + { join => [ 'build' ] + , order_by => [ 'machine' ] + } ) ]; } diff --git a/src/lib/Hydra/Model/DB.pm b/src/lib/Hydra/Model/DB.pm index c7d31bfa..eb5f5168 100644 --- a/src/lib/Hydra/Model/DB.pm +++ b/src/lib/Hydra/Model/DB.pm @@ -6,7 +6,10 @@ use Hydra::Helper::Nix; __PACKAGE__->config( schema_class => 'Hydra::Schema', - connect_info => [getHydraDBPath], + connect_info => { + dsn => getHydraDBPath, + pg_server_prepare => 0, + }, ); =head1 NAME