I received an email from my server host telling me that I had to optimize my database requests which seem to be too slow.
Indeed, I use a lot of custom requests within Wordpress and these requests are pointing towards the "wp_postmeta" table. This is not ideal because you have to deal with the table structure as it is given.
Here are the columns within the 'wp-postmeta' table:
meta_id (table id)
post_id (related post which is a foreign key)
meta_key (name of the parameter)
meta_value (value of that parameter)
meta_id is the default primary key of the table and there is NO index by default that is set by Wordpress.
I believe then that the best way to optimize the requests would be to assign an index to one of these columns.
So here is the most frequent request that I have:
SELECT meta_value FROM wp_postmeta WHERE meta_key = 'xxxx' and post_id = 1234
Knowing that:
- A same post_id can have up to 15 different related meta_keys and there are about 900 different post_id's
- A same meta_key can appear more than 150 times on the table records, whatever the post_id's
Which one of these two columns will be the best choice as the index of the table: post_id or meta_key?
Thank you.