SlideShare a Scribd company logo
13 Things
Every Developer
Should Know About
Their Database to
Run WordPress
Optimally
WordCamp Europe 2020
Otto Kekäläinen
@ottokekalainen
seravo.com
● Linux and open source advocate
● Written WP themes and plugins,
contributed to WordPress Core,
MySQL, MariaDB…
● CEO, sysadmin and developer at
Seravo.com – WordPress
hosting and upkeep
Otto Kekäläinen
Premium
Hosting and
Upkeep for
WordPress
The database is involved in both!
○ contains all your valuable data
○ often the bottleneck of performance
Common Issues in WordPress:
Security & Speed
DATABASE
THE SINGLE MOST
IMPORTANT PART OF
YOUR WORDPRESS
INFRASTRUCTURE
TIP #1: MAKE (USE OF) DB DUMPS
Benefits
● Copying your WordPress files is not enough for a
backup
● The database dump file format is
○ plain-text: view and modify it as much as you like
○ interoperable: import it into a MySQL or MariaDB
database server anywhere
Command line: mysqldump or wp db export
TIP #1: MAKE (USE OF) DB DUMPS
$ wp db export --skip-extended-insert --allow-root --single-transaction
example-after.sql
$ diff -u example-before.sql example-after.sql
--- example-before.sql 2018-08-30 10:58:23.243836204 +0300
+++ example-after.sql 2018-08-30 10:57:57.771762687 +0300
@@ -2,3 +2,4 @@
INSERT INTO `wp_terms` VALUES (70,'transients','transients',0,0);
INSERT INTO `wp_terms` VALUES (71,'code','code',0,0);
INSERT INTO `wp_terms` VALUES (72,'performance','performance',0,0);
+INSERT INTO `wp_terms` VALUES (73,'WordPress','wordpress',0,0);
What are these tables and columns? See
codex.wordpress.org/Database_Description
TIP #2: LEARN WP-CLI DB COMMANDS
List them all with wp db --help
My most used ones:
● wp db export/import
● wp db size --tables --all-tables --size_format=mb
● wp db cli
● wp db search --one_line
● wp search-replace --all-tables http://example.com
https://example.com
^ My favourite!
TIP #3: USE ADMINER TO BROWSE
● Adminer is one file, easy to install and update
● Less security vulnerabilities than in PHPMyAdmin
TIP #4: WP_OPTIONS AND AUTOLOAD
● Every single WordPress page load runs this query
SELECT * FROM wp_options WHERE autoload = 'yes'
TIP #4: WP_OPTIONS AND AUTOLOAD
● If wp_options table is larger than 1 MB, try to clean it up
● Find rows with the largest amount of data in the option_value
field:
○ SELECT option_name, length(option_value) FROM
wp_options WHERE autoload='yes' ORDER BY
length(option_value) DESC LIMIT 30
● File bugs against stupid plugins polluting your options table
● If cannot be cleaned, add an index on autoload
○ CREATE INDEX autoloadindex ON wp_options(autoload,
option_name)
TIP #5: WP_POSTMETA BLOAT
● Any site using custom post types or WooCommerce is likely to have a
big wp_postmeta table. While every post adds just one new row (and
many fields) to the database and keeping the number of column names
constant, the use of add_post_meta() calls will bloat the database with
tens or hundreds of rows per post where each row only contains two
fields: name and value.
● Find the meta_key naming patterns with most amount of rows:
○ SELECT substring(meta_key, 1, 20) AS
key_start, count(*) AS count FROM
wp_postmeta GROUP BY key_start
ORDER BY count DESC LIMIT 30
Unfortunately database bloat
and stupid use of database is
common in plugins.
We need to do more to raise
awareness about database
best practices!
TIP #6: LEARN SQL
● It is not enough that you know PHP, learn SQL as well.
● The database has 20 years of engineering on how to fetch a
small set of data from a huge set of data as quickly as
possible. Don’t try to reinvent that in PHP.
● Don’t put everything in wp_postmeta. Don’t be afraid of
creating your own custom tables that have the correct
columns, relations and indexes already defined.
● Learn what ‘index’ means and why you don’t want to be
causing ‘full table scans’ in the database.
TIP #7: BUT DON’T USE SQL DIRECTLY
● In WordPress PHP code use get_posts() for basics
● Use the WP_Query class for more advanced cases
● When WP_Query does not fit, use the $wpdb->get_row(),
$wpdb->insert() etc methods
● If you really need raw
SQL, don’t access the
database directly,
instead use
$wpdb->prepare() and
$wpdb->query() to
avoid SQL injection
vulnerabilities
TIP #8: CONFIGURE DATABASE SERVER
1. MariaDB preferred over Oracle MySQL
2. Use a recent version (MariaDB 10.3+)
3. Storage engine: InnoDB (not MyISAM)
4. Character set: UTFMB4 (for emojis )
5. Collation: your local sorting order A-Ö
6. ..and optimize all the other settings
Or hire a database administrator, or use a
WordPress hosting company that does this for you.
Reasons to
switch to
MariaDB
● MariaDB development is more open
and vibrant
● Quicker and more transparent
security releases
● More cutting edge features
● More storage engines
● Better performance for WP
● Galera active-active master
clustering
● Oracle stewardship is uncertain
● MariaDB has leapt in popularity
● Compatible and easy to migrate
https://seravo.fi/2015/10-reasons-to-migrate-t
o-mariadb-if-still-using-mysql
TIP #9: TRANSIENTS AND OBJECT CACHE
● Use Redis cache or similar
to store transients and
sessions so they can be
removed from wp_options
// Simple WP Transient API example
if ( ! $result = get_transient( ‘q_result’ ) ) {
$result = run_complex_and_slow_query();
set_transient( ‘q_result’, $result, 3600 );
}
echo $result;
● Redis and WP Transients
API will ease the load on
the database and help you
make super fast sites
TIP #10: MONITOR PERFORMANCE
● Take a peek with
SHOW PROCESSLIST;
● Analyse unusual slowness by
enabling mariadb-slow.log
logging
● Enable Tideways or similar
service for sampling your
WordPress site PHP execution in
production to find bottlenecks
AND ONE EXTRA TIP:
NEVER PUSH YOUR DEV
DATABASE INTO PRODUCTION
TIP #11: DB CLEANUP
● DELETE FROM `wp_posts` WHERE post_type =
'revision' AND post_date NOT LIKE '2018-%';
● DELETE FROM wp_options WHERE option_name
LIKE ('_transient_%') OR option_name LIKE
('_site_transient_%');
● Checkout what
wp-content/plugins/*/uninstall.php contains and
what plugins are supposed to clean away if they
are uninstalled
TIP #12: EXPLAIN, PLEASE
● You can append ‘EXPLAIN’ to any query and the
optimizer will tell how it is running the query
● EXPLAIN SELECT * FROM wp_options WHERE
autoload = 'yes';
MariaDB [wp_palvelu_06a4ad]> explain SELECT * FROM wp_options WHERE autoload = 'yes';
+------+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | wp_options | ALL | NULL | NULL | NULL | NULL | 415 | Using where |
+------+-------------+------------+------+---------------+------+---------+------+------+-------------+
TIP #13: TEST WITH DUMMY DATA
● While developing a site, load lots of dummy data into it so you
can test how your site looks and performs with 100, 1000 or 100
000 posts.
● Basic: Import themeunittestdata.wordpress.xml
○ codex.wordpress.org/Theme_Unit_Test
● More data: wp post generate
○ curl http://loripsum.net/api/5 |
wp post generate --post_content --count=10
● More realism: wp-cli-fixtures
○ github.com/nlemoine/wp-cli-fixtures
Thank you!
Seravo.com @Seravo @ottokekalainen
See also
Make Your Site Faster with Caching
https://seravo.com/blog/wordpress-cache/
300% faster WordPress load times with transients
https://seravo.com/blog/faster-wordpress-with-transients/
5 common reasons why your WordPress site is slow
https://youtu.be/8sJExUO-U4A
Improving WordPress Performance with XDebug and PHP Profiling
https://youtu.be/oKcIS5A-6_c

More Related Content

What's hot

Doing Things the WordPress Way
Doing Things the WordPress WayDoing Things the WordPress Way
Doing Things the WordPress Way
Matt Wiebe
 
20110606 e z_flow_gig_v1
20110606 e z_flow_gig_v120110606 e z_flow_gig_v1
20110606 e z_flow_gig_v1
Gilles Guirand
 
OpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr SearchingOpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr Searching
Alkacon Software GmbH & Co. KG
 
20100707 e z_rmll_gig_v1
20100707 e z_rmll_gig_v120100707 e z_rmll_gig_v1
20100707 e z_rmll_gig_v1
Gilles Guirand
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern Approach
Alessandro Fiore
 
Drupalxamppxp2 1231342958532404 1
Drupalxamppxp2 1231342958532404 1Drupalxamppxp2 1231342958532404 1
Drupalxamppxp2 1231342958532404 1
vijayraghav
 
Becoming A Drupal Master Builder
Becoming A Drupal Master BuilderBecoming A Drupal Master Builder
Becoming A Drupal Master Builder
Philip Norton
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
The wp config.php
The wp config.phpThe wp config.php
The wp config.php
Anthony Montalbano
 
Creating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without SwearingCreating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without Swearingmartinwolak
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
Tareq Hasan
 
20100622 e z_find_slides_gig_v2.1
20100622 e z_find_slides_gig_v2.120100622 e z_find_slides_gig_v2.1
20100622 e z_find_slides_gig_v2.1Gilles Guirand
 
Saving Time with WP-CLI
Saving Time with WP-CLISaving Time with WP-CLI
Saving Time with WP-CLI
Taylor Lovett
 
Drupal 8 Configuration Management with Features
Drupal 8 Configuration Management with FeaturesDrupal 8 Configuration Management with Features
Drupal 8 Configuration Management with Features
Nuvole
 
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
slobodanmanic
 
Drupal Installation & Configuration
Drupal Installation & ConfigurationDrupal Installation & Configuration
Drupal Installation & Configuration
Anil Mishra
 
Introduction to Cakephp
Introduction to CakephpIntroduction to Cakephp
Introduction to CakephpAditya Mooley
 
Drupal Theme Development
Drupal Theme DevelopmentDrupal Theme Development
Drupal Theme Development
Web Development Montreal
 

What's hot (20)

Doing Things the WordPress Way
Doing Things the WordPress WayDoing Things the WordPress Way
Doing Things the WordPress Way
 
20110606 e z_flow_gig_v1
20110606 e z_flow_gig_v120110606 e z_flow_gig_v1
20110606 e z_flow_gig_v1
 
Php admin affi
Php admin affiPhp admin affi
Php admin affi
 
OpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr SearchingOpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr Searching
 
20100707 e z_rmll_gig_v1
20100707 e z_rmll_gig_v120100707 e z_rmll_gig_v1
20100707 e z_rmll_gig_v1
 
Wordpress development: A Modern Approach
Wordpress development:  A Modern ApproachWordpress development:  A Modern Approach
Wordpress development: A Modern Approach
 
Drupalxamppxp2 1231342958532404 1
Drupalxamppxp2 1231342958532404 1Drupalxamppxp2 1231342958532404 1
Drupalxamppxp2 1231342958532404 1
 
Becoming A Drupal Master Builder
Becoming A Drupal Master BuilderBecoming A Drupal Master Builder
Becoming A Drupal Master Builder
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
The wp config.php
The wp config.phpThe wp config.php
The wp config.php
 
Creating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without SwearingCreating WordPress Theme Faster, Smarter & Without Swearing
Creating WordPress Theme Faster, Smarter & Without Swearing
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
20100622 e z_find_slides_gig_v2.1
20100622 e z_find_slides_gig_v2.120100622 e z_find_slides_gig_v2.1
20100622 e z_find_slides_gig_v2.1
 
skintutorial
skintutorialskintutorial
skintutorial
 
Saving Time with WP-CLI
Saving Time with WP-CLISaving Time with WP-CLI
Saving Time with WP-CLI
 
Drupal 8 Configuration Management with Features
Drupal 8 Configuration Management with FeaturesDrupal 8 Configuration Management with Features
Drupal 8 Configuration Management with Features
 
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14WordPress Theme and Plugin Optimization - WordPress Arvika March '14
WordPress Theme and Plugin Optimization - WordPress Arvika March '14
 
Drupal Installation & Configuration
Drupal Installation & ConfigurationDrupal Installation & Configuration
Drupal Installation & Configuration
 
Introduction to Cakephp
Introduction to CakephpIntroduction to Cakephp
Introduction to Cakephp
 
Drupal Theme Development
Drupal Theme DevelopmentDrupal Theme Development
Drupal Theme Development
 

Similar to 13 things every developer should know about their database to run word press optimally

10 things every developer should know about their database to run word press ...
10 things every developer should know about their database to run word press ...10 things every developer should know about their database to run word press ...
10 things every developer should know about their database to run word press ...
Otto Kekäläinen
 
Optimize the obvious
Optimize the obviousOptimize the obvious
Optimize the obvious
drhenner
 
Performance and Scalability
Performance and ScalabilityPerformance and Scalability
Performance and Scalability
Mediacurrent
 
PHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHPPHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHP
Vibrant Technologies & Computers
 
php databse handling
php databse handlingphp databse handling
php databse handling
kunj desai
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
Taylor Lovett
 
Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress Applications
Taylor Lovett
 
MySQL Scaling Presentation
MySQL Scaling PresentationMySQL Scaling Presentation
MySQL Scaling PresentationTommy Falgout
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
aadi Surve
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin development
arryaas
 
Handling Database Deployments
Handling Database DeploymentsHandling Database Deployments
Handling Database Deployments
Mike Willbanks
 
My SQL 101
My SQL 101My SQL 101
My SQL 101
Dave Stokes
 
MySQL crash course by moshe kaplan
MySQL crash course by moshe kaplanMySQL crash course by moshe kaplan
MySQL crash course by moshe kaplan
Moshe Kaplan
 
Using Mysql.pptx
Using Mysql.pptxUsing Mysql.pptx
Using Mysql.pptx
StephenEfange3
 
Developers, Be a Bada$$ with WP-CLI
Developers, Be a Bada$$ with WP-CLIDevelopers, Be a Bada$$ with WP-CLI
Developers, Be a Bada$$ with WP-CLI
WP Engine
 
BITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and InstallationBITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and Installation
BITS
 
Here Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPressHere Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPress
Rami Sayar
 
Word press interview question and answer tops technologies
Word press interview question and answer   tops technologiesWord press interview question and answer   tops technologies
Word press interview question and answer tops technologies
TOPS Technologies
 

Similar to 13 things every developer should know about their database to run word press optimally (20)

10 things every developer should know about their database to run word press ...
10 things every developer should know about their database to run word press ...10 things every developer should know about their database to run word press ...
10 things every developer should know about their database to run word press ...
 
Optimize the obvious
Optimize the obviousOptimize the obvious
Optimize the obvious
 
Performance and Scalability
Performance and ScalabilityPerformance and Scalability
Performance and Scalability
 
PHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHPPHP - Intriduction to MySQL And PHP
PHP - Intriduction to MySQL And PHP
 
php databse handling
php databse handlingphp databse handling
php databse handling
 
Mysql ppt
Mysql pptMysql ppt
Mysql ppt
 
Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
 
Download It
Download ItDownload It
Download It
 
Best Practices for Building WordPress Applications
Best Practices for Building WordPress ApplicationsBest Practices for Building WordPress Applications
Best Practices for Building WordPress Applications
 
MySQL Scaling Presentation
MySQL Scaling PresentationMySQL Scaling Presentation
MySQL Scaling Presentation
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
WordPress plugin development
WordPress plugin developmentWordPress plugin development
WordPress plugin development
 
Handling Database Deployments
Handling Database DeploymentsHandling Database Deployments
Handling Database Deployments
 
My SQL 101
My SQL 101My SQL 101
My SQL 101
 
MySQL crash course by moshe kaplan
MySQL crash course by moshe kaplanMySQL crash course by moshe kaplan
MySQL crash course by moshe kaplan
 
Using Mysql.pptx
Using Mysql.pptxUsing Mysql.pptx
Using Mysql.pptx
 
Developers, Be a Bada$$ with WP-CLI
Developers, Be a Bada$$ with WP-CLIDevelopers, Be a Bada$$ with WP-CLI
Developers, Be a Bada$$ with WP-CLI
 
BITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and InstallationBITS: Introduction to MySQL - Introduction and Installation
BITS: Introduction to MySQL - Introduction and Installation
 
Here Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPressHere Be Dragons - Debugging WordPress
Here Be Dragons - Debugging WordPress
 
Word press interview question and answer tops technologies
Word press interview question and answer   tops technologiesWord press interview question and answer   tops technologies
Word press interview question and answer tops technologies
 

More from Seravo

Use Xdebug to profile PHP
Use Xdebug to profile PHPUse Xdebug to profile PHP
Use Xdebug to profile PHP
Seravo
 
Optimizing WordPress PHP performance with Tideways
Optimizing WordPress PHP performance with TidewaysOptimizing WordPress PHP performance with Tideways
Optimizing WordPress PHP performance with Tideways
Seravo
 
Mindre och snabbare – Cache tips for WordPress developers
Mindre och snabbare – Cache tips for WordPress developersMindre och snabbare – Cache tips for WordPress developers
Mindre och snabbare – Cache tips for WordPress developers
Seravo
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developers
Seravo
 
Vähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjille
Vähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjilleVähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjille
Vähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjille
Seravo
 
Improving WordPress Performance: Xdebug and PHP profiling
Improving WordPress Performance: Xdebug and PHP profilingImproving WordPress Performance: Xdebug and PHP profiling
Improving WordPress Performance: Xdebug and PHP profiling
Seravo
 
Seravo.com: WordPress Security 101
Seravo.com: WordPress Security 101Seravo.com: WordPress Security 101
Seravo.com: WordPress Security 101
Seravo
 

More from Seravo (7)

Use Xdebug to profile PHP
Use Xdebug to profile PHPUse Xdebug to profile PHP
Use Xdebug to profile PHP
 
Optimizing WordPress PHP performance with Tideways
Optimizing WordPress PHP performance with TidewaysOptimizing WordPress PHP performance with Tideways
Optimizing WordPress PHP performance with Tideways
 
Mindre och snabbare – Cache tips for WordPress developers
Mindre och snabbare – Cache tips for WordPress developersMindre och snabbare – Cache tips for WordPress developers
Mindre och snabbare – Cache tips for WordPress developers
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developers
 
Vähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjille
Vähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjilleVähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjille
Vähemmän ja nopeammin – Välimuistivinkit WordPress-kehittäjille
 
Improving WordPress Performance: Xdebug and PHP profiling
Improving WordPress Performance: Xdebug and PHP profilingImproving WordPress Performance: Xdebug and PHP profiling
Improving WordPress Performance: Xdebug and PHP profiling
 
Seravo.com: WordPress Security 101
Seravo.com: WordPress Security 101Seravo.com: WordPress Security 101
Seravo.com: WordPress Security 101
 

Recently uploaded

一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
Javier Lasa
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 

Recently uploaded (20)

一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 

13 things every developer should know about their database to run word press optimally

  • 1. 13 Things Every Developer Should Know About Their Database to Run WordPress Optimally WordCamp Europe 2020 Otto Kekäläinen @ottokekalainen seravo.com
  • 2. ● Linux and open source advocate ● Written WP themes and plugins, contributed to WordPress Core, MySQL, MariaDB… ● CEO, sysadmin and developer at Seravo.com – WordPress hosting and upkeep Otto Kekäläinen
  • 4. The database is involved in both! ○ contains all your valuable data ○ often the bottleneck of performance Common Issues in WordPress: Security & Speed
  • 5. DATABASE THE SINGLE MOST IMPORTANT PART OF YOUR WORDPRESS INFRASTRUCTURE
  • 6. TIP #1: MAKE (USE OF) DB DUMPS Benefits ● Copying your WordPress files is not enough for a backup ● The database dump file format is ○ plain-text: view and modify it as much as you like ○ interoperable: import it into a MySQL or MariaDB database server anywhere Command line: mysqldump or wp db export
  • 7. TIP #1: MAKE (USE OF) DB DUMPS $ wp db export --skip-extended-insert --allow-root --single-transaction example-after.sql $ diff -u example-before.sql example-after.sql --- example-before.sql 2018-08-30 10:58:23.243836204 +0300 +++ example-after.sql 2018-08-30 10:57:57.771762687 +0300 @@ -2,3 +2,4 @@ INSERT INTO `wp_terms` VALUES (70,'transients','transients',0,0); INSERT INTO `wp_terms` VALUES (71,'code','code',0,0); INSERT INTO `wp_terms` VALUES (72,'performance','performance',0,0); +INSERT INTO `wp_terms` VALUES (73,'WordPress','wordpress',0,0); What are these tables and columns? See codex.wordpress.org/Database_Description
  • 8. TIP #2: LEARN WP-CLI DB COMMANDS List them all with wp db --help My most used ones: ● wp db export/import ● wp db size --tables --all-tables --size_format=mb ● wp db cli ● wp db search --one_line ● wp search-replace --all-tables http://example.com https://example.com ^ My favourite!
  • 9. TIP #3: USE ADMINER TO BROWSE ● Adminer is one file, easy to install and update ● Less security vulnerabilities than in PHPMyAdmin
  • 10. TIP #4: WP_OPTIONS AND AUTOLOAD ● Every single WordPress page load runs this query SELECT * FROM wp_options WHERE autoload = 'yes'
  • 11. TIP #4: WP_OPTIONS AND AUTOLOAD ● If wp_options table is larger than 1 MB, try to clean it up ● Find rows with the largest amount of data in the option_value field: ○ SELECT option_name, length(option_value) FROM wp_options WHERE autoload='yes' ORDER BY length(option_value) DESC LIMIT 30 ● File bugs against stupid plugins polluting your options table ● If cannot be cleaned, add an index on autoload ○ CREATE INDEX autoloadindex ON wp_options(autoload, option_name)
  • 12. TIP #5: WP_POSTMETA BLOAT ● Any site using custom post types or WooCommerce is likely to have a big wp_postmeta table. While every post adds just one new row (and many fields) to the database and keeping the number of column names constant, the use of add_post_meta() calls will bloat the database with tens or hundreds of rows per post where each row only contains two fields: name and value. ● Find the meta_key naming patterns with most amount of rows: ○ SELECT substring(meta_key, 1, 20) AS key_start, count(*) AS count FROM wp_postmeta GROUP BY key_start ORDER BY count DESC LIMIT 30
  • 13. Unfortunately database bloat and stupid use of database is common in plugins. We need to do more to raise awareness about database best practices!
  • 14. TIP #6: LEARN SQL ● It is not enough that you know PHP, learn SQL as well. ● The database has 20 years of engineering on how to fetch a small set of data from a huge set of data as quickly as possible. Don’t try to reinvent that in PHP. ● Don’t put everything in wp_postmeta. Don’t be afraid of creating your own custom tables that have the correct columns, relations and indexes already defined. ● Learn what ‘index’ means and why you don’t want to be causing ‘full table scans’ in the database.
  • 15. TIP #7: BUT DON’T USE SQL DIRECTLY ● In WordPress PHP code use get_posts() for basics ● Use the WP_Query class for more advanced cases ● When WP_Query does not fit, use the $wpdb->get_row(), $wpdb->insert() etc methods ● If you really need raw SQL, don’t access the database directly, instead use $wpdb->prepare() and $wpdb->query() to avoid SQL injection vulnerabilities
  • 16. TIP #8: CONFIGURE DATABASE SERVER 1. MariaDB preferred over Oracle MySQL 2. Use a recent version (MariaDB 10.3+) 3. Storage engine: InnoDB (not MyISAM) 4. Character set: UTFMB4 (for emojis ) 5. Collation: your local sorting order A-Ö 6. ..and optimize all the other settings Or hire a database administrator, or use a WordPress hosting company that does this for you.
  • 17. Reasons to switch to MariaDB ● MariaDB development is more open and vibrant ● Quicker and more transparent security releases ● More cutting edge features ● More storage engines ● Better performance for WP ● Galera active-active master clustering ● Oracle stewardship is uncertain ● MariaDB has leapt in popularity ● Compatible and easy to migrate https://seravo.fi/2015/10-reasons-to-migrate-t o-mariadb-if-still-using-mysql
  • 18. TIP #9: TRANSIENTS AND OBJECT CACHE ● Use Redis cache or similar to store transients and sessions so they can be removed from wp_options // Simple WP Transient API example if ( ! $result = get_transient( ‘q_result’ ) ) { $result = run_complex_and_slow_query(); set_transient( ‘q_result’, $result, 3600 ); } echo $result; ● Redis and WP Transients API will ease the load on the database and help you make super fast sites
  • 19. TIP #10: MONITOR PERFORMANCE ● Take a peek with SHOW PROCESSLIST; ● Analyse unusual slowness by enabling mariadb-slow.log logging ● Enable Tideways or similar service for sampling your WordPress site PHP execution in production to find bottlenecks
  • 20. AND ONE EXTRA TIP: NEVER PUSH YOUR DEV DATABASE INTO PRODUCTION
  • 21. TIP #11: DB CLEANUP ● DELETE FROM `wp_posts` WHERE post_type = 'revision' AND post_date NOT LIKE '2018-%'; ● DELETE FROM wp_options WHERE option_name LIKE ('_transient_%') OR option_name LIKE ('_site_transient_%'); ● Checkout what wp-content/plugins/*/uninstall.php contains and what plugins are supposed to clean away if they are uninstalled
  • 22. TIP #12: EXPLAIN, PLEASE ● You can append ‘EXPLAIN’ to any query and the optimizer will tell how it is running the query ● EXPLAIN SELECT * FROM wp_options WHERE autoload = 'yes'; MariaDB [wp_palvelu_06a4ad]> explain SELECT * FROM wp_options WHERE autoload = 'yes'; +------+-------------+------------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+------------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | wp_options | ALL | NULL | NULL | NULL | NULL | 415 | Using where | +------+-------------+------------+------+---------------+------+---------+------+------+-------------+
  • 23. TIP #13: TEST WITH DUMMY DATA ● While developing a site, load lots of dummy data into it so you can test how your site looks and performs with 100, 1000 or 100 000 posts. ● Basic: Import themeunittestdata.wordpress.xml ○ codex.wordpress.org/Theme_Unit_Test ● More data: wp post generate ○ curl http://loripsum.net/api/5 | wp post generate --post_content --count=10 ● More realism: wp-cli-fixtures ○ github.com/nlemoine/wp-cli-fixtures
  • 24. Thank you! Seravo.com @Seravo @ottokekalainen
  • 25. See also Make Your Site Faster with Caching https://seravo.com/blog/wordpress-cache/ 300% faster WordPress load times with transients https://seravo.com/blog/faster-wordpress-with-transients/ 5 common reasons why your WordPress site is slow https://youtu.be/8sJExUO-U4A Improving WordPress Performance with XDebug and PHP Profiling https://youtu.be/oKcIS5A-6_c