提交 2c0dba44 authored 作者: Thomas Mueller's avatar Thomas Mueller

Queries with DISTINCT and ORDER BY will now use the index on ORDER BY if possible.

上级 5f3b5d85
...@@ -18,7 +18,9 @@ Change Log ...@@ -18,7 +18,9 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>The new statement EXPLAIN ANALYZE executes the statement, <ul><li>Queries with DISTINCT and ORDER BY will now use the index on ORDER BY if possible.
This is specially important for queries used inside IN(SELECT ...).
</li><li>The new statement EXPLAIN ANALYZE executes the statement,
and displays the query plan with the actual row scan count for each table. and displays the query plan with the actual row scan count for each table.
</li><li>H2 Console: the auto-complete feature didn't quote column names </li><li>H2 Console: the auto-complete feature didn't quote column names
that need quoting. Issue 186. that need quoting. Issue 186.
......
...@@ -531,7 +531,7 @@ public class Select extends Query { ...@@ -531,7 +531,7 @@ public class Select extends Query {
} }
int columnCount = expressions.size(); int columnCount = expressions.size();
LocalResult result = new LocalResult(session, expressionArray, visibleColumnCount); LocalResult result = new LocalResult(session, expressionArray, visibleColumnCount);
if (!sortUsingIndex) { if (!sortUsingIndex || distinct) {
result.setSortOrder(sort); result.setSortOrder(sort);
} }
if (distinct && !isDistinctQuery) { if (distinct && !isDistinctQuery) {
...@@ -754,10 +754,8 @@ public class Select extends Query { ...@@ -754,10 +754,8 @@ public class Select extends Query {
Index current = topTableFilter.getIndex(); Index current = topTableFilter.getIndex();
if (index != null && (current.getIndexType().isScan() || current == index)) { if (index != null && (current.getIndexType().isScan() || current == index)) {
topTableFilter.setIndex(index); topTableFilter.setIndex(index);
if ((!distinct || isDistinctQuery) && (!topTableFilter.hasInComparisons())) { if (!topTableFilter.hasInComparisons()) {
// - sort using index would not work correctly for distinct result sets // in(select ...) and in(1,2,3) my return the key is another order
// because it would break too early when limit is used
// - in(select ...) and in(1,2,3) indices are unsorted
sortUsingIndex = true; sortUsingIndex = true;
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论