Fast publication date lookup with Wikidata Query Service

186 Views Asked by At

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
1

There are 1 best solutions below

2
On BEST ANSWER

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.