When developing a WordPress plugin it's always a good idea to consider the site's performance.
Many plugin developers store default config information or empty values in the database (options or post meta) which could easily be stored as a hard-coded value within the plugin.
Empty, 0 value, or null values are generally useless.
If there are any database experts. Please share your thoughts on empty, 0 and null values.
One of our recent clients has a WordPress site with 520,000 + rows in the post meta table!
Let’s say a site has 1,000 posts which could be any mix of pages, posts, products or any WP custom post type. Then a plugin stores 5 default values that would add an additional 5,000 rows to the post meta database table. What if another plugin uses 10 more default values?
That’s 15,000 extra rows. it just adds up!
The default values could be easily read from a php constant if the retrieved value is empty.
What is the solution?
In some of Orbisius WordPress plugins we intentionally delete the option if its value is zero or an empty string.
WordPress has delete_option() function. Actually, We do have a generic setOption(key, value) method which checks and value that needs to be saved and determines which function to call update_option() or delete_option(). No need use add_option.
If the option does not exist, update_option() will create it.
In some plugins we generate a JSON file that has the plugin’s config values so the plugin doesn’t have to load its configuration unless really necessary.
Do you have lots of empty values in the post meta table?
The following query will show you how many rows to have with empty values.
You can paste the following query in the PHP admin custom query box or whatever database manager you’re using.
Do make sure you enter the correct WordPress database prefix which is set during the installation and can be found in the wp-config. php file. It defaults to wp_
SELECT * FROM `wp_postmeta` WHERE meta_value='' OR meta_value=NULL OR meta_value='0';
How to remove empty empty, null or fields with value=0
Warning: backup your site!
Warning 2 running this command will delete values and that may affect plugins, themes or crash the site if the plugins are not written well. Use it at your own risk.
For this reason we highly recommend that you run these commands on WordPress Staging Site such as qSandbox.com or WPSandbox.net
Delete FROM `wp_postmeta` where meta_value='' OR meta_value=NULL OR meta_value='0';