You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Håvard Mikkelsen Ottestad <ha...@acando.no> on 2015/11/26 14:17:48 UTC
BGP optimization
Hi,
I’m experimenting with BGP optimization and I’ve hit a bit of a wall.
My question: How can i do stats based BGP rewrites on in-memory datasets/models?
I read online that it’s the storage layers responsibility to handle it, so I set up TDB and created a stats.opt file. Executing queries against the TDB backed Dataset will in fact do a BGP rewrite.
My queries:
#SLOW
select * where {
?a rdfs:label ?label.
?a a ?c.
}
# FAST
select * where {
?a a ?c.
?a rdfs:label ?label.
}
And my dataset:
2 000 000 triples on the form: ?a rdfs:label ?label.
100 triples on the form: ?a a ?c.
My experience so far: It’s faster to query TDB than the in-memory model, simply because it rewrites my query. The SLOW query takes 1.9 seconds in-memory and <0.1 seconds for TDB (without the stats.opt it takes ~5 seconds).
Cheers.
Håvard M. Ottestad
Re: BGP optimization
Posted by Andy Seaborne <an...@apache.org>.
Hi Håvard,
On 26/11/15 21:15, Håvard Mikkelsen Ottestad wrote:
> Hi Andy,
>
> I managed to get the in-mem dataset to rewrite the BGP by using a transform on the query together with stats file from the TDB. I looked at your examples now and saw that those might be better. Anyway.
>
>
> static class Rewrite extends TransformBase {
See TransformReorder
> ReorderTransformation weighted = ReorderLib.weighted("./tdb/stats.opt");
>
> @Override
> public Op transform(OpBGP opBGP) {
> BasicPattern pattern = opBGP.getPattern();
>
> BasicPattern reorder = weighted.reorder(pattern);
>
> OpBGP opBGP1 = new OpBGP();
> opBGP1.getPattern().addAll(reorder);
>
> return super.transform(opBGP1);
> }
> }
>
>
>
> Is this safe do you think?
Looking at it - looks fine for a single block.
When ARQ executes, a subsequent block has the inputs substituted into it
- as done in StageGeneratorGeneric.
>
> And is there a way to generate stats from an in-memory dataset without having to write it to TDB?
The stats engine is more general than the stats files generated by TDB
so you may want to generate the stats files then edit them.
The code in tdb.tdbstats can be easily (well, looking at it it look easy
:-) made to work on Nodes rather than NodeIds.
It uses StatsCollectorBase<T>
There's a writer of the formats as well in org.apache.jena.tdb.solver.stats
Sorry it is prepackaged for workign with any dadaset - if you do end up
coding this, please do contribute it back.
>
> Håvard
Andy
>
>
>
>
>
> On 26/11/15 22:02, "Andy Seaborne" <an...@apache.org> wrote:
>
>> Hi Håvard,
>>
>> The in-memory dataset does limited reordering of BGPs : it is not stats
>> based, it simply uses most grounded triple first.
>>
>> This is done in it's StageGenerator, the extension point to add BGP
>> processing. TDB intercepts more of the algebra than this and does not
>> match Nodes but internal ids.
>>
>> For in-memory it's StageGeneratorGeneric and it uses ReorderFixed (which
>> is the same as TDB's fixed reordering). To have a different policy, put
>> a a different StageGenerator for that dataset or globally,.
>>
>> There is an example of doing this in
>> jena-arq/src-examples/arq/examples/bgpmatching/
>>
>> Andy
>>
>> FYI: The project is in the process of adding a new in-memory dataset
>> implementation which is fully ACID transactional. It stores all quads
>> and triples itself, unlike the usual in-memory dataset is "general
>> purpose". In the general one you can add graphs from any storage and is
>> not transactional; there is a wrapper to add MRSW-lock controlled
>> transactions.
>>
>>
>> On 26/11/15 13:17, Håvard Mikkelsen Ottestad wrote:
>>> Hi,
>>>
>>> I’m experimenting with BGP optimization and I’ve hit a bit of a wall.
>>>
>>> My question: How can i do stats based BGP rewrites on in-memory datasets/models?
>>>
>>> I read online that it’s the storage layers responsibility to handle it, so I set up TDB and created a stats.opt file. Executing queries against the TDB backed Dataset will in fact do a BGP rewrite.
>>>
>>> My queries:
>>>
>>> #SLOW
>>> select * where {
>>> ?a rdfs:label ?label.
>>> ?a a ?c.
>>> }
>>>
>>>
>>> # FAST
>>> select * where {
>>> ?a a ?c.
>>> ?a rdfs:label ?label.
>>> }
>>>
>>> And my dataset:
>>>
>>> 2 000 000 triples on the form: ?a rdfs:label ?label.
>>> 100 triples on the form: ?a a ?c.
>>>
>>> My experience so far: It’s faster to query TDB than the in-memory model, simply because it rewrites my query. The SLOW query takes 1.9 seconds in-memory and <0.1 seconds for TDB (without the stats.opt it takes ~5 seconds).
>>>
>>> Cheers.
>>> Håvard M. Ottestad
>>>
>>
Re: BGP optimization
Posted by Håvard Mikkelsen Ottestad <ha...@acando.no>.
Hi Andy,
I managed to get the in-mem dataset to rewrite the BGP by using a transform on the query together with stats file from the TDB. I looked at your examples now and saw that those might be better. Anyway.
static class Rewrite extends TransformBase {
ReorderTransformation weighted = ReorderLib.weighted("./tdb/stats.opt");
@Override
public Op transform(OpBGP opBGP) {
BasicPattern pattern = opBGP.getPattern();
BasicPattern reorder = weighted.reorder(pattern);
OpBGP opBGP1 = new OpBGP();
opBGP1.getPattern().addAll(reorder);
return super.transform(opBGP1);
}
}
Is this safe do you think?
And is there a way to generate stats from an in-memory dataset without having to write it to TDB?
Håvard
On 26/11/15 22:02, "Andy Seaborne" <an...@apache.org> wrote:
>Hi Håvard,
>
>The in-memory dataset does limited reordering of BGPs : it is not stats
>based, it simply uses most grounded triple first.
>
>This is done in it's StageGenerator, the extension point to add BGP
>processing. TDB intercepts more of the algebra than this and does not
>match Nodes but internal ids.
>
>For in-memory it's StageGeneratorGeneric and it uses ReorderFixed (which
>is the same as TDB's fixed reordering). To have a different policy, put
>a a different StageGenerator for that dataset or globally,.
>
>There is an example of doing this in
>jena-arq/src-examples/arq/examples/bgpmatching/
>
> Andy
>
>FYI: The project is in the process of adding a new in-memory dataset
>implementation which is fully ACID transactional. It stores all quads
>and triples itself, unlike the usual in-memory dataset is "general
>purpose". In the general one you can add graphs from any storage and is
>not transactional; there is a wrapper to add MRSW-lock controlled
>transactions.
>
>
>On 26/11/15 13:17, Håvard Mikkelsen Ottestad wrote:
>> Hi,
>>
>> I’m experimenting with BGP optimization and I’ve hit a bit of a wall.
>>
>> My question: How can i do stats based BGP rewrites on in-memory datasets/models?
>>
>> I read online that it’s the storage layers responsibility to handle it, so I set up TDB and created a stats.opt file. Executing queries against the TDB backed Dataset will in fact do a BGP rewrite.
>>
>> My queries:
>>
>> #SLOW
>> select * where {
>> ?a rdfs:label ?label.
>> ?a a ?c.
>> }
>>
>>
>> # FAST
>> select * where {
>> ?a a ?c.
>> ?a rdfs:label ?label.
>> }
>>
>> And my dataset:
>>
>> 2 000 000 triples on the form: ?a rdfs:label ?label.
>> 100 triples on the form: ?a a ?c.
>>
>> My experience so far: It’s faster to query TDB than the in-memory model, simply because it rewrites my query. The SLOW query takes 1.9 seconds in-memory and <0.1 seconds for TDB (without the stats.opt it takes ~5 seconds).
>>
>> Cheers.
>> Håvard M. Ottestad
>>
>
Re: BGP optimization
Posted by Andy Seaborne <an...@apache.org>.
Hi Håvard,
The in-memory dataset does limited reordering of BGPs : it is not stats
based, it simply uses most grounded triple first.
This is done in it's StageGenerator, the extension point to add BGP
processing. TDB intercepts more of the algebra than this and does not
match Nodes but internal ids.
For in-memory it's StageGeneratorGeneric and it uses ReorderFixed (which
is the same as TDB's fixed reordering). To have a different policy, put
a a different StageGenerator for that dataset or globally,.
There is an example of doing this in
jena-arq/src-examples/arq/examples/bgpmatching/
Andy
FYI: The project is in the process of adding a new in-memory dataset
implementation which is fully ACID transactional. It stores all quads
and triples itself, unlike the usual in-memory dataset is "general
purpose". In the general one you can add graphs from any storage and is
not transactional; there is a wrapper to add MRSW-lock controlled
transactions.
On 26/11/15 13:17, Håvard Mikkelsen Ottestad wrote:
> Hi,
>
> I’m experimenting with BGP optimization and I’ve hit a bit of a wall.
>
> My question: How can i do stats based BGP rewrites on in-memory datasets/models?
>
> I read online that it’s the storage layers responsibility to handle it, so I set up TDB and created a stats.opt file. Executing queries against the TDB backed Dataset will in fact do a BGP rewrite.
>
> My queries:
>
> #SLOW
> select * where {
> ?a rdfs:label ?label.
> ?a a ?c.
> }
>
>
> # FAST
> select * where {
> ?a a ?c.
> ?a rdfs:label ?label.
> }
>
> And my dataset:
>
> 2 000 000 triples on the form: ?a rdfs:label ?label.
> 100 triples on the form: ?a a ?c.
>
> My experience so far: It’s faster to query TDB than the in-memory model, simply because it rewrites my query. The SLOW query takes 1.9 seconds in-memory and <0.1 seconds for TDB (without the stats.opt it takes ~5 seconds).
>
> Cheers.
> Håvard M. Ottestad
>