subquery - WikiData SPARQL 因子查询 + 标签服务 + 可选而超时
问题描述
在尝试回答这个问题时:How to filter results of wikidata to specific language,我遇到了以下问题:
一些国家拥有不止一种资本。此查询随机选择每个国家/地区的一个首都:
SELECT ?country (sample(?capital) as ?aCapital) WHERE {
?country wdt:P31 wd:Q3624078.
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} # not a former country
?country wdt:P36 ?capital.
}
GROUP BY ?country
但是,在尝试添加标签和坐标时,查询超时:
SELECT ?country ?countryLabel ?aCapital ?aCapitalLabel ?coords WHERE {
OPTIONAL {?aCapital wdt:P625 ?coords.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
{
SELECT ?country (sample(?capital) as ?aCapital) WHERE {
?country wdt:P31 wd:Q3624078.
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} # not a former country
?country wdt:P36 ?capital.
}
GROUP BY ?country
}
}
ORDER BY ?countryLabel
LIMIT 1000
解决方案
遵循上面@AKSW 的评论 -OPTIONAL
在 SPARQL 中是左连接。
重新排序子查询和 OPTIONAL 解决了这个问题:
SELECT ?country ?countryLabel ?aCapital ?aCapitalLabel ?coords WHERE {
{
{
SELECT ?country (sample(?capital) as ?aCapital) WHERE {
?country wdt:P31 wd:Q3624078.
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240} # not a former country
?country wdt:P36 ?capital.
}
GROUP BY ?country
}
OPTIONAL {?aCapital wdt:P625 ?coords.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
}
ORDER BY ?countryLabel
LIMIT 1000
请注意,这需要添加一个额外的{
+}
以保持语法正确。
另请参阅:SPARQL 可选查询
推荐阅读
- if-statement - 谷歌表格对前几行数据求和 + 常量变量
- python - 时间序列验证算法
- ruby-on-rails - 表单提交在连续快速提交时失去幂等性
- html - 更改 html 和 Angular 2 中的按钮文本
- fragment - 在活动之间发送数据
- python - 在 Google Cloud Functions 中访问用户定义的函数(ImportError:尝试相对导入而没有已知的父包)
- github - 在 github 页面前加上 www 返回 404
- android - 使用 Qualcomm 编解码器的自定义分辨率时,MediaCodec 的结果出现乱码
- php - 如何使用 foreach 将多个文件名存储到 phpmyadmin 数据库中?
- html - 使用 html 音频制作播放暂停开关