2

I don't understand why the following queries show up in slow query log with query time from 12 to 20 seconds.

UPDATE `wp_postmeta` SET `meta_value` = '35' WHERE `post_id` = 1267 AND `meta_key` = 'views'
UPDATE `wp_postmeta` SET `meta_value` = '32' WHERE `post_id` = 874 AND `meta_key` = 'views'
UPDATE `wp_postmeta` SET `meta_value` = '122' WHERE `post_id` = 18557 AND `meta_key` = 'views'
UPDATE `wp_postmeta` SET `meta_value` = '3078' WHERE `post_id` = 21741 AND `meta_key` = 'views'
UPDATE `wp_postmeta` SET `meta_value` = '2657' WHERE `post_id` = 878 AND `meta_key` = 'views'

They look pretty normal to me and executing 1 of them in phpMyAdmin for testing only takes 0.0056s.
The size of the wp_postmeta table here is 77,996.

I wonder why the above queries are so slow and if there is anything I can do to improve them?

ericn
  • 12,476
  • 16
  • 84
  • 127

2 Answers2

8

Better index to use than what @Sandeep suggested is:

CREATE INDEX <some index name>
ON wp_postmeta (meta_key, post_id);

This index will capture all the WHERE clauses and enable the database engine to quickly go to the right row.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
4

Create an index on column "post_id". It will solve your problem.

When data is huge, indexes play a significant role in query optimization.

Sandeep
  • 1,154
  • 10
  • 16