Previous topic  Top  Next topic  Print this Topic
 

Prepared Queries

 

For complex queries the preparation of the rules and the optimization of the evaluation strategy may take a lot of time (up to 60% of the whole answering process was reported in some cases). To improve the performance of query preparation, the query answering has been divided into two steps:

The preparation phase and
The execution phase

This allows the re-usage of prepared programs when querying the same or a similar query. Similar means in terms of given value restrictions that can be changed before the execution takes place.

NOTE: Prepared queries are not supported when access control (role-based security) is enabled.

Usage

Prepared queries can be maintained with the following commands:

preparequery
removepreparedquery
preparedquerylist

Their syntax and semantics will be described in the following sections.

Prepare a Query

Syntax:

preparequery [temporary facts] <objectlogic-query> [fillnull]

where

<objectlogic-query> := QUERY <queryname>: <objectlogic>

and

<objectlogic> := any valid objectlogic query that may contain constants _ti which supply placeholders

Semantics:

This command sends the given query to the server. The name will be used to identify the query for further usage in commands. When it is expected to execute the query with temporary facts a sample set of these temporary facts should also be supplied for the preparation process. The preparation of the query depends on the characteristics of the facts in the EDB. If the characteristics change between the preparation and the execution of a query, the server delivers an error and it is necessary to recompile the query. As temporary facts may change these characteristics, we recommend that you send a sample set of these facts.

Execute a prepared query

A prepared query is executed like a normal query, but a special syntax is used.

Query Syntax:

[temporary facts]

[substitute(<substitutions>)@pq.]

?- <vars> <- PREPARED_QUERY(<queryname>,<vars>). [<query options>]

?- ?X:?Y.

Semantics:

Executes a query with the given name. If no prepared query is available an error is reported. Temporary facts are added to the EDB for this query if given. Appended query options (orderedby etc.) work as defined. If the prepared query contains substitutions, the values for these placeholders are provided by the special temporary fact with the predicate "substitute". This allows special constants to be replaced as defined in (1) with concrete values for a given execution.

For example, if there are two substitution placeholders, i.e. _t1 and _t2, the substitution facts must look like this:

substitute(<value for _t1>,<value for _t2>)@pq.

Remove a prepared query

Syntax:

removepreparedquery <queryname>

Semantics:

Removes a prepared query with the given name. If no query exists no error is reported.

Get a list of all prepared queries

Syntax:

preparedquerylist

Semantics:

Returns a list of names of all prepared queries in the server. If no queries exist an empty list is returned.

Sample

Prepare a query by executing the following command:

preparequery QUERY myQuery: - p(?X) AND ?X < "_t1".

Show list of prepared queries by executing the command:

preparedquerylist

Run the prepared query using any interface available to run a normal query:

p(1).

p(2).

p(3).

substitute(2.5)@pq.

?- PREPARED_QUERY("myQuery",?X).

Remove the prepared query by executing the command:

removepreparedquery myQuery

preparequery

This command sends the given query to the server. The name will be used to identify the query for further usage in commands.

syntax: preparequery {temporary facts{ {flogic-query{ [fillnull]
OntoBroker Return Values: Program prepared for query: <queryname>

preparedquerylist

Returns a list of names of all prepared queries in the server. If no queries exist an empty list is returned.

syntax: preparedquerylist
OntoBroker Return Values: [<list-of-prepared-query-names>]

Removepreparedquery

Removes a prepared query with the given name. If no query exists no error is reported.

syntax: removepreparedquery <queryname>
OntoBroker Return Values: PreparedQuery <queryname> removed from server

 

NOTE: Prepared queries are currently not supported when access control is enabled.