There’s lots you can do to improve database (DB) performance, but each change comes with risks. Always thoroughly test changes in a development environment first before making permanent database changes in production.
Growth in data requires a growth in hosting to handle the scale of database size and queries.
There’s only so much you can clean-up practically. When you reach this point, hosting with solid managed hosting like Pantheon.io or custom cloud infrastructure like AWS can offer a great deal of scale, costing on the magnitude of $5k/mo in huge DB servers if it becomes too important to have all the historic data while keeping things fast. Those costs are out of reach for most of us, so minding your DB health is critical as you grow so you don’t have to spend huge on hosting before it’s truly necessary to do so.
There may even come a point where it makes sense to shard the data (i.e. move historic quarters or years data over to backup environments for archival usage).
The best solution will depend on each client’s unique needs.
Here’s my practical tips in this area:
1) Ensure you’re using InnoDB tables.
To generate update queries:
2) Use WP Optimize plugin.
This can clear out orphaned records, old drafts, etc.
3) Use my Prune Orders plugin.
This can trash all old failed, pending, cancelled orders by selected status to a specified date. If you’re working faster or prefer to use SQL here’s a useful query to select then pivot to a deletion query.
4) Delete legacy post types, post meta, and user meta data.
For example: old order metadata, Google analytics meta, address fields, etc. To see these in order of size:
5) Examine wp_options
table health.
Remove old plugin cruft. To see how many MB are loading with each page request from wp_options
run this to see which rows are hogging up the most space and decide which can be removed:
6) Run through Query Monitor plugin or New Relic APM.
Use available application performance monitoring tools to analyze your staging or production environments. The best screens to test are listing and editing orders as well as all front-end pages.