Since T237276 and 781934d, labels for groupings are retrieved in SPARQL instead of using the MediaWiki API.
There were several good reasons for that move, but one unexpected outcome is that it tanks the performance of the grouping query.
When investigating www.wikidata.org/wiki/Wikidata:WikiProject_sum_of_all_paintings/Property_statistics/Sandbox, I realized the grouping SPARQL query is now timing out.
SELECT ?grouping (SAMPLE(?_higher_grouping) as ?higher_grouping) ?grouping_link_value (COUNT(DISTINCT ?entity) as ?count) WHERE {
?entity wdt:P31 wd:Q3305213 .
?entity wdt:P195 ?grouping .
OPTIONAL { ?grouping wdt:P17/wdt:P298 ?_higher_grouping }.
OPTIONAL {{
?grouping rdfs:label ?labelMUL.
FILTER(lang(?labelMUL)='mul')
}}.
OPTIONAL {{
?grouping rdfs:label ?labelEN.
FILTER(lang(?labelEN)='en')
}}.
BIND(COALESCE(?labelEN, ?labelMUL) AS ?grouping_link_value).
} GROUP BY ?grouping ?higher_grouping ?grouping_link_value
HAVING (?count >= 100)
ORDER BY DESC(?count)
LIMIT 1000does not resolve in time, while the previous version without the labels returns correctly.
Can this query be optimized somehow?