Deprecated: version_compare(): Passing null to parameter #2 ($version2) of type string is deprecated in /var/www/html/wpxss.com/application/wp-content/plugins/tweet-old-post-pro/vendor/codeinwp/themeisle-sdk/load.php on line 24

Deprecated: version_compare(): Passing null to parameter #2 ($version2) of type string is deprecated in /var/www/html/wpxss.com/application/wp-content/plugins/tweet-old-post-pro/vendor/codeinwp/themeisle-sdk/load.php on line 29

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the pb-seo-friendly-images domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/wpxss.com/application/wp-includes/functions.php on line 6260

Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /var/www/html/wpxss.com/application/wp-content/plugins/wp_glossary/includes/class-wpg-linkify.php on line 316

Warning: Only the first byte will be assigned to the string offset in /var/www/html/wpxss.com/application/wp-content/plugins/wp_glossary/includes/class-wpg-linkify.php on line 387
Adding high-performance keys to MySQL tables to speed up WordPress πŸ§‘πŸΏβ€πŸš€ - wpXSS
Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /var/www/html/wpxss.com/application/wp-content/plugins/wp_glossary/includes/class-wpg-linkify.php on line 316

Warning: Only the first byte will be assigned to the string offset in /var/www/html/wpxss.com/application/wp-content/plugins/wp_glossary/includes/class-wpg-linkify.php on line 387

Deprecated: Function wp_get_loading_attr_default is deprecated since version 6.3.0! Use wp_get_loading_optimization_attributes() instead. in /var/www/html/wpxss.com/application/wp-includes/functions.php on line 6260
Database Β» Adding high-performance keys to MySQL tables to speed up WordPress πŸ§‘πŸΏβ€πŸš€

Adding high-performance keys to MySQL tables to speed up WordPress πŸ§‘πŸΏβ€πŸš€


Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /var/www/html/wpxss.com/application/wp-content/plugins/wp_glossary/includes/class-wpg-linkify.php on line 316

Warning: Only the first byte will be assigned to the string offset in /var/www/html/wpxss.com/application/wp-content/plugins/wp_glossary/includes/class-wpg-linkify.php on line 387

wp_postmeta table should be the biggest table in your database, if not, there is a problem. Read wp_actionscheduler_actions & wp_actionscheduler_logs tables

When you install WordPress for the first time, tables are created in the database, for example for the wp_postmeta table:

CREATE TABLE wp_postmeta (
      meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      post_id bigint(20) unsigned NOT NULL DEFAULT '0',
      meta_key varchar(255) DEFAULT NULL,
      meta_value longtext,
      PRIMARY KEY (meta_id),
      KEY post_id (post_id),
      KEY meta_key (meta_key)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

the problem?

  • AUTO_INCREMENT slows down most queries, because of having to look in the secondary index to find auto_inc ID, then looking in data for the actual ID you need.
  • BIGINT and AUTO_INCREMENT are overkill
  • meta_key or meta_value will never be NULL 😀

solution:

CREATE TABLE wp_postmeta (
        post_id BIGINT UNSIGNED NOT NULL,
        meta_key VARCHAR(255) NOT NULL,
        meta_value LONGTEXT NOT NULL,
        PRIMARY KEY(post_id, meta_key),
        INDEX(meta_key)
        ) ENGINE=InnoDB;

On an existing tables, We can add high-performance keys that match the most common WordPress queries:


ALTER TABLE wp_postmeta ADD UNIQUE KEY meta_id (meta_id);
ALTER TABLE wp_postmeta DROP PRIMARY KEY;
ALTER TABLE wp_postmeta ADD PRIMARY KEY (post_id, meta_key, meta_id);
ALTER TABLE wp_postmeta DROP KEY post_id;
ALTER TABLE wp_postmeta DROP KEY meta_key;
ALTER TABLE wp_postmeta ADD KEY meta_key (meta_key, post_id);

This will create new high-performance keys:

TableStandard WP keysNew keys
wp_postmetameta_id: primary key
post_id
meta_key
post_id, meta_key, meta_id: primary key
meta_id: unique key
meta_key, post_id
See also  What is ℹ️ Data breach (information disclosure) and How to prevent WordPress information disclosure

We can do the same for other major tables:


ALTER TABLE wp_posts DROP KEY type_status_date;
ALTER TABLE wp_posts ADD KEY type_status_date (post_type, post_status, post_date, post_author, ID);
ALTER TABLE wp_posts DROP KEY post_author;
ALTER TABLE wp_posts ADD KEY post_author (post_author, post_type, post_status, post_date, ID);
TableStandard WP keysNew keys
wp_postspost_type, post_status, post_date,ID
post_author
post_type,post_status,post_date,post_author,ID
post_author,post_type,post_status,post_date,ID

ALTER TABLE wp_options ADD UNIQUE KEY option_id (option_id);
ALTER TABLE wp_options DROP PRIMARY KEY;
ALTER TABLE wp_options ADD PRIMARY KEY (autoload, option_id);
ALTER TABLE wp_options DROP KEY autoload;
TableStandard WP keysNew keys
wp_optionsoption_id: primary key
option_name: unique key
autoload
autoload, option_id: primary key
option_name: unique_key
option_id: unique key

ALTER TABLE wp_usermeta ADD UNIQUE KEY umeta_id (umeta_id);
ALTER TABLE wp_usermeta DROP PRIMARY KEY;
ALTER TABLE wp_usermeta ADD PRIMARY KEY (user_id, meta_key, umeta_id);
ALTER TABLE wp_usermeta DROP KEY user_id;
ALTER TABLE wp_usermeta DROP KEY meta_key;
ALTER TABLE wp_usermeta ADD KEY meta_key (meta_key, user_id);
TableStandard WP keysNew keys
wp_usermetaumeta_id: primary key
user_id
meta_key
user_id, meta_key, umeta_id: primary key
umeta_id: unique key
meta_key, user_id

ALTER TABLE wp_termmeta ADD UNIQUE KEY meta_id (meta_id);
ALTER TABLE wp_termmeta DROP PRIMARY KEY;
ALTER TABLE wp_termmeta ADD PRIMARY KEY (term_id, meta_key, meta_id);
ALTER TABLE wp_termmeta DROP KEY term_id;
ALTER TABLE wp_termmeta DROP KEY meta_key;
ALTER TABLE wp_termmeta ADD KEY meta_key (meta_key, term_id);
TableStandard WP keysNew keys
wp_termmeta
meta_id: primary key
term_id
meta_key
term_id, meta_key, meta_id: primary key
meta_id: unique key
meta_key, term_id

The schema changes mentioned here are automated in Index WP MySQL For Speed WordPress plugin.


Source: MySQL Documents by Rick James
Speeding up WordPress database operations by Oliver Jones

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended