gpt4 book ai didi

query-optimization - SPARQL 加速联合查询

转载 作者:行者123 更新时间:2023-12-04 07:59:48 25 4
gpt4 key购买 nike

我有自己的数据集,我想在 SPARQL 中执行联合查询。这是查询:

PREFIX : <http://myURIsNamespace#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

select * where {
?bioentity :hasMutatedVersionOf ?gene .
?gene :partOf wd:Q430258 .

SERVICE <https://query.wikidata.org/sparql> {
?gene p:P644 ?statement;
wdt:P31 wd:Q7187 ;
wdt:P703 wd:Q15978631 ;
wdt:P1057 wd:Q430258 .
?statement ps:P644 ?start .
?statement pq:P659 wd:Q20966585 .

?gene p:P645 ?statement2.
?statement2 ps:P645 ?end .
?statement2 pq:P659 wd:Q20966585 .
FILTER (xsd:integer(?start)>21000000 && xsd:integer(?start)<30000000)
}

}

我通过 graphDB SPARQL 接口(interface)运行查询,但它真的很慢。返回 8 条记录需要一分钟多的时间。如果我将查询分成两部分,它们的速度非常快。

查询#1

select * where { 
?bioentity :hasMutatedVersionOf ?gene .
?gene :partOf wd:Q430258 .

}

0.1s 内 56 条记录

查询#2

select * where { 
SERVICE <https://query.wikidata.org/sparql> {
?gene p:P644 ?statement;
wdt:P31 wd:Q7187 ;
wdt:P703 wd:Q15978631 ;
wdt:P1057 wd:Q430258 .
?statement ps:P644 ?start .
?statement pq:P659 wd:Q20966585 .

?gene p:P645 ?statement2.
?statement2 ps:P645 ?end .
?statement2 pq:P659 wd:Q20966585 .
FILTER (xsd:integer(?start)>21000000 && xsd:integer(?start)<30000000)
}

}

0.5s 内 158 条记录

为什么联邦这么慢?有没有办法优化性能?

最佳答案

简答

  1. 只需将您的 SERVICE第一部分,我。 e.之前 ?bioentity :hasMutatedVersionOf ?gene .

  2. 阅读一篇关于该主题的好文章(例如 chapter 5this book )

上述文章的相关引用:

3.3.2 Query Optimization and Execution

The execution order of query operators significantly influences the overall query evaluation cost.Besides the important query execution time there are also otheraspects in the federated scenario which are relevant for the queryoptimization:

Minimizing communication cost. The number of contacteddata sources directly influences the performance of the queryexecution due to the communication overhead. However, reducing thenumber of involved data source trades off against completeness ofresults.

Optimizing execution localization. The standard queryinterfaces of linked data sources are generally only capable ofanswering queries on their provided data. Therefore, joins withother data results usually need to be done at the query issuer. Ifpossible at all, a better strategy will move parts of the resultmerging operations to the data sources, especially if they can beexecuted in parallel.

Streaming results. Retrieving a complete resultwhen evaluating a query on a large dataset may take a while even witha well optimized execution strategy. Thus one can return results assoon as they become available, which can be optimized by trying toreturn relevant results first.

长答案

示例数据

PREFIX : <http://myURIsNamespace#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

INSERT { ?gene rdf:type owl:Thing }
WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?gene p:P644 ?statement;
wdt:P31 wd:Q7187 ;
wdt:P703 wd:Q15978631 ;
wdt:P1057 wd:Q430258 .
?statement ps:P644 ?start .
?statement pq:P659 wd:Q20966585 .
?gene p:P645 ?statement2.
?statement2 ps:P645 ?end .
?statement2 pq:P659 wd:Q20966585 .
FILTER (xsd:integer(?start)>26000000 && xsd:integer(?start)<30000000)
}
}

三元组总数为 79。请注意 26000000用于代替 21000000 .

查询 1

PREFIX : <http://myURIsNamespace#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT * WHERE {
?gene rdf:type owl:Thing .
SERVICE <https://query.wikidata.org/sparql> {
?gene p:P644 ?statement;
wdt:P31 wd:Q7187 ;
wdt:P703 wd:Q15978631 ;
wdt:P1057 wd:Q430258 .
?statement ps:P644 ?start .
?statement pq:P659 wd:Q20966585 .
?gene p:P645 ?statement2.
?statement2 ps:P645 ?end .
?statement2 pq:P659 wd:Q20966585 .
FILTER (xsd:integer(?start)>20000000 && xsd:integer(?start)<30000000)
}
}

查询 2

PREFIX : <http://myURIsNamespace#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT * WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?gene p:P644 ?statement;
wdt:P31 wd:Q7187 ;
wdt:P703 wd:Q15978631 ;
wdt:P1057 wd:Q430258 .
?statement ps:P644 ?start .
?statement pq:P659 wd:Q20966585 .
?gene p:P645 ?statement2.
?statement2 ps:P645 ?end .
?statement2 pq:P659 wd:Q20966585 .
FILTER (xsd:integer(?start)>20000000 && xsd:integer(?start)<30000000)
}
?gene rdf:type owl:Thing
}

性能

<头>
  查询1 查询2
图形数据库 30 秒 1 秒
闪耀图 1 秒 1 秒

