I was recently asked for the total number of posts on a WordPress website. Normally as simple as looking at the number in the dashboard, however in this instance the WordPress install was a Multisite with nearly 100 sites. That would have taken far too long to do manually, but what about having MySQL do the heavy lifting for me?
The information_schema database contains data about every database and table on the server. One of the metrics it stores is the count of rows in a table. We can use that to extrapolate roughly how many entries are in all the posts tables combined. This is not an exact science, however it will get you close to the real numbers. I argue that it will typically be “close enough” on larger installs, such as the one I was looking at.
In the following query, just replace the database name with your database and run it.
SELECT SUM( table_rows )
FROM information_schema.TABLES
WHERE
table_schema = "{{YOUR_DATABASE_NAME}}"
AND table_name LIKE '%_posts';