wppaste
WordPress

wp_get_db_schema( string $scope = 'all', int $blog_id = null ): string

Since
3.3.0
Source
wp-admin/includes/schema.php:36
Retrieve the SQL for creating database tables.

Compatibility

WordPress
since 3.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$scopestringoptional
The tables for which to retrieve SQL. Can be all, global, ms_global, or blog tables. Defaults to all.Default: 'all'
$blog_idintoptional
The site ID for which to retrieve SQL. Default is the current site ID.Default: null

Return value

string
The SQL needed to create the requested tables.

Performance profile

How much work a call to wp_get_db_schema() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
137–162

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 171.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_db_schema() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($old_blog_id)137–155->get_charset_collate(), is_multisite()
isset($old_blog_id)140–158->get_charset_collate(), is_multisite(), ->set_blog_id()
!isset($old_blog_id)145–159->get_charset_collate(), ->set_blog_id(), is_multisite()
isset($old_blog_id)148–162->get_charset_collate(), ->set_blog_id(), is_multisite(), ->set_blog_id()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 171 instructions, 137–162 executed per call, 12 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

Used by · 2

Source code

<?phpfunction wp_get_db_schema( $scope = 'all', $blog_id = null ) {	global $wpdb; 	$charset_collate = $wpdb->get_charset_collate(); 	if ( $blog_id && (int) $blog_id !== $wpdb->blogid ) {		$old_blog_id = $wpdb->set_blog_id( $blog_id );	} 	// Engage multisite if in the middle of turning it on from network.php.	$is_multisite = is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ); 	/*	 * Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that.	 * As of 4.2, however, we moved to utf8mb4, which uses 4 bytes per character. This means that an index which	 * used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters.	 */	$max_index_length = 191; 	// Blog-specific tables.	$blog_tables = "CREATE TABLE $wpdb->termmeta (	meta_id bigint(20) unsigned NOT NULL auto_increment,	term_id bigint(20) unsigned NOT NULL default '0',	meta_key varchar(255) default NULL,	meta_value longtext,	PRIMARY KEY  (meta_id),	KEY term_id (term_id),	KEY meta_key (meta_key($max_index_length))) $charset_collate;CREATE TABLE $wpdb->terms ( term_id bigint(20) unsigned NOT NULL auto_increment, name varchar(200) NOT NULL default '', slug varchar(200) NOT NULL default '', term_group bigint(10) NOT NULL default 0, PRIMARY KEY  (term_id), KEY slug (slug($max_index_length)), KEY name (name($max_index_length))) $charset_collate;CREATE TABLE $wpdb->term_taxonomy ( term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default 0, taxonomy varchar(32) NOT NULL default '', description longtext NOT NULL, parent bigint(20) unsigned NOT NULL default 0, count bigint(20) NOT NULL default 0, PRIMARY KEY  (term_taxonomy_id), UNIQUE KEY term_id_taxonomy (term_id,taxonomy), KEY taxonomy (taxonomy)) $charset_collate;CREATE TABLE $wpdb->term_relationships ( object_id bigint(20) unsigned NOT NULL default 0, term_taxonomy_id bigint(20) unsigned NOT NULL default 0, term_order int(11) NOT NULL default 0, PRIMARY KEY  (object_id,term_taxonomy_id), KEY term_taxonomy_id (term_taxonomy_id)) $charset_collate;CREATE TABLE $wpdb->commentmeta (	meta_id bigint(20) unsigned NOT NULL auto_increment,	comment_id bigint(20) unsigned NOT NULL default '0',	meta_key varchar(255) default NULL,	meta_value longtext,	PRIMARY KEY  (meta_id),	KEY comment_id (comment_id),	KEY meta_key (meta_key($max_index_length))) $charset_collate;CREATE TABLE $wpdb->comments (	comment_ID bigint(20) unsigned NOT NULL auto_increment,	comment_post_ID bigint(20) unsigned NOT NULL default '0',	comment_author tinytext NOT NULL,	comment_author_email varchar(100) NOT NULL default '',	comment_author_url varchar(200) NOT NULL default '',	comment_author_IP varchar(100) NOT NULL default '',	comment_date datetime NOT NULL default '0000-00-00 00:00:00',	comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',	comment_content text NOT NULL,	comment_karma int(11) NOT NULL default '0',	comment_approved varchar(20) NOT NULL default '1',	comment_agent varchar(255) NOT NULL default '',	comment_type varchar(20) NOT NULL default 'comment',	comment_parent bigint(20) unsigned NOT NULL default '0',

Changelog

Introduced in 3.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-admin/includes/schema.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.