GraphDB 行为

执行查询 1,GraphDB 执行 79 个不同的 GET对 Wikidata 的请求¹:

Wireshark — packets

Wireshark — statistics

这些请求属于此类查询:

SELECT ?start ?statement ?end ?statement2 WHERE {
<http://www.wikidata.org/entity/Q18031286> p:P644 ?statement;
wdt:P31 wd:Q7187 ;
wdt:P703 wd:Q15978631 ;
wdt:P1057 wd:Q430258 .
?statement ps:P644 ?start .
?statement pq:P659 wd:Q20966585 .
<http://www.wikidata.org/entity/Q18031286> p:P645 ?statement2.
?statement2 ps:P645 ?end .
?statement2 pq:P659 wd:Q20966585 .
FILTER (xsd:integer(?start)>20000000 && xsd:integer(?start)<30000000)

有趣的是,GraphDB 在另一台机器上执行 GET另一种请求:

GET /sparql?queryLn="Sparql"&query=<original_query_service_part>&$gene=<http://www.wikidata.org/entity/Q18031286>

在此请求中,Sesame protocol使用,URL 中的这些绑定(bind)不是 SPARQL 1.1 Protocol 的一部分.

也许确切的请求类型取决于内部 reuse.vars.in.subselects 的值。参数,默认值在 Windows 和 Linux 上可能不同。


Blazegraph 行为

执行查询 1,Blazegraph 执行单个 POST对 Wikidata² 的请求:

SELECT  ?gene ?statement ?start ?statement2 ?end
WHERE {
?gene p:P644 ?statement;
wdt:P31 wd:Q7187 ;
wdt:P703 wd:Q15978631 ;
wdt:P1057 wd:Q430258 .
?statement ps:P644 ?start .
?statement pq:P659 wd:Q20966585 .
?gene p:P645 ?statement2.
?statement2 ps:P645 ?end .
?statement2 pq:P659 wd:Q20966585 .
FILTER (xsd:integer(?start)>20000000 && xsd:integer(?start)<30000000)

}
VALUES ( ?gene) {
( wd:Q14908148 ) ( wd:Q15320063 ) ( wd:Q17861651 ) ( wd:Q17917753 ) ( wd:Q17928333 )
( wd:Q18024923 ) ( wd:Q18026347 ) ( wd:Q18030710 ) ( wd:Q18031220 ) ( wd:Q18031457 )
( wd:Q18031551 ) ( wd:Q18031832 ) ( wd:Q18032918 ) ( wd:Q18033094 ) ( wd:Q18033798 )
( wd:Q18034311 ) ( wd:Q18035006 ) ( wd:Q18035085 ) ( wd:Q18035609 ) ( wd:Q18036516 )
( wd:Q18036676 ) ( wd:Q18037580 ) ( wd:Q18038385 ) ( wd:Q18038459 ) ( wd:Q18038737 )
( wd:Q18038763 ) ( wd:Q18039997 ) ( wd:Q18040291 ) ( wd:Q18041261 ) ( wd:Q18041415 )
( wd:Q18041558 ) ( wd:Q18045881 ) ( wd:Q18047232 ) ( wd:Q18047373 ) ( wd:Q18047918 )
( wd:Q18047966 ) ( wd:Q18048744 ) ( wd:Q18049145 ) ( wd:Q18049164 ) ( wd:Q18053139 )
( wd:Q18056540 ) ( wd:Q18057411 ) ( wd:Q18060804 ) ( wd:Q18060856 ) ( wd:Q18060876 )
( wd:Q18060905 ) ( wd:Q18060958 ) ( wd:Q20773708 ) ( wd:Q15312971 ) ( wd:Q17860819 )
( wd:Q17917713 ) ( wd:Q18026310 ) ( wd:Q18027015 ) ( wd:Q18031286 ) ( wd:Q18032599 )
( wd:Q18032797 ) ( wd:Q18035169 ) ( wd:Q18035627 ) ( wd:Q18039938 ) ( wd:Q18041207 )
( wd:Q18041512 ) ( wd:Q18041930 ) ( wd:Q18045491 ) ( wd:Q18045762 ) ( wd:Q18046301 )
( wd:Q18046472 ) ( wd:Q18046487 ) ( wd:Q18047149 ) ( wd:Q18047491 ) ( wd:Q18047719 )
( wd:Q18048527 ) ( wd:Q18049774 ) ( wd:Q18051886 ) ( wd:Q18053875 ) ( wd:Q18056212 )
( wd:Q18056538 ) ( wd:Q18065866 ) ( wd:Q20766978 ) ( wd:Q20781543 )
}

结论

使用联合查询,很难创建有效的执行计划,因为远程模式的选择性是未知的。

在您的特定情况下,是否在本地或远程加入结果应该不是很重要,因为本地和远程结果集都很小。但是,在 GraphDB 中,远程加入结果的效果较差,因为 GraphDB 不会降低通信成本。


¹ 对于创建屏幕截图,<http://query.wikidata.org/sparql>而不是 <https://query.wikidata.org/sparql>被使用了。

² 在 Blazegraph 中,可以写成 hint:Query hint:optimizer "None"以确保顺序评估。

关于query-optimization - SPARQL 加速联合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45356326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com