From dd7ab1d74b8fe1d9d188da8da91ae9cf809fced2 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 12 Dec 2013 16:28:40 +0900 Subject: [PATCH] change the PK NAME select query from the core postgresql class to a more faster version --- www/libs/Class.DB.IO.inc | 16 +++++++++++----- www/libs/db_pgsql.inc | 28 +++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/www/libs/Class.DB.IO.inc b/www/libs/Class.DB.IO.inc index 7886fa32..5bc60ad9 100644 --- a/www/libs/Class.DB.IO.inc +++ b/www/libs/Class.DB.IO.inc @@ -2,8 +2,8 @@ /******************************************************************** * $HeadURL: svn://svn/development/core_data/php/www/libs/Class.DB.IO.inc $ * $LastChangedBy: gullevek $ - * $LastChangedDate: 2013-10-24 16:05:16 +0900 (Thu, 24 Oct 2013) $ - * $LastChangedRevision: 4706 $ + * $LastChangedDate: 2013-12-12 16:28:35 +0900 (Thu, 12 Dec 2013) $ + * $LastChangedRevision: 4741 $ ********************************************************************* * AUTHOR: Clemens "Gullevek" Schwaighofer (www.gullevek.org) * CREATED: 2000/11/23 @@ -369,9 +369,9 @@ $this->class_info['db_io']=array( 'class_name' => 'DB IO', 'class_version' => '4.1.0', - 'class_revision' => '$LastChangedRevision: 4706 $', + 'class_revision' => '$LastChangedRevision: 4741 $', 'class_created' => '2000-11-23', - 'class_last_changed' => '$LastChangedDate: 2013-10-24 16:05:16 +0900 (Thu, 24 Oct 2013) $', + 'class_last_changed' => '$LastChangedDate: 2013-12-12 16:28:35 +0900 (Thu, 12 Dec 2013) $', 'class_author' => 'Clemens Schwaighofer' ); } @@ -1049,6 +1049,8 @@ // DESC executes the query and returns & sets the internal cursor // fruthermore this functions also sets varios other vars // like num_rows, num_fields, etc depending on query + // for INSERT INTO queries it is highly recommended to set the pk_name to avoid an additional + // read from the database for the PK NAME public function db_exec($query = 0, $pk_name = '') { // prepare and check if we can actually run it @@ -1070,6 +1072,8 @@ // pk_name -> optional primary key name, only used with insert for returning call // RETURN true if async query was sent ok, false if error happened // DESC executres the query async so other methods can be run during this + // for INSERT INTO queries it is highly recommended to set the pk_name to avoid an additional + // read from the database for the PK NAME // NEEDS db_check_async public function db_exec_async($query, $pk_name = '') { @@ -1257,6 +1261,8 @@ // PARAMS $stm_name, $query, $pk_name: optional // RETURN false on error // DESC prepares a query + // for INSERT INTO queries it is highly recommended to set the pk_name to avoid an additional + // read from the database for the PK NAME public function db_prepare($stm_name, $query, $pk_name = '') { if (!$query) @@ -1652,5 +1658,5 @@ $this->debug('ExecuteData', 'ERROR in STM['.$stm_name.'|'.$this->prepare_cursor[ } } // end if db class - // $Id: Class.DB.IO.inc 4706 2013-10-24 07:05:16Z gullevek $ + // $Id: Class.DB.IO.inc 4741 2013-12-12 07:28:35Z gullevek $ ?> diff --git a/www/libs/db_pgsql.inc b/www/libs/db_pgsql.inc index d46c8f8e..fe1c0d51 100644 --- a/www/libs/db_pgsql.inc +++ b/www/libs/db_pgsql.inc @@ -2,8 +2,8 @@ /********************************************************************* * $HeadURL: svn://svn/development/core_data/php/www/libs/db_pgsql.inc $ * $LastChangedBy: gullevek $ - * $LastChangedDate: 2013-10-11 16:54:21 +0900 (Fri, 11 Oct 2013) $ - * $LastChangedRevision: 4689 $ + * $LastChangedDate: 2013-12-12 16:28:35 +0900 (Thu, 12 Dec 2013) $ + * $LastChangedRevision: 4741 $ ********************************************************************* * AUTHOR: Clemens "Gullevek" Schwaighofer (www.gullevek.org) * CREATED: 2003/04/09 @@ -232,13 +232,31 @@ if ($table) { // read from table the PK name - $q = "SELECT c.column_name "; + // slower version +/* $q = "SELECT c.column_name "; $q .= "FROM information_schema.table_constraints tc "; $q .= "JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) "; $q .= "JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name "; $q .= "WHERE constraint_type = 'PRIMARY KEY' and tc.table_name = '".$table."'"; if ($schema) - $q .= " AND c.table_schema = '".$schema."'"; + $q .= " AND c.table_schema = '".$schema."'"; */ + // faster primary key get + $q = "SELECT pg_attribute.attname AS column_name, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type "; + $q .= "FROM pg_index, pg_class, pg_attribute"; + if ($schema) + $q .= ", pg_namespace "; + $q .= "WHERE "; + // regclass translates the OID to the name + $q .= "pg_class.oid = '".$table."'::regclass AND "; + $q .= "indrelid = pg_class.oid AND "; + if ($schema) + { + $q .= "nspname = '".$schema."' AND "; + $q .= "pg_class.relnamespace = pg_namespace.oid AND "; + } + $q .= "pg_attribute.attrelid = pg_class.oid AND "; + $q .= "pg_attribute.attnum = any(pg_index.indkey) "; + $q .= "AND indisprimary"; $cursor = $this->_db_query($q); return $this->_db_fetch_array($cursor)['column_name']; } @@ -364,5 +382,5 @@ } } - // $Id: db_pgsql.inc 4689 2013-10-11 07:54:21Z gullevek $ + // $Id: db_pgsql.inc 4741 2013-12-12 07:28:35Z gullevek $ ?>