Skip to content

Commit 44ade0e

Browse files
committed
Fix #80027 Terrible performance using $query->fetch on queries with many 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).
1 parent ad750c3 commit 44ade0e

File tree

8 files changed

+33
-2
lines changed

8 files changed

+33
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ PHP NEWS
1414
. Fixed bug #80002 (calc free space for new interned string is wrong).
1515
(t-matsuno)
1616

17+
- PDO:
18+
. Fixed bug #80027 (Terrible performance using $query->fetch on queries with
19+
many bind parameters (Matteo)
20+
1721
- Standard:
1822
. Fixed bug #79986 (str_ireplace bug with diacritics characters). (cmb)
1923

ext/pdo/pdo_stmt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ static int dispatch_param_event(pdo_stmt_t *stmt, enum pdo_param_event event_typ
163163
struct pdo_bound_param_data *param;
164164
HashTable *ht;
165165

166+
if (stmt->dbh->skip_param_evt & (1 << event_type)) {
167+
return 1;
168+
}
169+
166170
if (!stmt->methods->param_hook) {
167171
return 1;
168172
}

ext/pdo/php_pdo_driver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,12 @@ struct _pdo_dbh_t {
468468
/* when set, convert int/floats to strings */
469469
unsigned stringify:1;
470470

471+
/* bitmap for pdo_param_event(s) to skip in dispatch_param_event */
472+
unsigned skip_param_evt:7;
473+
471474
/* the sum of the number of bits here and the bit fields preceding should
472475
* equal 32 */
473-
unsigned _reserved_flags:21;
476+
unsigned _reserved_flags:14;
474477

475478
/* data source string used to open this handle */
476479
const char *data_source;

ext/pdo_mysql/mysql_driver.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
617617

618618
dbh->driver_data = H;
619619

620+
dbh->skip_param_evt =
621+
1 << PDO_PARAM_EVT_FREE |
622+
1 << PDO_PARAM_EVT_EXEC_POST |
623+
1 << PDO_PARAM_EVT_FETCH_PRE |
624+
1 << PDO_PARAM_EVT_FETCH_POST |
625+
1 << PDO_PARAM_EVT_NORMALIZE;
626+
620627
#ifndef PDO_USE_MYSQLND
621628
H->max_buffer_size = 1024*1024;
622629
#endif

ext/pdo_oci/oci_driver.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,11 @@ static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ *
674674
H = pecalloc(1, sizeof(*H), dbh->is_persistent);
675675
dbh->driver_data = H;
676676

677+
dbh->skip_param_evt =
678+
1 << PDO_PARAM_EVT_FETCH_PRE |
679+
1 << PDO_PARAM_EVT_FETCH_POST |
680+
1 << PDO_PARAM_EVT_NORMALIZE;
681+
677682
H->prefetch = PDO_OCI_PREFETCH_DEFAULT;
678683

679684
/* allocate an environment */

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,11 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
11981198
H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
11991199
dbh->driver_data = H;
12001200

1201+
dbh->skip_param_evt =
1202+
1 << PDO_PARAM_EVT_EXEC_POST |
1203+
1 << PDO_PARAM_EVT_FETCH_PRE |
1204+
1 << PDO_PARAM_EVT_FETCH_POST;
1205+
12011206
H->einfo.errcode = 0;
12021207
H->einfo.errmsg = NULL;
12031208

ext/pdo_pgsql/pgsql_statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *
399399
}
400400
break;
401401
}
402-
} else if (param->is_param) {
402+
} else if (param->is_param && event_type == PDO_PARAM_EVT_NORMALIZE) {
403403
/* We need to manually convert to a pg native boolean value */
404404
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
405405
((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,9 @@ static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{
797797
H->einfo.errmsg = NULL;
798798
dbh->driver_data = H;
799799

800+
/* skip all but this one param event */
801+
dbh->skip_param_evt = 0x7F ^ (1 << PDO_PARAM_EVT_EXEC_PRE);
802+
800803
filename = make_filename_safe(dbh->data_source);
801804

802805
if (!filename) {

0 commit comments

Comments
 (0)