Skip to content

Commit

Permalink
Fix #80027 Terrible performance using $query->fetch on queries with m…
Browse files Browse the repository at this point in the history
…any bind parameters

Added new flags that allow skipping param_evt(s) that are not used by drivers,
in a backwards and forward compatible manner. Updated the pgsql, mysql, sqlite
and oci drivers to properly use the new flags. I've left out pdo_dblib, which
doesn't have a param_hook, and pdo_firebird, which seems to be using
PARAM_EVT_NORMALIZE in a wrong context (param type vs event type).
  • Loading branch information
mbeccati committed Aug 31, 2020
1 parent ad750c3 commit 44ade0e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ PHP NEWS
. Fixed bug #80002 (calc free space for new interned string is wrong).
(t-matsuno)

- PDO:
. Fixed bug #80027 (Terrible performance using $query->fetch on queries with
many bind parameters (Matteo)

- Standard:
. Fixed bug #79986 (str_ireplace bug with diacritics characters). (cmb)

Expand Down
4 changes: 4 additions & 0 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ static int dispatch_param_event(pdo_stmt_t *stmt, enum pdo_param_event event_typ
struct pdo_bound_param_data *param;
HashTable *ht;

if (stmt->dbh->skip_param_evt & (1 << event_type)) {
return 1;
}

if (!stmt->methods->param_hook) {
return 1;
}
Expand Down
5 changes: 4 additions & 1 deletion ext/pdo/php_pdo_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,12 @@ struct _pdo_dbh_t {
/* when set, convert int/floats to strings */
unsigned stringify:1;

/* bitmap for pdo_param_event(s) to skip in dispatch_param_event */
unsigned skip_param_evt:7;

/* the sum of the number of bits here and the bit fields preceding should
* equal 32 */
unsigned _reserved_flags:21;
unsigned _reserved_flags:14;

/* data source string used to open this handle */
const char *data_source;
Expand Down
7 changes: 7 additions & 0 deletions ext/pdo_mysql/mysql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,13 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)

dbh->driver_data = H;

dbh->skip_param_evt =
1 << PDO_PARAM_EVT_FREE |
1 << PDO_PARAM_EVT_EXEC_POST |
1 << PDO_PARAM_EVT_FETCH_PRE |
1 << PDO_PARAM_EVT_FETCH_POST |
1 << PDO_PARAM_EVT_NORMALIZE;

#ifndef PDO_USE_MYSQLND
H->max_buffer_size = 1024*1024;
#endif
Expand Down
5 changes: 5 additions & 0 deletions ext/pdo_oci/oci_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,11 @@ static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ *
H = pecalloc(1, sizeof(*H), dbh->is_persistent);
dbh->driver_data = H;

dbh->skip_param_evt =
1 << PDO_PARAM_EVT_FETCH_PRE |
1 << PDO_PARAM_EVT_FETCH_POST |
1 << PDO_PARAM_EVT_NORMALIZE;

H->prefetch = PDO_OCI_PREFETCH_DEFAULT;

/* allocate an environment */
Expand Down
5 changes: 5 additions & 0 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,11 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
dbh->driver_data = H;

dbh->skip_param_evt =
1 << PDO_PARAM_EVT_EXEC_POST |
1 << PDO_PARAM_EVT_FETCH_PRE |
1 << PDO_PARAM_EVT_FETCH_POST;

H->einfo.errcode = 0;
H->einfo.errmsg = NULL;

Expand Down
2 changes: 1 addition & 1 deletion ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *
}
break;
}
} else if (param->is_param) {
} else if (param->is_param && event_type == PDO_PARAM_EVT_NORMALIZE) {
/* We need to manually convert to a pg native boolean value */
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
Expand Down
3 changes: 3 additions & 0 deletions ext/pdo_sqlite/sqlite_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,9 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{
H->einfo.errmsg = NULL;
dbh->driver_data = H;

/* skip all but this one param event */
dbh->skip_param_evt = 0x7F ^ (1 << PDO_PARAM_EVT_EXEC_PRE);

filename = make_filename_safe(dbh->data_source);

if (!filename) {
Expand Down

0 comments on commit 44ade0e

Please sign in to comment.