This page describes an older version of the product. The latest stable version is 16.4.


The SQLQuery class is used to query the space using SQL-like syntax. The query statement includes only the WHERE statement part - the selection aspect of a SQL statement is embedded in other parameters for a SQL query.

See also:

For the full documentation of the class’s methods and constructors, see Javadoc.


An SQLQuery is composed from the type of entry to query and an expression in a SQL syntax.

For example, suppose we have a class called MyClass with an Integer property called num and a String property called name:

// Read an entry of type MyClass whose num property is greater than 500:
MyClass result1 = SQLQuery<MyClass>(MyClass.class, "num > 500"));

// Take an entry of type MyClass whose num property is less than 500:
MyClass result2 = gigaSpace.take(new SQLQuery<MyClass>(MyClass.class, "num < 500"));

MyClass[] results;
// Read all entries of type MyClass whose num is between 1 and 100:
results = gigaSpace.readMultiple(new SQLQuery<MyClass>(MyClass.class, "num >= 1 AND num <= 100"));

// Read all entries of type MyClass who num is between 1 and 100 using BETWEEN syntax:
results = gigaSpace.readMultiple(new SQLQuery<MyClass>(MyClass.class, "num BETWEEN 1 AND 100"));

// Read all entries of type MyClass whose num is either 1, 2, or 3:
results = gigaSpace.readMultiple(new SQLQuery<MyClass>(MyClass.class, "num IN (1,2,3)"));

// Read all entries of type MyClass whose num is greater than 1,
// and order the results by the name property:
results = gigaSpace.readMultiple(new SQLQuery<MyClass>(MyClass.class, "num > 1 ORDER BY name"));
See also:
For an example of SQLQuery with EventSession, refer to the Session Based Messaging API section.

Supported Space Operations

The following operations fully support GigaSpaces `SQLQuery`:
  • count
  • clear
  • read, readIfExists, readMultiple
  • take, takeIfExists, takeMultiple

The following operations supportSQLQuery only with Simple Queries:

  • snapshot
  • EventSession
  • GSIterator

Supported SQL Features

SQLQuery supports the following:
  • AND / OR operators to combine two or more conditions.
  • All basic logical operations to create conditions: =, <>, <, >, >=, <=, like, NOT like, is null, is NOT null, IN.
  • ORDER BY (ASC | DESC) for multiple properties. Supported only by readMultiple. ORDER BY supports also nested object fields.
  • GROUP BY - performs DISTINCT on the properties. Supported only by readMultiple. GROUP BY supports also nested object fields.
  • sysdate - current system date and time.
  • rownum - limits the number of rows to select.
  • Sub queries.
  • ”.” used to indicate a double data type.
  • Regular Index and a Compound Index - Index a single property or multiple properties to improve query execution time.

Comparing null values

SQLQuery comparisons will evaluate the non-null values for a field. If you want to include objects with null values in a SQLQuery you will need to add another condition to your clause.

For example, if you have an object with a property named message of type String, which is set to null and you write it to the space, then you write a SQLQuery with the where clause is: "message <> 'abcd'".

You may expect that the object with message = null gets returned in the query. However it doesn’t, the comparison looks at only non-null values. To get the objects that are also null for message use: "message <> 'abcd' or message = null" to include objects whose message property is set to null.


It is highly recommended to use indexes on relevant properties to increase performance when using equality , bigger / less than , BETWEEN, IN , LIKE , NOT LIKE, IS NULL statements. For more information see Indexing. The above supported query features can leverage indexes except for the is NOT null and NOT IN statement.

Parameterized Queries

In many cases developers prefer to separate the concrete values from the SQL criteria expression. In GigaSpaces’ SQLQuery this can be done by placing a ’?’ symbol instead of the actual value in the expression. When executing the query, the conditions that includes ’?’ are replaced with corresponding parameter values supplied via the setParameter/setParameters methods, or the SQLQuery constructor. For example:

