#20838 closed enhancement (fixed)
Don't populate $col_info in production
| Reported by: | GrahamKelly | Owned by: | nacin |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.5 |
| Component: | Database | Version: | 3.3.2 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: |
Description
While profiling WordPress 3.3.2 I noticed that mysql_fetch_field() seemed to be fairly expensive. Upon looking into this I found that the col_info property is being set in wpdb but it does not appear (from some grepping through WP Core and quite a few extensions) that anyone is actually making use of the results (Either from directly accessing $col_info or from calls to get_col_info). In order to improve performance I wrapped the calls in 'if (WP_DEBUG)' which seems to do the trick.
Patch attached.
Attachments (3)
Change History (16)
#2
@
14 years ago
- Keywords needs-patch added; has-patch removed
- Milestone Awaiting Review → 3.5
This could be an easy lazy-loading win.
We could use a magic getter to handle someone accessing col_info directly, then make get_col_info() act on $this->result directly. This would prevent mysql_fetch_field() from firing unless someone needs it.
#4
@
14 years ago
- Keywords has-patch added; needs-patch removed
New patch uses a magic getter to only load column info when requested.
The only downside of this is that we can't do the mysql_free_result( $this->result ) in wpdb::query(), so I've moved it to wpdb::flush().
#5
@
14 years ago
Patch 2 changes the access visibility of wpdb::col_info and wpdb::get_col_info() to protected - they may need to be over-ridden by replacements for wpdb.
#7
follow-up:
↓ 8
@
14 years ago
- Cc added
The committed code - r21472 - includes error suppression:
@mysql_free_result( $this->result );
The PHP docs for mysql_free_result indicate that this is a problem only when a non-resource is provided:
If a non-resource is used for the result, an error of level E_WARNING will be emitted. It's worth noting that mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.
Can we get a way with not having the @ with something like:
if ( is_resource( $this->result ) ) {
mysql_free_result( $this->result );
}
?
It would be worth while to avoid the use of @ when ever possible.
#8
in reply to: ↑ 7
;
follow-up:
↓ 9
@
14 years ago
Replying to josephscott:
It would be worth while to avoid the use of @ when ever possible.
We discussed this as well. I agree this is a worthwhile change. New ticket?
#12
follow-up:
↓ 13
@
13 years ago
Could someone update the $wpdb documentation at Codex to reflect this change? I don't think I'm competent to do so. But I can see that get_col isn't a lot of use anymore, is it?
#13
in reply to: ↑ 12
@
13 years ago
Replying to jeffmcmahan:
Could someone update the $wpdb documentation at Codex to reflect this change? I don't think I'm competent to do so. But I can see that get_col isn't a lot of use anymore, is it?
There's no public API change here. We simply now load column information on demand, rather than with each query.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Out of curiosity, is your profiling data available? While it probably is a good idea to only fetch this while in debug mode (or maybe while SAVEQUERIES is true) I have previously profiled
mysql_fetch_fieldand found the performance impact is negligable.