query-6e25442e020ff81bdeb80dc60763fb1d
Any way to make this query faster?Developing the Commons app, I use the query below to show Nearby places. Problem: Recently it is slow and often times out (I set the radius to "0.1" but ideally would like it to be "1"). Question: Is there any trick that would make this query a bit faster?
Use at
- https://query.wikidata.org/sparql
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT
?item
(SAMPLE(?label) AS ?label)
(SAMPLE(?description) AS ?description)
(SAMPLE(?class) AS ?class)
(SAMPLE(?classLabel) AS ?classLabel)
(SAMPLE(?pic) AS ?pic)
(SAMPLE(?destroyed) AS ?destroyed)
(SAMPLE(?endTime) AS ?endTime)
(SAMPLE(?wikipediaArticle) AS ?wikipediaArticle)
(SAMPLE(?commonsArticle) AS ?commonsArticle)
(SAMPLE(?commonsCategory) AS ?commonsCategory)
(SAMPLE(?monument) AS ?monument)
WHERE {
# Around given location
SERVICE wikibase:around {
?item wdt:P625 ?location.
bd:serviceParam wikibase:center "Point(4.89 52.37)"^^geo:wktLiteral. # Longitude latitude
bd:serviceParam wikibase:radius "0.1". # Radius in kilometers.
}
# Get the label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {?item rdfs:label ?itemLabelPreferredLanguage. FILTER (lang(?itemLabelPreferredLanguage) = "en")}
OPTIONAL {?item rdfs:label ?itemLabelAnyLanguage}
BIND(COALESCE(?itemLabelPreferredLanguage, ?itemLabelAnyLanguage, "?") as ?label)
# Get the description in the preferred language of the user, or any other language if no description is available in that language.
OPTIONAL {?item schema:description ?itemDescriptionPreferredLanguage. FILTER (lang(?itemDescriptionPreferredLanguage) = "en")}
OPTIONAL {?item schema:description ?itemDescriptionAnyLanguage}
BIND(COALESCE(?itemDescriptionPreferredLanguage, ?itemDescriptionAnyLanguage, "?") as ?description)
# Get the class label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {
?item p:P31/ps:P31 ?class.
OPTIONAL {?class rdfs:label ?classLabelPreferredLanguage. FILTER (lang(?classLabelPreferredLanguage) = "en")}
OPTIONAL {?class rdfs:label ?classLabelAnyLanguage}
BIND(COALESCE(?classLabelPreferredLanguage, ?classLabelAnyLanguage, "?") as ?classLabel)
}
# Get picture
OPTIONAL {?item wdt:P18 ?pic}
# Get existence
OPTIONAL {?item wdt:P576 ?destroyed}
OPTIONAL {?item wdt:P582 ?endTime}
# Get Commons category
OPTIONAL {?item wdt:P373 ?commonsCategory}
# Get Wikipedia article
OPTIONAL {
?wikipediaArticle schema:about ?item.
?wikipediaArticle schema:isPartOf <https://en.wikipedia.org/>. # TODO internationalization
}
# Get Commons article
OPTIONAL {
?commonsArticle schema:about ?item.
?commonsArticle schema:isPartOf <https://commons.wikimedia.org/>.
}
# Wiki Loves Monuments
OPTIONAL {?item p:P1435 ?monument}
OPTIONAL {?item p:P2186 ?monument}
OPTIONAL {?item p:P1459 ?monument}
OPTIONAL {?item p:P1460 ?monument}
OPTIONAL {?item p:P1216 ?monument}
OPTIONAL {?item p:P709 ?monument}
OPTIONAL {?item p:P718 ?monument}
OPTIONAL {?item p:P5694 ?monument}
}
GROUP BY ?item
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v20("?class"):::projected
v20("?classLabel"):::projected
v11("?classLabelAnyLanguage")
v9("?classLabelPreferredLanguage")
v20("?commonsArticle"):::projected
v20("?commonsCategory"):::projected
v20("?description"):::projected
v20("?destroyed"):::projected
v20("?endTime"):::projected
v3("?item"):::projected
v7("?itemDescriptionAnyLanguage")
v1("?itemDescriptionPreferredLanguage")
v5("?itemLabelAnyLanguage")
v2("?itemLabelPreferredLanguage")
v20("?label"):::projected
v4("?location")
v20("?monument"):::projected
v20("?pic"):::projected
v20("?wikipediaArticle"):::projected
a1((" "))
c6([sPoint(4.89 52.37)^^<http://www.opengis.net/ont/geosparql#wktLiteral>"]):::literal
c19([https://en.wikipedia.org/]):::iri
c4(["bd:serviceParam"]):::iri
c20([https://commons.wikimedia.org/]):::iri
c8(["0.1"]):::literal
subgraph s1["http://wikiba.se/ontology#around"]
style s1 stroke-width:4px;
v3 --"p:direct/P625"--> v4
c4 --"wikibase:center"--> c6
c4 --"wikibase:radius"--> c8
end
subgraph optional0["(optional)"]
style optional0 fill:#bbf,stroke-dasharray: 5 5;
v3 -."rdfs:label".-> v2
end
subgraph optional1["(optional)"]
style optional1 fill:#bbf,stroke-dasharray: 5 5;
v3 -."rdfs:label".-> v5
end
bind0[/"?itemLabelPreferredLanguage?itemLabelAnyLanguage'?'"/]
v2 --o bind0
v5 --o bind0
bind0 --as--o v20
subgraph optional2["(optional)"]
style optional2 fill:#bbf,stroke-dasharray: 5 5;
v3 -."schema:description".-> v1
end
subgraph optional3["(optional)"]
style optional3 fill:#bbf,stroke-dasharray: 5 5;
v3 -."schema:description".-> v7
end
bind1[/"?itemDescriptionPreferredLanguage?itemDescriptionAnyLanguage'?'"/]
v1 --o bind1
v7 --o bind1
bind1 --as--o v20
subgraph optional4["(optional)"]
style optional4 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P31".-> a1
a1 --"p:statement/P31"--> v20
subgraph optional5["(optional)"]
style optional5 fill:#bbf,stroke-dasharray: 5 5;
v20 -."rdfs:label".-> v9
end
subgraph optional6["(optional)"]
style optional6 fill:#bbf,stroke-dasharray: 5 5;
v20 -."rdfs:label".-> v11
end
bind2[/"?classLabelPreferredLanguage?classLabelAnyLanguage'?'"/]
v9 --o bind2
v11 --o bind2
bind2 --as--o v20
end
subgraph optional7["(optional)"]
style optional7 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:direct/P18".-> v20
end
subgraph optional8["(optional)"]
style optional8 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:direct/P576".-> v20
end
subgraph optional9["(optional)"]
style optional9 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:direct/P582".-> v20
end
subgraph optional10["(optional)"]
style optional10 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:direct/P373".-> v20
end
subgraph optional11["(optional)"]
style optional11 fill:#bbf,stroke-dasharray: 5 5;
v20 -."schema:about".-> v3
v20 --"schema:isPartOf"--> c19
end
subgraph optional12["(optional)"]
style optional12 fill:#bbf,stroke-dasharray: 5 5;
v20 -."schema:about".-> v3
v20 --"schema:isPartOf"--> c20
end
subgraph optional13["(optional)"]
style optional13 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P1435".-> v20
end
subgraph optional14["(optional)"]
style optional14 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P2186".-> v20
end
subgraph optional15["(optional)"]
style optional15 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P1459".-> v20
end
subgraph optional16["(optional)"]
style optional16 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P1460".-> v20
end
subgraph optional17["(optional)"]
style optional17 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P1216".-> v20
end
subgraph optional18["(optional)"]
style optional18 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P709".-> v20
end
subgraph optional19["(optional)"]
style optional19 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P718".-> v20
end
subgraph optional20["(optional)"]
style optional20 fill:#bbf,stroke-dasharray: 5 5;
v3 -."p:P5694".-> v20
end
bind14[/"sample(?label)"/]
v20 --o bind14
bind14 --as--o v20
bind15[/"sample(?description)"/]
v20 --o bind15
bind15 --as--o v20
bind16[/"sample(?class)"/]
v20 --o bind16
bind16 --as--o v20
bind17[/"sample(?classLabel)"/]
v20 --o bind17
bind17 --as--o v20
bind18[/"sample(?pic)"/]
v20 --o bind18
bind18 --as--o v20
bind19[/"sample(?destroyed)"/]
v20 --o bind19
bind19 --as--o v20
bind20[/"sample(?endTime)"/]
v20 --o bind20
bind20 --as--o v20
bind21[/"sample(?wikipediaArticle)"/]
v20 --o bind21
bind21 --as--o v20
bind22[/"sample(?commonsArticle)"/]
v20 --o bind22
bind22 --as--o v20
bind23[/"sample(?commonsCategory)"/]
v20 --o bind23
bind23 --as--o v20
bind24[/"sample(?monument)"/]
v20 --o bind24
bind24 --as--o v20