wordpress-seo icon indicating copy to clipboard operation
wordpress-seo copied to clipboard

Slow SQL QUERY Bug.

Open Kocarik opened this issue 10 years ago • 11 comments

Hallo,

I have instal woocommerce + yoast seo , and my MySQL Databaze report Slow Query please fix, her is report from log:

# Query_time: 4.848047 Lock_time: 0.000028 Rows_sent: 0 Rows_examined: 574724
use hmlm95nx;
SET timestamp=1454396620;
DELETE FROM wp_options WHERE option_name LIKE '_transient_wpseo_sitemap_cache_shop_order_%' OR option_name LIKE '_transient_timeout_wpseo_sitemap_cache_shop_order_%';
# Time: 160202 8:03:46

https://wordpress.org/plugins/wordpress-seo/

tom Member Posted 15 hours ago #

i face the same issue. Please see this .

(1)WordPress database error Deadlock found when trying to get lock; try restarting transaction for query DELETE FROM wp_options WHERE option_name LIKE 'transient_wpseo_sitemap_cache_shop_order%' OR option_name LIKE 'transient_timeout_wpseo_sitemap_cache_shop_order%' made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), call_user_func_array, WC_AJAX::do_wc_ajax, do_action('wc_ajax_checkout'), call_user_func_array, WC_AJAX::checkout, WC_Checkout->process_checkout, WC_Checkout->create_order, wc_create_order, wp_insert_post, clean_post_cache, clean_object_term_cache, do_action('clean_object_term_cache'), call_user_func_array, wpseo_invalidate_sitemap_cache_terms, wpseo_invalidate_sitemap_cache, WPSEO_Utils::clear_sitemap_cache

(2)WordPress database error Deadlock found when trying to get lock; try restarting transaction for query DELETE FROM wp_options WHERE option_name LIKE 'transient_wpseo_sitemap_cache_shop_order%' OR option_name LIKE 'transient_timeout_wpseo_sitemap_cache_shop_order%' made by require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), call_user_func_array, WC_AJAX::do_wc_ajax, do_action('wc_ajax_checkout'), call_user_func_array, WC_AJAX::checkout, WC_Checkout->process_checkout, WC_Gateway_COD->process_payment, WC_Abstract_Order->update_status, WC_Abstract_Order->add_order_note, wp_insert_comment, wp_update_comment_count, wp_update_comment_count_now, clean_post_cache, clean_object_term_cache, do_action('clean_object_term_cache'), call_user_func_array, wpseo_invalidate_sitemap_cache_terms, wpseo_invalidate_sitemap_cache, WPSEO_Utils::clear_sitemap_cache

Kocarik avatar Feb 25 '16 22:02 Kocarik

Are you quoting the first part from someone else? It doesn't seem to be any similar to the errors in second part.

Deadlock one is curious and is likely coming from concurrent queries conflicting. Do you experience it repeatedly? Occasionally?

Rarst avatar Mar 10 '16 12:03 Rarst

Another similar report linked. I think this seems to be more of performance issue than code logic issue exactly. Need to make sense if we can handle situation better, but not completely clear what precise conditions cause this.

Rarst avatar Mar 24 '16 10:03 Rarst

Hello,

I think I'm facing this issue too, we recently(last week) updated the plugin to version 3.1.2, we were using version 3.0.6 since last december.

We had two incidents since the update, the last was one hour ago and I was able to investigate it better.

Queries in 'show processlist' at the time of the incident, there was a lot more like these:

| 134403023 | xxx | xxx | xxx | Query   |    3 | updating | DELETE FROM wp_options WHERE option_name LIKE '\_transient\_%\_8C8T6D' |
| 134403131 | xxx | xxx | xxx | Query   |    2 | updating | DELETE FROM wp_options WHERE option_name LIKE '\_transient\_%\_8CyaJt' |
| 134403172 | xxx | xxx | xxx | Query   |    0 | updating | DELETE FROM wp_options WHERE option_name LIKE '\_transient\_%\_8Ch8mr' |
| 134403209 | xxx | xxx | xxx | Query   |    2 | updating | DELETE FROM wp_options WHERE option_name LIKE '\_transient\_%\_8Cxzws' |

This kind of query also appears in the logs of the previous incident:

# User@Host: xxx[xxx] @  [xxx]  Id: 132214597
# Query_time: 1.812995  Lock_time: 0.995601 Rows_sent: 0  Rows_examined: 3669
SET timestamp=1459111552;
DELETE FROM wp_options WHERE option_name LIKE '\_transient\_%\_9QRays';
# Time: 160327 20:45:53
# User@Host: xxx[xxx] @  [xxx]  Id: 132214780
# Query_time: 1.832642  Lock_time: 1.484869 Rows_sent: 0  Rows_examined: 3669
SET timestamp=1459111553;
DELETE FROM wp_options WHERE option_name LIKE '\_transient\_%\_9QPEfu';

Stacktrace from New Relic:

Error message
MysqlError: Lock wait timeout exceeded; try restarting transaction

Stack trace
in mysql_query called at /data/www/xxx/wp/wp-includes/wp-db.php (1750)
in wpdb::_do_query called at /data/www/xxx/wp/wp-includes/wp-db.php (1654)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (179)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (307)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (95)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (434)
…lled at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/DbCache.php (143)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (95)
…t /data/www/xxx/content/plugins/wordpress-seo/inc/class-wpseo-utils.php (579)
…t /data/www/xxx/content/plugins/wordpress-seo/inc/class-wpseo-utils.php (539)
…t /data/www/xxx/content/plugins/wordpress-seo/inc/class-wpseo-utils.php (516)
… at /data/www/xxx/content/plugins/wordpress-seo/inc/wpseo-functions.php (317)
… at /data/www/xxx/content/plugins/wordpress-seo/inc/wpseo-functions.php (348)
in wpseo_invalidate_sitemap_cache_on_save_post called at ? (?)
… call_user_func_array called at /data/www/xxx/wp/wp-includes/plugin.php (503)
in do_action called at /data/www/xxx/wp/wp-includes/post.php (3591)
in wp_insert_post called at /data/www/xxx/wp/wp-admin/includes/post.php (597)
…t_default_post_to_edit called at /data/www/xxx/wp/wp-admin/post-new.php (78)

Error message
MysqlError: Lock wait timeout exceeded; try restarting transaction

Sample stack trace
in mysql_query called at /data/www/xxx/wp/wp-includes/wp-db.php (1750)
in wpdb::_do_query called at /data/www/xxx/wp/wp-includes/wp-db.php (1654)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (179)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (307)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (95)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (434)
…lled at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/DbCache.php (143)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (95)
…t /data/www/xxx/content/plugins/wordpress-seo/inc/class-wpseo-utils.php (579)
…t /data/www/xxx/content/plugins/wordpress-seo/inc/class-wpseo-utils.php (539)
…t /data/www/xxx/content/plugins/wordpress-seo/inc/class-wpseo-utils.php (516)
… at /data/www/xxx/content/plugins/wordpress-seo/inc/wpseo-functions.php (317)
… at /data/www/xxx/content/plugins/wordpress-seo/inc/wpseo-functions.php (348)
in wpseo_invalidate_sitemap_cache_on_save_post called at ? (?)
… call_user_func_array called at /data/www/xxx/wp/wp-includes/plugin.php (503)
in do_action called at /data/www/xxx/wp/wp-includes/post.php (3591)
in wp_insert_post called at /data/www/xxx/wp/wp-includes/post.php (3664)
in wp_update_post called at /data/www/xxx/wp/wp-admin/includes/post.php (369)
in edit_post called at /data/www/xxx/wp/wp-admin/includes/post.php (1697)
in post_preview called at /data/www/xxx/wp/wp-admin/post.php (318)

This one started at our theme's code, it is during a get_transient:

Error message
MysqlError: Deadlock found when trying to get lock; try restarting transaction

Stack trace
in mysql_query called at /data/www/xxx/wp/wp-includes/wp-db.php (1750)
in wpdb::_do_query called at /data/www/xxx/wp/wp-includes/wp-db.php (1654)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (179)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (307)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (95)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (434)
…lled at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/DbCache.php (143)
…ry called at /data/www/xxx/content/plugins/w3-total-cache/lib/W3/Db.php (95)
in W3_Db::query called at /data/www/xxx/wp/wp-includes/wp-db.php (1957)
in wpdb::delete called at /data/www/xxx/wp/wp-includes/option.php (501)
in delete_option called at /data/www/xxx/wp/wp-includes/option.php (627)
… called at /data/www/xxx/content/themes/eee/inc/views.php (14)
…author_views called at /data/www/xxx/content/themes/eee/author.php (19)
in include called at /data/www/xxx/wp/wp-includes/template-loader.php (75)
in require_once called at /data/www/xxx/wp/wp-blog-header.php (16)
in require called at /data/www/xxx/index.php (17)