// Option 1 - Use the fluent setParameter(int index, Object value) method:
SQLQuery<MyClass> query1 = new SQLQuery<MyClass>(MyClass.class,
    "num > ? or num < ? and name = ?")
    .setParameter(1, 2)
    .setParameter(2, 3)
    .setParameter(3, "smith");

// Option 2 - Use the setParameters(Object... parameters) method:
SQLQuery<MyClass> query2 = new SQLQuery<MyClass>(MyClass.class,
    "num > ? or num < ? and name = ?");
query.setParameters(2, 3, "smith");

// Option 3: Use the constructor to pass the parameters:
SQLQuery<MyClass> query3 = new SQLQuery<MyClass>(MyClass.class,
    "num > ? or num < ? and name = ?", 2, 3, "smith");

The number of ’?’ symbols in the expression string must match the number of parameters set on the query. For example, when using IN condition:

SQLQuery<MyClass> query = new SQLQuery<MyClass>(MyClass.class,
    "name = ? AND num IN (?,?,?)");
query.setParameters("A", 1, 2, 3);

// Is equivalent to:
SQLQuery<MyClass> query = new SQLQuery<MyClass>(MyClass.class,
    "name = 'A' AND num IN (1,2,3)");

Parameter assignment to the SQLQuery instance is not thread safe. If the query is intended to be executed on multiple threads which may change the parameters, it is recommended to use different SQLQuery instances. This has an analogue in JDBC, because PreparedStatement is not threadsafe either. In previous options, parameters could be passed via a POJO template as well. This option is still available, but is deprecated and will be removed in future versions.

Properties Types

Nested Properties

GigaSpaces SQL syntax contains various extensions to support matching nested properties, maps, collections and arrays.

Some examples:

// Query for a Person who lives in New York:
... = new SQLQuery<Person>(Person.class, " = 'New York'");
// Query for a Dealer which sales a Honda:
... = new SQLQuery<Dealer>(Dealer.class, "cars[*] = 'Honda'");

For more information see Query Nested Properties.

Enum Properties

An enum property can be matched either using the enum’s instance value or its string representation. For example:

public class Vehicle {
    public enum VehicleType { CAR, BIKE, TRUCK };

    private VehicleType type;
    // Getters and setters are omitted for brevity

// Query for vehicles of type CAR using the enum's value:
... = new SQLQuery<Vehicle>(Vehicle.class, "type = ?", VehicleType.CAR);
// Query for vehicles of type CAR using the enum's string representation:
... = new SQLQuery<Vehicle>(Vehicle.class, "type = 'CAR'");
When using an Enum string value, the value must be identical (case sensitive) to the name of the Enum value.

Date Properties

A Date property can be matched either using the Date instance value or its string representation. For example:

// Query using a Date instance value:
... = new SQLQuery<MyClass>(MyClass.class, "birthday < ?", new java.util.Date(2020, 11, 20));
// Query using a Date string representation:
... = new SQLQuery<MyClass>(MyClass.class ,"birthday < '2020-12-20'");

Specifying date and time values as strings is error prone since it requires configuring the date and time format properties and adhering to the selected format. It is recommended to simply use Date instance parameters.

When string representation is required, the following space properties should be used:


For example:

    <os-core:space id="space" url="/./space">
                <prop key="space-config.QueryProcessor.date_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="space-config.QueryProcessor.time_format">HH:mm:ss</prop>

These space properties should be configured with a valid Java format pattern as defined in the official Java language documentation.

The space-config.QueryProcessor.date_format property used when your query include a String representing the date Date properties are often used for comparison (greater/less than). Consider using extended indexing to boost performance.


The sysdate value is evaluated differently when using the JDBC API vs when using it with SQLQuery API. When used with JDBC API it is evaluated using the space clock. When used with SQLQuery API it is evaluated using the client clock. If you have a partitioned space across multiple different machines and the clock across these machines is not synchronized you might not get the desired results. If you use JDBC API you should consider setting the date value as part of the SQL within the client side (since you might write objects using the GigaSpace API). In this case , you should synchronize all the client machine time. In short - all the machines (client and server) clocks should be synchronized.

