0

i've to query a set of statements before i create a new version of an object with the same identifier. With the result i analyse the changes and reuse referenced objects, if the haven't changed. But the following query is aweful and very slow. if there are ~ 1000 object versions, it runs about 120 seconds. and i've to import lot more !

Is there a way to query the statements in a more performant way? I know the "OPTIONAL" is bad, but the properties can be empty.

Thanks

PREFIX schema: <https://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX as: <https://www.w3.org/ns/activitystreams#>

CONSTRUCT {
    ?publication rdf:type ?type ;
        schema:about ?about ;
        schema:about ?about;
        schema:name ?name ;
        as:name ?asname ;
        as:published ?published ;
        as:attributedTo ?attributedTo ;
        schema:creativeWorkStatus ?creativeWorkStatus ;
        schema:dateCreated ?dateCreated ;
        schema:dateModified ?dateModified ;
        schema:description ?description ;
        schema:identifier ?identifier ;
        schema:keywords ?keywords ;
        schema:license ?license ;
        schema:version ?version .
        ?about rdf:type ?aboutType ;
        schema:name ?aboutName ;
        schema:contactPoint ?aboutContactPoint ;
        schema:location ?aboutLocation .
    ?aboutContactPoint rdf:type ?contactPointType ;
        schema:email ?contactPointEmail ;
        schema:name ?contactPointName ;
        schema:telephone?contactPointTelephone .
    ?aboutLocation rdf:type ?locationType ;
        schema:latitude ?locationLatitude ;
      schema:longitude ?locationLongitude ;
      schema:address ?locationAddress .
    # Adress
    ?locationAddress rdf:type ?addressType ;
        schema:addressCountry ?addressCountry ;
        schema:addressLocality ?addressLocality ;
        schema:streetAddress ?addressStreetAddress ;
        schema:postalCode ?addressPostalCode .
}
FROM <http://localhost:8081/camel/fef6dc4b-e1e9-46f5-a34e-8ab5c1600ec4>
WHERE {
    ?publication rdf:type schema:CreativeWork ;
    rdf:type ?type ;
    schema:identifier "6472c8bbf6504b56b473a1e6a10fcc8d".
    OPTIONAL {?publication schema:name ?name . }
    OPTIONAL {?publication as:name ?asname . }
    OPTIONAL {?publication as:published ?published . }
    OPTIONAL {?publication as:attributedTo ?attributedTo . }
    OPTIONAL {?publication schema:creativeWorkStatus ?creativeWorkStatus . }
    OPTIONAL {?publication schema:dateCreated ?dateCreated . }
    OPTIONAL {?publication schema:dateModified ?dateModified . }
    OPTIONAL {?publication schema:description ?description . }
    OPTIONAL {?publication schema:identifier ?identifier . }
    OPTIONAL {?publication schema:keywords ?keywords . }
    OPTIONAL {?publication schema:license ?license . }
    OPTIONAL {?publication schema:version ?version . }
    OPTIONAL {?publication schema:about ?about . }
    # Organization
    OPTIONAL {?publication schema:about/rdf:type ?aboutType . }
    OPTIONAL {?publication schema:about/schema:name ?aboutName . }
    OPTIONAL {?publication schema:about/schema:contactPoint ?aboutContactPoint . }
    OPTIONAL {?publication schema:about/schema:location ?aboutLocation . }
    # ContactPoint
    OPTIONAL {?publication schema:about/schema:contactPoint/rdf:type ?contactPointType . }
    OPTIONAL {?publication schema:about/schema:contactPoint/schema:email ?contactPointEmail . }
    OPTIONAL {?publication schema:about/schema:contactPoint/schema:name ?contactPointName . }
    OPTIONAL {?publication schema:about/schema:contactPoint/schema:telephone?contactPointTelephone . }
    # Place
    OPTIONAL {?publication schema:about/schema:location/rdf:type ?locationType . }
    OPTIONAL {?publication schema:about/schema:location/schema:latitude ?locationLatitude . }
    OPTIONAL {?publication schema:about/schema:location/schema:longitude ?locationLongitude . }
    OPTIONAL {?publication schema:about/schema:location/schema:address ?locationAddress . }
    # Adress
    OPTIONAL {?publication schema:about/schema:location/schema:address/rdf:type ?addressType . }
    OPTIONAL {?publication schema:about/schema:location/schema:address/schema:addressLocality ?addressLocality . }
    OPTIONAL {?publication schema:about/schema:location/schema:address/schema:streetAddress ?addressStreetAddress . }
    OPTIONAL {?publication schema:about/schema:location/schema:address/schema:postalCode ?addressPostalCode . }
    OPTIONAL {?publication schema:about/schema:location/schema:address/schema:addressCountry ?addressCountry . }
}
naturzukunft
  • 79
  • 1
  • 8
  • just to make clear, this isn't an issue of `CONSTRUCT` as query type, which the question might indicate - I think it is just the query itself. You have plenty of `OPTIONAL`s resulting in left-joins. Moreover, many of them contain property paths of length 2 and 3 which is also time consuming if there are so many. – UninformedUser May 15 '22 at 19:09
  • You could start from an easier `SELECT` query and check which of those patterns make the query slow. But in the end, if you need all of the information as triples, there is no way to change anything, there is no room for reorder or other improvements as far as I can see. – UninformedUser May 15 '22 at 19:13
  • 1
    the only thing worth to change is to combine the many `OPTIONAL` patterns. For example, in `OPTIONAL {?publication schema:about ?about . }` you should put all other `OPTIONAL`s that obviously depend on the existence of such an `schema:about` path. The same for the more nested ones like `contactPoint` - use one `OPTIONAL` for it and all depending ones put inside it. In the end you should have some multiple times nested `OPTIONAL` patterns in your query – UninformedUser May 15 '22 at 19:18

1 Answers1

1

Use VALUES to avoid OPTIONAL.

    VALUES ?p {
        schema:name
        as:name
        as:published 
        as:attributedTo
        schema:creativeWorkStatus
        schema:dateCreated
        schema:dateModified
        schema:description
        schema:identifier
        schema:keywords
        schema:license
        schema:version
        schema:about
      }
      ?publication ?p ?o .