We had 4k active users(according to GA real time) during the first incident, and 2k active users during the last one.

Are my incidents related to this issue?

I have a backup of the previous installed version of the plugin, should I rollback?

Thanks!

moisespr avatar Mar 30 '16 19:03 moisespr

So the invalidation did recently change, however not all reports so far seem to have queries from a new version. So I am not confident at all that it is connected to update.

This feels like performance issue and deleting possibly many transients from possibly very large options table can take time.

For now I would say that I highly recommend using WP object cache for any serious site. That will have transients stored there and database cleanup queries unnecessary and skipped.

Rarst avatar Mar 30 '16 19:03 Rarst

Some probably relevant metrics that would be helpful to hear on this:

  • how many rows do you have in options table?
  • how many sitemaps do you have generated in index?

Rarst avatar Apr 12 '16 08:04 Rarst

#4429 should fix excessive invalidation calls for saves in post types that don't need it, which probably indirectly improves SQL issue.

Rarst avatar Apr 22 '16 08:04 Rarst

I'm having a problem with saving all post types (posts, pages, Woocommerce products) taking 30+ seconds when Yoast SEO 3.2.5 is active. Site is on a VPS, max memory limit is 1GB.

I have a large wp_options table (827510 row) with lots of entries beginning _transient_wcch. Clearing transients in Woocommerce 2.5.5 System Status --> Tools does not seem to affect this.

I'm not using a caching plugin. Performance is good other than when trying to write to the database.

Disabling the XML Sitemaps module resolves the issue. But I would really rather like XML Sitemaps!

Am I in the right place to raise this and can anyone offer any advice?

nikkeefe avatar May 13 '16 19:05 nikkeefe

@nikkeefe using Object Cache would be a good idea for any sizeable site, that will make transient cache stop using database.

I am slowly working out kinks in cache logic about excessive invalidations on post saves, but at the end of the day it needs to happen sometimes and the only way to untangle it from database is if Object Cache is being used for storage.

Rarst avatar May 14 '16 06:05 Rarst

Thank you. So I should use something like W3TC to do this? Or have I misunderstood?

Do you know any other low-risk ways of getting rid of all these 100,000s or transients from my database, as they don't seem to be getting cleared by whatever function is built into Woocommerce for this?

For info, in case it helps you, this began after I updated the Yoast SEO plugin for the first time in at least six months, and possibly twelve. These ~30-120 second delays on saving posts weren't previously happening.

nikkeefe avatar May 16 '16 11:05 nikkeefe

So I should use something like W3TC to do this? Or have I misunderstood?

There are multiple solutions that implement object cache, see documentation.

Do you know any other low-risk ways of getting rid of all these 100,000s or transients from my database, as they don't seem to be getting cleared by whatever function is built into Woocommerce for this?

You could just delete transient entries from DB via SQL (having bakup and such of course). I don't know what WooCommerce is attempting to do or why it fails for you.

For info, in case it helps you, this began after I updated the Yoast SEO plugin for the first time in at least six months, and possibly twelve.

Cache invalidation has been reworked recently. We are working out kinks between invalidating not enough (leaving invalid sitemap up) and invalidating too much (causing performance issues on installs that don't handle it too well).

Rarst avatar May 16 '16 12:05 Rarst

#4574 further fixes excessive cache flushes for types that don't need it (among other things this should get rid of flushes for WooCommerce orders).

#4625 should toggle clears off altogether if transient caching is disabled.

Overall I don't think there is a single "fix" to this. We will continue to optimize mechanics of cache and flushing, but in the end at some point flushes do need to happen.

As mentioned above I would recommend to use Object Cache, which makes transients stored there and any DB issues related to them go away.

Rarst avatar May 24 '16 09:05 Rarst