Skip to content

Commit

Permalink
Queries optimizations:
Browse files Browse the repository at this point in the history
- cache prepared statements and two-step queries
- skip reduce query if possible
 - do not use index snapshots by default
  • Loading branch information
sboikov committed Nov 26, 2015
1 parent 3c42da8 commit 4af461a
Show file tree
Hide file tree
Showing 21 changed files with 776 additions and 213 deletions.
Expand Up @@ -373,6 +373,9 @@ public class CacheConfiguration<K, V> extends MutableConfiguration<K, V> {
/** */ /** */
private int sqlOnheapRowCacheSize = DFLT_SQL_ONHEAP_ROW_CACHE_SIZE; private int sqlOnheapRowCacheSize = DFLT_SQL_ONHEAP_ROW_CACHE_SIZE;


/** */
private boolean snapshotableIdx;

/** Copy on read flag. */ /** Copy on read flag. */
private boolean cpOnRead = DFLT_COPY_ON_READ; private boolean cpOnRead = DFLT_COPY_ON_READ;


Expand Down Expand Up @@ -463,6 +466,7 @@ public CacheConfiguration(CompleteConfiguration<K, V> cfg) {
rebalancePoolSize = cc.getRebalanceThreadPoolSize(); rebalancePoolSize = cc.getRebalanceThreadPoolSize();
rebalanceTimeout = cc.getRebalanceTimeout(); rebalanceTimeout = cc.getRebalanceTimeout();
rebalanceThrottle = cc.getRebalanceThrottle(); rebalanceThrottle = cc.getRebalanceThrottle();
snapshotableIdx = cc.isSnapshotableIndex();
sqlEscapeAll = cc.isSqlEscapeAll(); sqlEscapeAll = cc.isSqlEscapeAll();
sqlFuncCls = cc.getSqlFunctionClasses(); sqlFuncCls = cc.getSqlFunctionClasses();
sqlOnheapRowCacheSize = cc.getSqlOnheapRowCacheSize(); sqlOnheapRowCacheSize = cc.getSqlOnheapRowCacheSize();
Expand Down Expand Up @@ -1899,6 +1903,32 @@ public CacheConfiguration<K, V> setSqlOnheapRowCacheSize(int size) {
return this; return this;
} }


/**
* Gets flag indicating whether SQL indexes should support snapshots.
*
* @return {@code True} if SQL indexes should support snapshots.
*/
public boolean isSnapshotableIndex() {
return snapshotableIdx;
}

/**
* Sets flag indicating whether SQL indexes should support snapshots.
* <p>
* Default value is {@code false}.
* <p>
* <b>Note</b> that this flag is ignored if indexes are stored in offheap memory,
* for offheap indexes snapshots are always enabled.
*
* @param snapshotableIdx {@code True} if SQL indexes should support snapshots.
* @return {@code this} for chaining.
*/
public CacheConfiguration<K, V> setSnapshotableIndex(boolean snapshotableIdx) {
this.snapshotableIdx = snapshotableIdx;

return this;
}

/** /**
* Gets array of cache plugin configurations. * Gets array of cache plugin configurations.
* *
Expand Down
Expand Up @@ -52,6 +52,16 @@ public class GridCacheSqlQuery implements Message {
/** */ /** */
private byte[] paramsBytes; private byte[] paramsBytes;


/** */
@GridToStringInclude
@GridDirectTransient
private int[] paramIdxs;

/** */
@GridToStringInclude
@GridDirectTransient
private int paramsSize;

/** */ /** */
@GridToStringInclude @GridToStringInclude
@GridDirectTransient @GridDirectTransient
Expand All @@ -77,6 +87,14 @@ public GridCacheSqlQuery(String qry, Object[] params) {
this.qry = qry; this.qry = qry;


this.params = F.isEmpty(params) ? EMPTY_PARAMS : params; this.params = F.isEmpty(params) ? EMPTY_PARAMS : params;
paramsSize = this.params.length;
}

/**
* @param paramIdxs Parameter indexes.
*/
public void parameterIndexes(int[] paramIdxs) {
this.paramIdxs = paramIdxs;
} }


