MySQL NDB Cluster API Developer Guide

4.3.1.17 Query

A Query instance represents a specific query with bound parameters. The instance is created by the method com.mysql.clusterj.Session.<T>createQuery(com.mysql.clusterj.query.QueryDefinition<T>).

4.3.1.17.1 Synopsis
 public interface Query<E> {
// Public Static Fields  public static final String INDEX_USED = "IndexUsed";
  public static final String SCAN_TYPE = "ScanType";
  public static final String SCAN_TYPE_INDEX_SCAN = "INDEX_SCAN";
  public static final String SCAN_TYPE_PRIMARY_KEY = "PRIMARY_KEY";
  public static final String SCAN_TYPE_TABLE_SCAN = "TABLE_SCAN";
  public static final String SCAN_TYPE_UNIQUE_KEY = "UNIQUE_KEY";
// Public Methods  public abstract int deletePersistentAll();
  public abstract Results<E> execute(Object parameter);
  public abstract Results<E> execute(Object[] parameters);
  public abstract Results<E> execute(Map<String, ?> parameters);
  public abstract Map<String, Object> explain();
  public abstract List<E> getResultList();
  public abstract void setLimits(long skip,
                                 long limit);

  public abstract void setOrdering(Ordering ordering,
                                   String[] orderingFields);

  public abstract void setParameter(String parameterName,
                                    Object value);

}
4.3.1.17.2 INDEX_USED
public static final String INDEX_USED = "IndexUsed";

The query explain index used key

4.3.1.17.3 SCAN_TYPE
public static final String SCAN_TYPE = "ScanType";

The query explain scan type key

4.3.1.17.4 SCAN_TYPE_INDEX_SCAN
public static final String SCAN_TYPE_INDEX_SCAN = "INDEX_SCAN";

The query explain scan type value for index scan

4.3.1.17.5 SCAN_TYPE_PRIMARY_KEY
public static final String SCAN_TYPE_PRIMARY_KEY = "PRIMARY_KEY";

The query explain scan type value for primary key

4.3.1.17.6 SCAN_TYPE_TABLE_SCAN
public static final String SCAN_TYPE_TABLE_SCAN = "TABLE_SCAN";

The query explain scan type value for table scan

4.3.1.17.7 SCAN_TYPE_UNIQUE_KEY
public static final String SCAN_TYPE_UNIQUE_KEY = "UNIQUE_KEY";

The query explain scan type value for unique key

4.3.1.17.8 deletePersistentAll()
public abstract int deletePersistentAll();

Delete the instances that satisfy the query criteria.

Table 4.35 deletePersistentAll()

Parameter Description
return the number of instances deleted

4.3.1.17.9 execute(Map<String, ?>)
public abstract Results<E> execute(Map<String, ?> parameters);

Execute the query with one or more named parameters. Parameters are resolved by name.

Table 4.36 execute(Map<String, ?>)

Parameter Description
parameters the parameters
return the result

4.3.1.17.10 execute(Object...)
public abstract Results<E> execute(Object[] parameters);

Execute the query with one or more parameters. Parameters are resolved in the order they were declared in the query.

Table 4.37 execute(Object...)

Parameter Description
parameters the parameters
return the result

4.3.1.17.11 execute(Object)
public abstract Results<E> execute(Object parameter);

Execute the query with exactly one parameter.

Table 4.38 execute(Object)

Parameter Description
parameter the parameter
return the result

4.3.1.17.12 explain()
public abstract Map<String, Object> explain();

Explain how this query will be or was executed. If called before binding all parameters, throws ClusterJUserException. Return a map of key:value pairs that explain how the query will be or was executed. Details can be obtained by calling toString on the value. The following keys are returned:

  • ScanType: the type of scan, with values:

    • PRIMARY_KEY: the query used key lookup with the primary key

    • UNIQUE_KEY: the query used key lookup with a unique key

    • INDEX_SCAN: the query used a range scan with a non-unique key

    • TABLE_SCAN: the query used a table scan

  • IndexUsed: the name of the index used, if any

Table 4.39 explain()

Parameter Description
return the data about the execution of this query

Exceptions

ClusterJUserException

if not all parameters are bound

4.3.1.17.13 getResultList()
public abstract List<E> getResultList();

Get the results as a list.

Table 4.40 getResultList()

Parameter Description
return the result

Exceptions

ClusterJUserException

if not all parameters are bound

ClusterJDatastoreException

if an exception is reported by the datastore

4.3.1.17.14 setLimits(long, long)
public abstract void setLimits(long skip,
                               long limit);

Set limits on results to return. The execution of the query is modified to return only a subset of results. If the filter would normally return 100 instances, skip is set to 50, and limit is set to 40, then the first 50 results that would have been returned are skipped, the next 40 results are returned and the remaining 10 results are ignored.

The parameter skip must be greater than or equal to 0 (see an exception below) and limit must be greater than or equal to 0.

When used with deletePersistentAll, skip must be 0, and the instances should be deleted iteratively until the count of deleted instances is less than the batch size. For example:

/* Delete in batches */ 
query.setLimits(0, DeleteBatchSize);
int result = 0;
do { result = query.deletePersistentAll();
 System.out.println("Batch result: " + result);
 } while(result == DeleteBatchSize); 

Table 4.41 setLimits(long, long)

Parameter Description
skip the number of results to skip
limit the number of results to return after skipping; use Long.MAX_VALUE for no limit.

4.3.1.17.15 setOrdering(Query.Ordering, String...)
public abstract void setOrdering(Ordering ordering,
                                 String[] orderingFields);

Set ordering for the results of this query. The execution of the query is modified to use an index previously defined.

  • There must be an index defined on the columns mapped to the ordering fields, in the order of the ordering fields.

  • There must be no gaps in the ordering fields relative to the index.

  • All ordering fields must be in the index, but not all fields in the index need be in the ordering fields.

  • If an "in" predicate is used in the filter on a field in the ordering, it can only be used with the first field.

  • If any of these conditions is violated, ClusterJUserException is thrown when the query is executed.

If an "in" predicate is used, each element in the parameter defines a separate range, and ordering is performed within that range. There may be a better (more efficient) index based on the filter, but specifying the ordering will force the query to use an index that contains the ordering fields.

Table 4.42 setOrdering(Query.Ordering, String...)

Parameter Description
ordering either Ordering.ASCENDING or Ordering.DESCENDING
orderingFields the fields to order by

4.3.1.17.16 setParameter(String, Object)
public abstract void setParameter(String parameterName,
                                  Object value);

Set the value of a parameter. If called multiple times for the same parameter, silently replace the value.

Table 4.43 setParameter(String, Object)

Parameter Description
parameterName the name of the parameter
value the value for the parameter