  • On windows there is a windows service that deals with clock synchronization.
  • On Linux there is a daemon service that deals with clock synchronization.

GigaSpaces using internally the TimeStamp data type to store dates. This means the date includes beyond the year, month and day, the hour/min/sec portions. If you are looking to query for a specific date you should perform a date range query.

Blocking Operations

Blocking operations (i.e. read or take with timeout greater than 0) are supported with the following restrictions:

  • Blocking operations on a partitioned space require a routing value (broadcast is not supported). For more information see Routing.
  • Blocking operations on complex queries are not supported. For more information see Simple Queries definition.
long timeout = 100000;
MyClass result = space.take(new SQLQuery<MyClass>(MyClass.class ,"num > 500"), timeout);


When running on a partitioned space, it is important to understand how routing is determined for SQL queries.

If the routing property is part of the criteria expression with an equality operand and without ORs, its value is used for routing.

For example, suppose the routing property of MyClass is num:

// Execute query on partition #1
SQLQuery<MyClass> query1 = new SQLQuery<MyClass>(MyClass.class,"num = 1");

// Execute query on all partitions - no way to tell which partitions hold matching results:
SQLQuery<MyClass> query2 = new SQLQuery<MyClass>(MyClass.class,"num > 1");

// Execute query on all partitions - no way to tell which partitions hold matching results:
SQLQuery<MyClass> query3 = new SQLQuery<MyClass>(MyClass.class,"num = 1 OR name='smith'");

Note that in query1 the num property is used both for routing and matching.

In some scenarios we may want to execute the query on a specific partition without matching the routing property (e.g. blocking operation). This can be done via the setRouting method:

SQLQuery<MyClass> query = new SQLQuery<MyClass>(MyClass.class,"num > 3");
MyClass[] result = gigaspace.readMultiple(query);

Regular Expressions

You can query the space using the SQL like operator or Java Regular Expression Query syntax.

When using the SQL like operator you may use the following: % - match any string of any length (including zero length) _ - match on a single character

SQLQuery<MyClass> query = new SQLQuery<MyClass>(MyClass.class,"name like 'A%'")

Querying the space using the Java Regular Expression provides more options than the SQL like operator. The Query syntax is done using the rlike operator:

// Match all entries of type MyClass that have a name that starts with a or c:
SQLQuery<MyClass> query = new SQLQuery<MyClass>(MyClass.class,"name rlike '(a|c).*'");

When you search for space objects with String fields that includes a single quote your query should use Parameterized Query - with the following we are searching for all Data objects that include the value today's with their myTextField:

String queryStr = "myTextField rlike ?";
SQLQuery<Data> query = new SQLQuery<Data>(Data.class, queryStr);
query.setParameter(1, "(today\u0027s)");
Data ret[] = space.readMultiple(query);

All the supported methods and options above are relevant also for using rlike with SQLQuery.


like and rlike queries are not using indexed data, hence executing such may be relatively time consuming compared to other queries that do leverage indexed data. This means the space engine iterate the potential candidate list to find matching object using the Java regular expression utilizes. A machine using 3GHz CPU may iterate 100,000-200,000 objects per second when executing regular expression query. To speed up like and rlike queries make sure your query leveraging also at least one indexed field to minimize the candidate list. Running multiple partitions will also speed up the query execution since this will allow the system to iterate over the potential matching objects in a parallel manner across the different partitions.

Free Text Search

Free text search required almost with every application. Users placing some free text into a form and later the system allows users to search for records that includes one or more words within a free text field. A simple way to enable such fast search without using regular expression query that my have some overhead can be done using the Collection Indexing, having an array or a collection of String values used for the query. Once the query is executed the SQL Query should use the searched words as usual. See example below:

Our Space class includes the following - note the words and the freeText fields:

public class MyData {
    String[] words;
    String freeText;

