4

Is there a way to lookup publication dates quickly in Wikidata Query Service's SPARQL to find publications of a certain date, e.g., today?

I was hoping that something like this query would be quick:

SELECT * WHERE {
  ?work wdt:P577 ?datetime .
  BIND("2018-09-28T00:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> as ?now_datetime)
  FILTER (?datetime = ?now_datetime)
}
LIMIT 10

However, it times out when using it on the SPARQL endpoint at https://query.wikidata.org

A range query seems neither to be quick. The query below returns after almost 30 seconds:

SELECT * WHERE {
  ?work wdt:P577 ?datetime .
  FILTER (?datetime > "2018-09-28T00:00:00Z"^^xsd:dateTime)
}
LIMIT 1
logi-kal
  • 7,107
  • 6
  • 31
  • 43
Finn Årup Nielsen
  • 6,130
  • 1
  • 33
  • 43

1 Answers1

4

The trick is to avoid full scan and use indexes:

  1. VALUES:

    SELECT * WHERE {
      VALUES (?datetime) {("2018-09-28T00:00:00Z"^^xsd:dateTime)}
      ?work wdt:P577 ?datetime .
    } LIMIT 10
    

    Try it!

  2. hint:rangeSafe:

    SELECT * WHERE {
      VALUES (?datetime) {("2018-09-28T00:00:00Z"^^xsd:dateTime)}
      ?work wdt:P577 ?date_time .
      hint:Prior hint:rangeSafe true .
      FILTER (?date_time > ?datetime)
    } LIMIT 10
    

    Try it!

    [The rangeSafe hint] declare[s] that the data touched by the query for a specific triple pattern is strongly typed, thus allowing a range filter to be pushed down onto an index.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • 1
    why is `VALUES` necessary for a single literal lookup and not just `SELECT * WHERE { ?work wdt:P577 "2018-09-28T00:00:00Z"^^xsd:dateTime . } LIMIT 10` ? – UninformedUser Sep 28 '18 at 09:50
  • 1
    @AKSW, obviously, it's OK too. I consider `"2018-09-28T00:00:00Z"^^xsd:dateTime` as a kind of "inline data", for which `VALUES` is designed. – Stanislav Kralin Sep 28 '18 at 10:10