/** /**
Expand Down Expand Up @@ -222,4 +240,28 @@ public void unmarshallParams(Marshaller m) throws IgniteCheckedException {
@Override public byte fieldsCount() { @Override public byte fieldsCount() {
return 3; return 3;
} }

/**
* @param args Arguments.
* @return Copy.
*/
public GridCacheSqlQuery copy(Object[] args) {
GridCacheSqlQuery cp = new GridCacheSqlQuery();

cp.qry = qry;
cp.cols = cols;
cp.paramIdxs = paramIdxs;
cp.paramsSize = paramsSize;

if (F.isEmpty(args))
cp.params = EMPTY_PARAMS;
else {
cp.params = new Object[paramsSize];

for (int paramIdx : paramIdxs)
cp.params[paramIdx] = args[paramIdx];
}

return cp;
}
} }
Expand Up @@ -47,16 +47,27 @@ public class GridCacheTwoStepQuery {
/** */ /** */
private Set<String> spaces; private Set<String> spaces;


/** */
private final boolean skipMergeTbl;

/** /**
* @param spaces All spaces accessed in query. * @param spaces All spaces accessed in query.
* @param rdc Reduce query. * @param rdc Reduce query.
* @param skipMergeTbl {@code True} if reduce query can skip merge table creation and
* get data directly from merge index.
*/ */
public GridCacheTwoStepQuery(Set<String> spaces, GridCacheSqlQuery rdc) { public GridCacheTwoStepQuery(Set<String> spaces, GridCacheSqlQuery rdc, boolean skipMergeTbl) {
assert rdc != null; assert rdc != null;


this.spaces = spaces; this.spaces = spaces;

this.rdc = rdc; this.rdc = rdc;
this.skipMergeTbl = skipMergeTbl;
}
/**
* @return {@code True} if reduce query can skip merge table creation and get data directly from merge index.
*/
public boolean skipMergeTable() {
return skipMergeTbl;
} }


/** /**
Expand Down Expand Up @@ -89,9 +100,12 @@ public int pageSize() {


/** /**
* @param qry SQL Query. * @param qry SQL Query.
* @return {@code this}.
*/ */
public void addMapQuery(GridCacheSqlQuery qry) { public GridCacheTwoStepQuery addMapQuery(GridCacheSqlQuery qry) {
mapQrys.add(qry); mapQrys.add(qry);

return this;
} }


/** /**
Expand Down Expand Up @@ -122,6 +136,21 @@ public void spaces(Set<String> spaces) {
this.spaces = spaces; this.spaces = spaces;
} }


/**
* @param args New arguments to copy with.
* @return Copy.
*/
public GridCacheTwoStepQuery copy(Object[] args) {
assert !explain;

GridCacheTwoStepQuery cp = new GridCacheTwoStepQuery(spaces, rdc.copy(args), skipMergeTbl);
cp.pageSize = pageSize;
for (int i = 0; i < mapQrys.size(); i++)
cp.mapQrys.add(mapQrys.get(i).copy(args));

return cp;
}

/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public String toString() { @Override public String toString() {
return S.toString(GridCacheTwoStepQuery.class, this); return S.toString(GridCacheTwoStepQuery.class, this);
Expand Down
Expand Up @@ -1671,35 +1671,10 @@ public static void onCompleted(GridCacheContext<?, ?> cctx, Object res, Throwabl
", duration=" + duration + ", fail=" + fail + ", res=" + res + ']'); ", duration=" + duration + ", fail=" + fail + ", res=" + res + ']');
} }


/**
*
*/
private abstract static class Property {
/**
* Gets this property value from the given object.
*
* @param key Key.
* @param val Value.
* @return Property value.
* @throws IgniteCheckedException If failed.
*/
public abstract Object value(Object key, Object val) throws IgniteCheckedException;

/**
* @return Property name.
*/
public abstract String name();

/**
* @return Class member type.
*/
public abstract Class<?> type();
}