    @SpaceIndex (path="[*]")
    public String[] getWords() {
        return words;

    public void setWords(String words[]) {

    public String getFreeText() {
        return freeText;
    public void setFreeText(String freeText) {
        this.freeText = freeText;
        this.words = freeText.split(" ");

Note how the freeText field is broken into the words array before placed into the indexed field.

You may write the data into the space using the following:

MyData data = new MyPOJO(...);

You can query for objects having the word hello as part of the freeText field using the following:

MyData results[] = gigaspace.readMultiple(new SQLQuery<MyData>(MyData.class, words[*]='hello'));

You can also execute the following to search for object having the within the freeText field the word hello or everyone:

MyData results[] = gigaspace.readMultiple(new SQLQuery<MyData>(MyData.class, words[*]='hello' OR words[*]='everyone'));

With the above approach you avoid the overhead with regular expression queries.


The same approach can be implemented also with the SpaceDocument.

Case Insensitive Query

Implementing case insensitive queries can be done via:

  • like operator or rlike operator. Relatively slow. Not recommended when having large amount of objects.
  • Store the data in lower case and query on via lower case String value (or upper case)

Best Practice

Compound Index

When having an AND query or a template that use two or more fields for matching a Compound Index may boost the query execution time. The Compound Index should be defined on multiple properties for a specific space class and will be used implicitly when a SQL Query or a Template will be using these properties.

Re-using SQLQuery

Constructing an SQLQuery instance is a relatively expensive operation. When possible, prefer using SQLQuery.setParameters and SQLQuery.setParameter to modify an existing query instead of creating a new one. However, remember that SQLQuery is not thread-safe.

Minimize OR usage

When using the OR logical operator together with AND logical operator as part of your query you can speed up the query execution by minimizing the number of OR conditions in the query. For example:

(A = 'X' OR A = 'Y') AND (B > '2000-10-1' AND B < '2003-11-1')

would be executed much faster when changing it to be:

(A = 'X' AND B > '2000-10-1' AND B < '2003-11-1')
(A = 'Y' AND B > '2000-10-1' AND B < '2003-11-1')

Projecting Partial Results

You can specify that the SQLQuery should contain only partial results which means that the returned object should only be populated with the projected properties.

See also:
For details on how to use the projection API please refer to Getting Partial Results Using Projection API

Unsupported SQL Features

SQLQuery does not support the following:
  • Aggregate functions: COUNT, MAX, MIN, SUM, AVG are only supported in sub queries (These are fully supported with the JDBC API).
  • Multiple tables select - This is supported with the JDBC API.
  • DISTINCT - This is supported with the JDBC API.
  • Set operations: Union, Minus, Union All.
  • Advanced Aggregate Functions: STDEV, STDEVP, VAR, VARP, FIRST, LAST.
  • Mathematical expressions.

Simple vs. Complex Queries

Most space operations and features support any SQL query, but some support only simple queries and not complex ones.

A query is considered complex if it contains one or more of the following: - GROUP BY - ORDER BY - Sub queries

The following features support only simple SQL queries

Interface Classes

SQLQuery supports concrete classes, derived classes and abstract classes. Interface classes are not supported.

Reserved Words

The following are reserved keywords in the GigaSpaces SQL syntax:

Reserved words

alter add all and asc avg between by create call drop desc bit tinyint end from group in is like rlike max min not null or distinct order select substr sum sysdate upper where count delete varchar2 char exception rownum index insert into set table to_char to_number smallint update union values commit rollback uid using as date datetime time float real double number decimal numeric boolean integer varchar bigint long clob blob lob true false int timestamp longvarchar

If a reserved word needs to be used as a property name it needs to be escaped using ``. For example: if you need to query a property by the name of count, which is a reserved word, it can be done as following:

new SQLQuery<MyData>(MyData.class, "`count` = 5")
Reserved Separators and Operators:

:= || ; . ROWTYPE ~ < <= > >= => != <> (+) ( ) * / + - ? { }