Depuración de consultas a la base de datos

Encontrar los cuellos de botella de la base de datos en WordPress puede llegar a complicarse sin querer. Y aunque tenemos el Query Monitor, sin duda no siempre es fácil acceder al problema.

Para usar Query Debug necesitaremos instalar una extensión.

wp package install runcommand/query-debug

A partir de aquí, sólo hemos de indicar la URL a analizar y listar sus consultas:

wp query-debug --url=https://example.com/2021/08/01/hello-world/ --format=table --path=/webs/example.com/

Esto nos devolverá algo tal que así:

+----------+----------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------+
| seconds  | backtrace                                                                              | query                                                                                  |
+----------+----------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------+
| 0.000137 | WP_CLI\Runner->load_wordpress, require('wp-settings.php'), include_once('/plugins/heal | SELECT VERSION()                                                                       |
|          | th-check/health-check.php'), require_once('/plugins/health-check/includes/class-health |                                                                                        |
|          | -check-site-status.php'), Health_Check_Site_Status->__construct, Health_Check_Site_Sta |                                                                                        |
|          | tus->init, Health_Check_Site_Status->prepare_sql_data                                  |                                                                                        |
| 0.000433 | wp, WP->main, WP->query_posts, WP_Query->query, WP_Query->get_posts                    | SELECT SQL_CALC_FOUND_ROWS  wpe21jz2_posts.ID FROM wpe21jz2_posts  WHERE 1=1  AND wpe2 |
|          |                                                                                        | 1jz2_posts.post_type = 'post' AND (wpe21jz2_posts.post_status = 'publish')  ORDER BY w |
|          |                                                                                        | pe21jz2_posts.post_date DESC LIMIT 0, 100                                              |
| 4.8E-5   | wp, WP->main, WP->query_posts, WP_Query->query, WP_Query->get_posts, WP_Query->set_fou | SELECT FOUND_ROWS()                                                                    |
|          | nd_posts                                                                               |                                                                                        |
| 0.000214 | require_once('wp-includes/template-loader.php'), get_front_page_template, get_query_te | SELECT   wpe21jz2_posts.* FROM wpe21jz2_posts  WHERE 1=1  AND wpe21jz2_posts.post_name |
|          | mplate, locate_block_template, resolve_block_template, get_block_templates, WP_Query-> |  IN ('front-page') AND (    0 = 1 ) AND wpe21jz2_posts.post_type = 'wp_template' AND ( |
|          | __construct, WP_Query->query, WP_Query->get_posts                                      | (wpe21jz2_posts.post_status = 'publish')) GROUP BY wpe21jz2_posts.ID ORDER BY wpe21jz2 |
|          |                                                                                        | _posts.post_date DESC                                                                  |
| 0.000108 | require_once('wp-includes/template-loader.php'), get_home_template, get_query_template | SELECT   wpe21jz2_posts.* FROM wpe21jz2_posts  WHERE 1=1  AND wpe21jz2_posts.post_name |
|          | , locate_block_template, resolve_block_template, get_block_templates, WP_Query->__cons |  IN ('home','index') AND (    0 = 1 ) AND wpe21jz2_posts.post_type = 'wp_template' AND |
|          | truct, WP_Query->query, WP_Query->get_posts                                            |  ((wpe21jz2_posts.post_status = 'publish')) GROUP BY wpe21jz2_posts.ID ORDER BY wpe21j |
|          |                                                                                        | z2_posts.post_date DESC                                                                |
| 9.2E-5   | require_once('wp-includes/template-loader.php'), include('/themes/twentytwentyone/inde | SELECT   wpe21jz2_posts.ID FROM wpe21jz2_posts  WHERE 1=1  AND (    0 = 1 ) AND wpe21j |
|          | x.php'), get_header, locate_template, load_template, require_once('/themes/twentytwent | z2_posts.post_type = 'wp_global_styles' AND ((wpe21jz2_posts.post_status = 'publish')) |
|          | yone/header.php'), wp_head, do_action('wp_head'), WP_Hook->do_action, WP_Hook->apply_f |  GROUP BY wpe21jz2_posts.ID ORDER BY wpe21jz2_posts.post_date DESC LIMIT 0, 1          |
|          | ilters, wp_enqueue_scripts, do_action('wp_enqueue_scripts'), WP_Hook->do_action, WP_Ho |                                                                                        |
|          | ok->apply_filters, gutenberg_experimental_global_styles_enqueue_assets, WP_Theme_JSON_ |                                                                                        |
|          | Resolver_Gutenberg::get_merged_data, WP_Theme_JSON_Resolver_Gutenberg::get_user_data,  |                                                                                        |
|          | WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type, wp_get_recent_p |                                                                                        |
|          | osts, get_posts, WP_Query->query, WP_Query->get_posts                                  |                                                                                        |
| 0.000332 | require_once('wp-includes/template-loader.php'), include('/themes/twentytwentyone/inde | SELECT   wpe21jz2_posts.ID FROM wpe21jz2_posts  WHERE 1=1  AND wpe21jz2_posts.post_typ |
|          | x.php'), get_footer, locate_template, load_template, require_once('/themes/twentytwent | e = 'post' AND ((wpe21jz2_posts.post_status = 'publish'))  ORDER BY wpe21jz2_posts.pos |
|          | yone/footer.php'), get_template_part, locate_template, load_template, require('/themes | t_date DESC LIMIT 0, 5                                                                 |
|          | /twentytwentyone/template-parts/footer/footer-widgets.php'), dynamic_sidebar, WP_Widge |                                                                                        |
|          | t->display_callback, WP_Widget_Recent_Posts->widget, WP_Query->__construct, WP_Query-> |                                                                                        |
|          | query, WP_Query->get_posts                                                             |                                                                                        |
+----------+----------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------+