提交 6257fb79 authored 作者: noelgrandin's avatar noelgrandin

Spatial Index: adjust costs so we do not use the spatial index if the query does…

Spatial Index: adjust costs so we do not use the spatial index if the query does not contain an intersects operator.
上级 a0343727
...@@ -175,9 +175,19 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -175,9 +175,19 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
@Override @Override
protected long getCostRangeIndex(int[] masks, long rowCount, protected long getCostRangeIndex(int[] masks, long rowCount,
TableFilter filter, SortOrder sortOrder) { TableFilter filter, SortOrder sortOrder) {
return getCostRangeIndex(masks, rowCount, columns);
}
/**
* Compute spatial index cost
* @param masks Search mask
* @param rowCount Table row count
* @param columns Table columns
* @return Index cost hint
*/
public static long getCostRangeIndex(int[] masks, long rowCount, Column[] columns) {
rowCount += Constants.COST_ROW_OFFSET; rowCount += Constants.COST_ROW_OFFSET;
long cost = rowCount; long cost = rowCount;
long rows = rowCount;
if (masks == null) { if (masks == null) {
return cost; return cost;
} }
...@@ -185,10 +195,10 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -185,10 +195,10 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
int index = column.getColumnId(); int index = column.getColumnId();
int mask = masks[index]; int mask = masks[index];
if ((mask & IndexCondition.SPATIAL_INTERSECTS) != 0) { if ((mask & IndexCondition.SPATIAL_INTERSECTS) != 0) {
cost = 3 + rows / 4; cost = 3 + rowCount / 4;
} }
} }
return cost; return 10 * cost;
} }
@Override @Override
......
...@@ -10,14 +10,13 @@ import java.util.Iterator; ...@@ -10,14 +10,13 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.index.BaseIndex; import org.h2.index.BaseIndex;
import org.h2.index.Cursor; import org.h2.index.Cursor;
import org.h2.index.IndexCondition;
import org.h2.index.IndexType; import org.h2.index.IndexType;
import org.h2.index.SpatialIndex; import org.h2.index.SpatialIndex;
import org.h2.index.SpatialTreeIndex;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.db.TransactionStore.Transaction; import org.h2.mvstore.db.TransactionStore.Transaction;
import org.h2.mvstore.db.TransactionStore.TransactionMap; import org.h2.mvstore.db.TransactionStore.TransactionMap;
...@@ -29,7 +28,6 @@ import org.h2.mvstore.rtree.SpatialKey; ...@@ -29,7 +28,6 @@ import org.h2.mvstore.rtree.SpatialKey;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
import org.h2.table.Column;
import org.h2.table.IndexColumn; import org.h2.table.IndexColumn;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.value.Value; import org.h2.value.Value;
...@@ -257,19 +255,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -257,19 +255,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
@Override @Override
protected long getCostRangeIndex(int[] masks, long rowCount, protected long getCostRangeIndex(int[] masks, long rowCount,
TableFilter filter, SortOrder sortOrder) { TableFilter filter, SortOrder sortOrder) {
rowCount += Constants.COST_ROW_OFFSET; return SpatialTreeIndex.getCostRangeIndex(masks, rowCount, columns);
long cost = rowCount;
if (masks == null) {
return cost;
}
for (Column column : columns) {
int index = column.getColumnId();
int mask = masks[index];
if ((mask & IndexCondition.SPATIAL_INTERSECTS) != 0) {
cost = 3 + rowCount / 4;
}
}
return cost;
} }
@Override @Override
......
...@@ -87,6 +87,7 @@ public class TestSpatial extends TestBase { ...@@ -87,6 +87,7 @@ public class TestSpatial extends TestBase {
testTableViewSpatialPredicate(); testTableViewSpatialPredicate();
testValueGeometryScript(); testValueGeometryScript();
testInPlaceUpdate(); testInPlaceUpdate();
testScanIndexOnNonSpatialQuery();
} }
private void testHashCode() { private void testHashCode() {
...@@ -830,4 +831,22 @@ public class TestSpatial extends TestBase { ...@@ -830,4 +831,22 @@ public class TestSpatial extends TestBase {
conn.close(); conn.close();
} }
} }
private void testScanIndexOnNonSpatialQuery() throws SQLException {
deleteDb("spatial");
Connection conn = getConnection(url);
try {
Statement stat = conn.createStatement();
stat.execute("drop table if exists test");
stat.execute("create table test(id serial primary key, value double, the_geom geometry)");
stat.execute("create spatial index spatial on test(the_geom)");
ResultSet rs = stat.executeQuery("explain select * from test where _ROWID_ = 5");
assertTrue(rs.next());
assertContains(rs.getString(1), "tableScan");
} finally {
// Close the database
conn.close();
}
deleteDb("spatial");
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论