提交 c097b6e2 authored 作者: shuji.w6e's avatar shuji.w6e

issue 178

上级 0842364b
......@@ -776,13 +776,32 @@ public class Select extends Query {
}
if (sort != null && !isQuickAggregateQuery && !isGroupQuery) {
Index index = getSortIndex();
Index current = topTableFilter.getIndex();
if (index != null && (current.getIndexType().isScan() || current == index)) {
topTableFilter.setIndex(index);
if (!topTableFilter.hasInComparisons()) {
// in(select ...) and in(1,2,3) my return the key is another order
sortUsingIndex = true;
}
if (index != null) {
Index current = topTableFilter.getIndex();
if (current.getIndexType().isScan() || current == index) {
topTableFilter.setIndex(index);
if (!topTableFilter.hasInComparisons()) {
// in(select ...) and in(1,2,3) my return the key is another order
sortUsingIndex = true;
}
} else if (index.getIndexColumns().length >= current.getIndexColumns().length) {
IndexColumn[] ic = index.getIndexColumns();
IndexColumn[] cic = current.getIndexColumns();
boolean swapIndex = false;
for (int i = 0; i < cic.length; i++) {
if (ic[i].column != cic[i].column) {
swapIndex = false;
break;
}
if (ic[i].sortType != cic[i].sortType) {
swapIndex = true;
}
}
if (swapIndex) {
topTableFilter.setIndex(index);
sortUsingIndex = true;
}
}
}
}
if (!isQuickAggregateQuery && isGroupQuery && getGroupByExpressionCount() > 0) {
......
......@@ -37,6 +37,7 @@ public class TestOptimizations extends TestBase {
}
public void test() throws Exception {
testSortIndex();
testAutoAnalyze();
testInAndBetween();
testNestedIn();
......@@ -61,6 +62,28 @@ public class TestOptimizations extends TestBase {
deleteDb("optimizations");
}
private void testSortIndex() throws SQLException {
Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
stat.execute("drop table test if exists");
stat.execute("create table test(id int)");
stat.execute("create index idx_id_desc on test(id desc)");
stat.execute("create index idx_id_asc on test(id)");
ResultSet rs;
rs = stat.executeQuery("explain select * from test where id > 10 order by id");
rs.next();
assertTrue(rs.getString(1).indexOf("IDX_ID_ASC") >= 0);
rs = stat.executeQuery("explain select * from test where id < 10 order by id desc");
rs.next();
assertTrue(rs.getString(1).indexOf("IDX_ID_DESC") >= 0);
rs.next();
stat.execute("drop table test");
conn.close();
}
private void testAutoAnalyze() throws SQLException {
int auto = SysProperties.ANALYZE_AUTO;
if (auto == 0) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论