/** /**
* Description of type property. * Description of type property.
*/ */
private static class ClassProperty extends Property { private static class ClassProperty extends GridQueryProperty {
/** */ /** */
private final Member member; private final Member member;


Expand Down Expand Up @@ -1794,7 +1769,7 @@ public boolean knowsClass(Class<?> cls) {
/** /**
* *
*/ */
private class PortableProperty extends Property { private class PortableProperty extends GridQueryProperty {
/** Property name. */ /** Property name. */
private String propName; private String propName;


Expand Down Expand Up @@ -1938,7 +1913,7 @@ private static class TypeDescriptor implements GridQueryTypeDescriptor {


/** */ /** */
@GridToStringExclude @GridToStringExclude
private final Map<String, Property> props = new HashMap<>(); private final Map<String, GridQueryProperty> props = new HashMap<>();


/** */ /** */
@GridToStringInclude @GridToStringInclude
Expand Down Expand Up @@ -1992,12 +1967,17 @@ void name(String name) {
return fields; return fields;
} }


/** {@inheritDoc} */
@Override public GridQueryProperty property(String name) {
return props.get(name);
}

/** {@inheritDoc} */ /** {@inheritDoc} */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override public <T> T value(String field, Object key, Object val) throws IgniteCheckedException { @Override public <T> T value(String field, Object key, Object val) throws IgniteCheckedException {
assert field != null; assert field != null;


Property prop = props.get(field); GridQueryProperty prop = props.get(field);


if (prop == null) if (prop == null)
throw new IgniteCheckedException("Failed to find field '" + field + "' in type '" + name + "'."); throw new IgniteCheckedException("Failed to find field '" + field + "' in type '" + name + "'.");
Expand Down Expand Up @@ -2096,7 +2076,7 @@ void keyClass(Class<?> keyCls) {
* @param failOnDuplicate Fail on duplicate flag. * @param failOnDuplicate Fail on duplicate flag.
* @throws IgniteCheckedException In case of error. * @throws IgniteCheckedException In case of error.
*/ */
public void addProperty(Property prop, boolean failOnDuplicate) throws IgniteCheckedException { public void addProperty(GridQueryProperty prop, boolean failOnDuplicate) throws IgniteCheckedException {
String name = prop.name(); String name = prop.name();


if (props.put(name, prop) != null && failOnDuplicate) if (props.put(name, prop) != null && failOnDuplicate)
Expand Down
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.internal.processors.query;

import org.apache.ignite.IgniteCheckedException;

/**
* Description and access method for query entity field.
*/
public abstract class GridQueryProperty {
/**
* Gets this property value from the given object.
*
* @param key Key.
* @param val Value.
* @return Property value.
* @throws IgniteCheckedException If failed.
*/
public abstract Object value(Object key, Object val) throws IgniteCheckedException;

/**
* @return Property name.
*/
public abstract String name();

/**
* @return Class member type.
*/
public abstract Class<?> type();
}
Expand Up @@ -49,6 +49,12 @@ public interface GridQueryTypeDescriptor {
*/ */
public <T> T value(String field, Object key, Object val) throws IgniteCheckedException; public <T> T value(String field, Object key, Object val) throws IgniteCheckedException;


/**
* @param name Property name.
* @return Property.
*/
public GridQueryProperty property(String name);

/** /**
* Gets indexes for this type. * Gets indexes for this type.
* *
Expand Down
Expand Up @@ -40,15 +40,20 @@ public abstract class GridH2ResultSetIterator<T> extends GridCloseableIteratorAd
/** */ /** */
protected final Object[] row; protected final Object[] row;


/** */
private final boolean closeStmt;

/** */ /** */
private boolean hasRow; private boolean hasRow;


/** /**
* @param data Data array. * @param data Data array.
* @param closeStmt If {@code true} closes result set statement when iterator is closed.
* @throws IgniteCheckedException If failed. * @throws IgniteCheckedException If failed.
*/ */
protected GridH2ResultSetIterator(ResultSet data) throws IgniteCheckedException { protected GridH2ResultSetIterator(ResultSet data, boolean closeStmt) throws IgniteCheckedException {
this.data = data; this.data = data;
this.closeStmt = closeStmt;


if (data != null) { if (data != null) {
try { try {
Expand Down Expand Up @@ -115,11 +120,13 @@ private boolean fetchNext() {
// Nothing to close. // Nothing to close.
return; return;


try { if (closeStmt) {
U.closeQuiet(data.getStatement()); try {
} U.closeQuiet(data.getStatement());
catch (SQLException e) { }
throw new IgniteCheckedException(e); catch (SQLException e) {
throw new IgniteCheckedException(e);
}
} }


U.closeQuiet(data); U.closeQuiet(data);
Expand Down

0 comments on commit 4af461a

Please sign in to comment.