Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 14:00 8:00 – 14:00 8:00 – 14:00 8:00 – 14:00 8:00 – 14:00 -
- 15:00 – 17:00 15:00 – 17:00 15:00 – 17:00 15:00 – 17:00 15:00 – 17:00 -

Supporter timezone: Europe/Madrid (GMT+02:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 1 year, 5 months ago.

Assisted by: Nigel.

Author Posts
November 24, 2022 at 1:56 pm #12527749

wimF

Hi,

The site mentioned (naturalleadership) is running very slow and a query is consuming a lot of CPU on our server.

The query that is reponsible is:
```SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) INNER JOIN wp_postmeta AS mt3 ON ( wp_posts.ID = mt3.post_id ) INNER JOIN wp_postmeta AS mt4 ON ( wp_posts.ID = mt4.post_id ) INNER JOIN wp_postmeta AS mt5 ON ( wp_posts.ID = mt5.post_id ) INNER JOIN wp_postmeta AS mt6 ON ( wp_posts.ID = mt6.post_id ) INNER JOIN wp_postmeta AS mt7 ON ( wp_posts.ID = mt7.post_id ) JOIN wp_icl_translations wpml_translations
ON wp_posts.ID = wpml_translations.element_id
AND wpml_translations.element_type = CONCAT('post_', wp_posts.post_type)
WHERE 1=1 AND (
wp_postmeta.meta_key = 'date_start'
AND
(
(
(
( mt1.meta_key = 'date_start' AND mt1.meta_value >= '20221101' )
AND
( mt2.meta_key = 'date_start' AND mt2.meta_value <= '20221131' )
)
OR
(
( mt3.meta_key = 'date_start' AND mt3.meta_value >= '20221201' )
AND
( mt4.meta_key = 'date_start' AND mt4.meta_value <= '20221231' )
)
OR
(
( mt5.meta_key = 'date_start' AND mt5.meta_value >= '20230101' )
AND
( mt6.meta_key = 'date_start' AND mt6.meta_value <= '20230131' )
)
)
AND
(
( mt7.meta_key = 'filter_service' AND mt7.meta_value LIKE '%\"5966\"%' )
OR
( mt7.meta_key = 'filter_service' AND mt7.meta_value LIKE '%\"5887\"%' )
OR
( mt7.meta_key = 'filter_service' AND mt7.meta_value LIKE '%\"6568\"%' )
)
)
) AND wp_posts.post_type = 'agenda' AND ((wp_posts.post_status = 'publish')) AND ( ( ( wpml_translations.language_code = 'en' OR 0 ) AND wp_posts.post_type IN ('post','page','attachment','wp_block','wp_template','wp_template_part','wp_navigation','agenda','quotes','partners','case','service','team' ) ) OR wp_posts.post_type NOT IN ('post','page','attachment','wp_block','wp_template','wp_template_part','wp_navigation','agenda','quotes','partners','case','service','team' ) )
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_name ASC
LIMIT 0, 6 | 0.000 |```

Googling i find a lot of the same posts, but never an answer.

The query we're running is:
```$args = [
'post_type' => 'agenda',
'orderby' => ['meta_value_num' => 'ASC', 'name' => 'ASC'],
'meta_key' => 'date_start',
'posts_per_page' => $fields['posts_per_page'],
'meta_query' => [
'relation' => 'AND',
[
'key' => 'date_start',
'value' => date('Ymd'),
'compare' => '>=',
],
[
'key' => 'filter_' . $post_type,
'value' => '"' . $post_id . '"',
'compare' => 'LIKE',
]
],
'tax_query' => ['relation' => 'AND']
];

$filter_taxonomies = apply_filters('proteus_agenda_filter_taxonomies', []);

if( !empty($filter_taxonomies) ) {
foreach ($filter_taxonomies as $taxonomy => $title) {

//if( !isset() )
$taxonomy_terms = $fields['filter_taxonomy_' . $taxonomy];
if( !empty($taxonomy_terms) ) {
$args['tax_query'][] = [
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $taxonomy_terms,
];
}
}
}

$agenda = new \WP_Query($args);```

What is wrong and how can we fix this?

November 25, 2022 at 8:53 am #12532661

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi there

There are a lot of joins to the postmeta table arising from filtering by and ordering by post meta, which makes for notoriously slow queries (hidden link).

First, I can see part of how WPML manipulates the query, but I would very much like to see the generated SQL for the same query when WPML is disabled. Could you please test and share that? (And how slow is the query without WPML in comparison to with WPML?)

Second, you may want to try a plugin that can be used to update the WordPress table indexes to improve performance:

https://wordpress.org/plugins/index-wp-mysql-for-speed/

(See this page for more technical details about the changes it makes: hidden link)

If you do try the plugin you will want to make a backup first, naturally.

The topic ‘[Closed] There some SQL queries those are running continuously that slows our serve’ is closed to new replies.