[Bps-public-commit] dbix-searchbuilder branch, master, updated. 1.65-3-g82d6883

Alex Vandiver alexmv at bestpractical.com
Tue Jul 8 14:43:03 EDT 2014


The branch, master has been updated
       via  82d6883342487225c25487ff7ac151fbac5569e6 (commit)
       via  0d04f6f992a49c4e992479f12977d301d967a97c (commit)
      from  e4093f97f1bd976b38231141971c3e0f018931a3 (commit)

Summary of changes:
 lib/DBIx/SearchBuilder/Handle.pm    | 11 ++++++-----
 lib/DBIx/SearchBuilder/Handle/Pg.pm | 12 +++++++++---
 2 files changed, 15 insertions(+), 8 deletions(-)

- Log -----------------------------------------------------------------
commit 0d04f6f992a49c4e992479f12977d301d967a97c
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Tue May 13 15:04:30 2014 -0400

    We can omit the (heavy on Pg) ->Fields call on 9.1 and above
    
    Postgres 9.1 and higher can intuit the GROUP BY for all other columns if
    just the primary key is listed[1], like MySQL does.  As ->Fields can be
    a very heavyweight call (~5s, as DBD::Pg::column_info is slow), GROUP
    purely by the id if possible.
    
    [1] http://www.postgresql.org/docs/9.1/static/release-9-1.html#AEN109008

diff --git a/lib/DBIx/SearchBuilder/Handle/Pg.pm b/lib/DBIx/SearchBuilder/Handle/Pg.pm
index 14be2bf..a170e8c 100755
--- a/lib/DBIx/SearchBuilder/Handle/Pg.pm
+++ b/lib/DBIx/SearchBuilder/Handle/Pg.pm
@@ -235,9 +235,15 @@ sub DistinctQuery {
     # It's hard to show with tests. Pg's optimizer can choose execution
     # plan not guaranting order
 
-    # So if we are ordering by something that is not in 'main', the we GROUP
-    # BY all columns and adjust the ORDER_BY accordingly
-    local $sb->{group_by} = [ map {+{FIELD => $_}} $self->Fields($table) ];
+    my $groups;
+    if ($self->DatabaseVersion =~ /^(\d+)\.(\d+)/ and ($1 > 9 or ($1 == 9 and $2 >= 1))) {
+        # Pg 9.1 supports "SELECT main.foo ... GROUP BY main.id" if id is the primary key
+        $groups = [ {FIELD => "id"} ];
+    } else {
+        # For earlier versions, we have to list out all of the columns
+        $groups = [ map {+{FIELD => $_}} $self->Fields($table) ];
+    }
+    local $sb->{group_by} = $groups;
     local $sb->{'order_by'} = [
         map {
             ($_->{'ALIAS'}||'') ne "main"

commit 82d6883342487225c25487ff7ac151fbac5569e6
Author: Michelle Sullivan <michelle at sorbs.net>
Date:   Tue Jul 8 14:38:28 2014 -0400

    Only request column_info for the requested tables
    
    Calling ->column_info( undef, '%', '%') requests column information for
    all tables in all schemas; although this information is cached, it can
    be extremely slow, especially when the Postgres server is not local.
    Look up and cache the table information on a table-by-table basis,
    instead of all at once, for considerale performance savings.
    
    Note that the caching assumes that table names are case-sensitively
    unique within a database.

diff --git a/lib/DBIx/SearchBuilder/Handle.pm b/lib/DBIx/SearchBuilder/Handle.pm
index 20bf98f..d974356 100755
--- a/lib/DBIx/SearchBuilder/Handle.pm
+++ b/lib/DBIx/SearchBuilder/Handle.pm
@@ -1428,18 +1428,19 @@ sub DistinctCount {
 
 sub Fields {
     my $self  = shift;
-    my $table = shift;
+    my $table = lc shift;
 
-    unless ( keys %FIELDS_IN_TABLE ) {
-        my $sth = $self->dbh->column_info( undef, '', '%', '%' )
+    unless ( $FIELDS_IN_TABLE{$table} ) {
+        $FIELDS_IN_TABLE{ $table } = [];
+        my $sth = $self->dbh->column_info( undef, '', $table, '%' )
             or return ();
         my $info = $sth->fetchall_arrayref({});
         foreach my $e ( @$info ) {
-            push @{ $FIELDS_IN_TABLE{ lc $e->{'TABLE_NAME'} } ||= [] }, lc $e->{'COLUMN_NAME'};
+            push @{ $FIELDS_IN_TABLE{ $table } }, lc $e->{'COLUMN_NAME'};
         }
     }
 
-    return @{ $FIELDS_IN_TABLE{ lc $table } || [] };
+    return @{ $FIELDS_IN_TABLE{ $table } };
 }
 
 

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list