I am working on extracting and displaying data from a WordPress DB to a mobile app for a customer and I am having a little trouble refining this query to be most efficient.
In wordpress, there are three tables that link the data I need to access
1. wp_posts – in this table there is the main post title, it’s published status and the post type.
2. wp_postmeta – this table has all supplemental info related to the post id in the above table.
3. wp_p2p – this table has links to all the parent-child posts and their relationship.
Because of the volume of data in these tables, the query I currently have takes about 13 seconds to run, could you please take a look at this sqlfiddle and let me know what I could look at to improve it? The query in it’s current form is not the end result, but improving it will improve my end result. I also need to add a search field on the “name” in the wp_postmeta table.
http://sqlfiddle.com/#!2/0e9e0/1
Any direction is appreciated, thank you!
If I understand correctly, you’re looking for only child posts, in which case, the query below should be much faster:
This query will be optimized to find where a match doesn’t exist in the p2p table so that part will be much faster than how you’re currently doing it. It looks like you can also remove the
JOINonwp_postmetasince you don’t use it at all. Removing thatJOINwould also make theGROUP BYredundant and removing it could help the performance a little. Removing theGROUP BYwould also be a good practice since strictly you can’t select non-aggregate fields that aren’t in theGROUP BYclause, but MySQL provides for this functionality so the query will still work either way.