Make WordPress Core

Opened 13 months ago

Last modified 9 months ago

#58344 new enhancement

Connect to database on first query

Reported by: spacedmonkey's profile spacedmonkey Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Database Keywords: has-patch needs-testing-info
Focuses: performance Cc:


Currently there is the database connection is made in __construct method by calling db_connect method. However with sites with persistent object caching enabled, this connection to the database may not be needed. It is common now for requests to have no database queries at all if persistent object caching is setup.

Moving the db_connect call to the query method like this.

        public function query( $query ) {
                if ( ! $this->has_connected ) {
                if ( ! $this->ready ) {
                        $this->check_current_query = true;
                        return false;

Attachments (1)

58344.patch (689 bytes) - added by elrae 10 months ago.

Download all attachments as: .zip

Change History (10)

#1 @spacedmonkey
13 months ago

Would love the thoughts of @johnjamesjacoby @johnbillion on this one.

#2 @johnbillion
13 months ago

  • Version 0.71 deleted

I pointed this out 11 years ago in #19372 but never followed up :D

If it's simple enough to achieve and has a performance benefit then I'm in favour.

#3 @dd32
13 months ago

One edge-case to consider is that currently WordPress will exit with a Site Unavailable type error when the DB is unavailable, with this potential change if the query isn't made until the Footer or mid-post-render the page may partially render before hitting a wp_die() condition (As you don't really want to return empty result-sets in the event of a DB-down scenario).

However, plugins like HyperDB and others do delay the DB connect until a query is made, so this might not be an actual issue that needs consideration.

That being said, If a persistent object cache is not in play, delaying until the first query will have very little change for most sites and scenario's.

#4 @spacedmonkey
11 months ago

  • Milestone changed from Awaiting Review to 6.4

10 months ago

#5 @elrae
10 months ago

Added a patch here based on the approach outlined in the ticket description

#6 @oglekler
10 months ago

  • Keywords has-patch needs-testing-info added

#7 @spacedmonkey
9 months ago

@dd32 @johnbillion Any recommendation on unit tests for this one?

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.

9 months ago

#9 @mukesh27
9 months ago

  • Milestone changed from 6.4 to Future Release

This ticket was discussed during the Performance Bug Scrub meeting.

The patch requires both unit testing and manual testing. Therefore, we have decided to punt it to the next milestone. Please feel free to reassign it to the current milestone if it is ready for merging. Thank you.

Additional props to @joemcgill @spacedmonkey

Note: See TracTickets for help on using tickets.