1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-16 23:02:26 +03:00

Disable an overzealous optimization the omitted sorting on a join if the first

table gave a unique result.  The sort can only be omitted if all tables in
the join are unique.  Ticket #1358. (CVS 2589)

FossilOrigin-Name: 4f07661279fb11a06b3ddffeda672f077c0d306a
This commit is contained in:
drh
2005-08-13 16:13:04 +00:00
parent 531fe87834
commit 8718f52624
5 changed files with 64 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
C Fix\sa\scomment\sin\sprintf.\s(CVS\s2588) C Disable\san\soverzealous\soptimization\sthe\somitted\ssorting\son\sa\sjoin\sif\sthe\sfirst\ntable\sgave\sa\sunique\sresult.\s\sThe\ssort\scan\sonly\sbe\somitted\sif\sall\stables\sin\nthe\sjoin\sare\sunique.\s\sTicket\s#1358.\s(CVS\s2589)
D 2005-08-13T13:40:42 D 2005-08-13T16:13:05
F Makefile.in 22ea9c0fe748f591712d8fe3c6d972c6c173a165 F Makefile.in 22ea9c0fe748f591712d8fe3c6d972c6c173a165
F Makefile.linux-gcc 06be33b2a9ad4f005a5f42b22c4a19dab3cbb5c7 F Makefile.linux-gcc 06be33b2a9ad4f005a5f42b22c4a19dab3cbb5c7
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -86,7 +86,7 @@ F src/vdbeapi.c dc5b78cabf8d6e33318bd3d4ed25307d2aadce9a
F src/vdbeaux.c d53139d819b887dac608ac4ae9a501baee3cd311 F src/vdbeaux.c d53139d819b887dac608ac4ae9a501baee3cd311
F src/vdbefifo.c b8805850afe13b43f1de78d58088cb5d66f88e1e F src/vdbefifo.c b8805850afe13b43f1de78d58088cb5d66f88e1e
F src/vdbemem.c 89154caae3b8d4d0397e1235390fc4ff8aba4233 F src/vdbemem.c 89154caae3b8d4d0397e1235390fc4ff8aba4233
F src/where.c ac754c021f716e17337f45ffdc2436c6d5109fd3 F src/where.c 27d6432ea5fa255008ef80cb1d8e5e58ea8d615f
F tclinstaller.tcl 046e3624671962dc50f0481d7c25b38ef803eb42 F tclinstaller.tcl 046e3624671962dc50f0481d7c25b38ef803eb42
F test/all.test 7f0988442ab811dfa41793b5b550f5828ce316f3 F test/all.test 7f0988442ab811dfa41793b5b550f5828ce316f3
F test/alter.test 9d6837a3d946b73df692b7cef2a7644d2e2f6bc6 F test/alter.test 9d6837a3d946b73df692b7cef2a7644d2e2f6bc6
@@ -198,7 +198,7 @@ F test/select4.test c239f516aa31f42f2ef7c6d7cd01105f08f934ca
F test/select5.test 2d414f712bff8e590091e08f9b7287600731be00 F test/select5.test 2d414f712bff8e590091e08f9b7287600731be00
F test/select6.test 6559d16ad16edb7d6864f7e74a3d204d0af72486 F test/select6.test 6559d16ad16edb7d6864f7e74a3d204d0af72486
F test/select7.test 1bf795b948c133a15a2a5e99d3270e652ec58ce6 F test/select7.test 1bf795b948c133a15a2a5e99d3270e652ec58ce6
F test/sort.test 131787aadaa0eca8bc97eaf244bbde2380a4bb77 F test/sort.test 3b871d6e032f0a6c84d9f3d2d4b226e8fda97de0
F test/subquery.test 0df3de0dbb65165b96ebe895550f1549d5439856 F test/subquery.test 0df3de0dbb65165b96ebe895550f1549d5439856
F test/subselect.test 3f3f7a940dc3195c3139f4d530385cb54665d614 F test/subselect.test 3f3f7a940dc3195c3139f4d530385cb54665d614
F test/table.test d0e05ede3f6e5a8b79f8661ddcc4618cf7e69f8a F test/table.test d0e05ede3f6e5a8b79f8661ddcc4618cf7e69f8a
@@ -228,7 +228,7 @@ F test/vacuum2.test 5d77e98c458bcdbeecc6327de5107179ba1aa095
F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102 F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102
F test/view.test 3c79232a2ee45918c62a0cf90411525899404a76 F test/view.test 3c79232a2ee45918c62a0cf90411525899404a76
F test/where.test b6ab0f64adc5fbb4259f284b19da6cd9aeadc711 F test/where.test b6ab0f64adc5fbb4259f284b19da6cd9aeadc711
F test/where2.test 3432fc9c24af9120df65a9a4cc8dad3bb9285633 F test/where2.test 503e2e2b6abe14c5c10222e72d08ef84c1bf1ffb
F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b
F tool/lemon.c c88936c67f6411608db8fa4254d254f509fa40f6 F tool/lemon.c c88936c67f6411608db8fa4254d254f509fa40f6
F tool/lempar.c f0c30abcae762a7d1eb37cd88b2232ab8dd625fb F tool/lempar.c f0c30abcae762a7d1eb37cd88b2232ab8dd625fb
@@ -291,7 +291,7 @@ F www/tclsqlite.tcl 3df553505b6efcad08f91e9b975deb2e6c9bb955
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
F www/whentouse.tcl 528299b8316726dbcc5548e9aa0648c8b1bd055b F www/whentouse.tcl 528299b8316726dbcc5548e9aa0648c8b1bd055b
P 240bb049001b0d1419d72b6ef909236e12bd5949 P 1054685f15095ef147d9e9a32bd56b0eaaf9fa54
R c4c4f129cf45073166872f89ead87e29 R d225fbaf33bc549b919b820e573eeddf
U drh U drh
Z 355afc4615017b256e4a9bb53eaadeae Z ca2afe75bb06779be079d2484ddb7fa4

View File

@@ -1 +1 @@
1054685f15095ef147d9e9a32bd56b0eaaf9fa54 4f07661279fb11a06b3ddffeda672f077c0d306a

View File

@@ -16,7 +16,7 @@
** so is applicable. Because this module is responsible for selecting ** so is applicable. Because this module is responsible for selecting
** indices, you might also think of this module as the "query optimizer". ** indices, you might also think of this module as the "query optimizer".
** **
** $Id: where.c,v 1.160 2005/08/12 22:56:09 drh Exp $ ** $Id: where.c,v 1.161 2005/08/13 16:13:05 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
@@ -719,10 +719,6 @@ or_not_possible:
** constraints. Any of these columns may be missing from the ORDER BY ** constraints. Any of these columns may be missing from the ORDER BY
** clause and the match can still be a success. ** clause and the match can still be a success.
** **
** If the index is UNIQUE, then the ORDER BY clause is allowed to have
** additional terms past the end of the index and the match will still
** be a success.
**
** All terms of the ORDER BY that match against the index must be either ** All terms of the ORDER BY that match against the index must be either
** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE ** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
** index do not need to satisfy this constraint.) The *pbRev value is ** index do not need to satisfy this constraint.) The *pbRev value is
@@ -791,10 +787,9 @@ static int isSortingIndex(
} }
/* The index can be used for sorting if all terms of the ORDER BY clause /* The index can be used for sorting if all terms of the ORDER BY clause
** or covered or if we ran out of index columns and the it is a UNIQUE ** are covered.
** index.
*/ */
if( j>=nTerm || (i>=pIdx->nColumn && pIdx->onError!=OE_None) ){ if( j>=nTerm ){
*pbRev = sortOrder==SQLITE_SO_DESC; *pbRev = sortOrder==SQLITE_SO_DESC;
return 1; return 1;
} }
@@ -829,14 +824,12 @@ static int sortableByRowid(
** the total cost of performing operatings with O(logN) or O(NlogN) ** the total cost of performing operatings with O(logN) or O(NlogN)
** complexity. Because N is just a guess, it is no great tragedy if ** complexity. Because N is just a guess, it is no great tragedy if
** logN is a little off. ** logN is a little off.
**
** We can assume N>=1.0;
*/ */
static double estLog(double N){ static double estLog(double N){
double logN = 1.0; double logN = 1.0;
double x = 10.0; double x = 10.0;
while( N>x ){ while( N>x ){
logN = logN+1.0; logN += 1.0;
x *= 10; x *= 10;
} }
return logN; return logN;
@@ -1013,7 +1006,7 @@ static double bestIndex(
*/ */
if( pOrderBy ){ if( pOrderBy ){
if( (flags & WHERE_COLUMN_IN)==0 && if( (flags & WHERE_COLUMN_IN)==0 &&
isSortingIndex(pParse, pProbe, pSrc->pTab, iCur, pOrderBy, nEq, &rev) ){ isSortingIndex(pParse,pProbe,pSrc->pTab,iCur,pOrderBy,nEq,&rev) ){
if( flags==0 ){ if( flags==0 ){
flags = WHERE_COLUMN_RANGE; flags = WHERE_COLUMN_RANGE;
} }

View File

@@ -11,7 +11,7 @@
# This file implements regression tests for SQLite library. The # This file implements regression tests for SQLite library. The
# focus of this file is testing the CREATE TABLE statement. # focus of this file is testing the CREATE TABLE statement.
# #
# $Id: sort.test,v 1.21 2005/08/13 12:59:16 drh Exp $ # $Id: sort.test,v 1.22 2005/08/13 16:13:06 drh Exp $
set testdir [file dirname $argv0] set testdir [file dirname $argv0]
source $testdir/tester.tcl source $testdir/tester.tcl
@@ -431,4 +431,20 @@ do_test sort-10.3 {
} }
} {2 1} } {2 1}
# ticket #1358. Just because one table in a join gives a unique
# result does not mean they all do. We cannot disable sorting unless
# all tables in the join give unique results.
#
do_test sort-11.1 {
execsql {
create table t8(a unique, b, c);
insert into t8 values(1,2,3);
insert into t8 values(2,3,4);
create table t9(x,y);
insert into t9 values(2,4);
insert into t9 values(2,3);
select y from t8, t9 where a=1 order by a, y;
}
} {3 4}
finish_test finish_test

View File

@@ -12,7 +12,7 @@
# focus of this file is testing the use of indices in WHERE clauses # focus of this file is testing the use of indices in WHERE clauses
# based on recent changes to the optimizer. # based on recent changes to the optimizer.
# #
# $Id: where2.test,v 1.4 2005/07/29 19:43:59 drh Exp $ # $Id: where2.test,v 1.5 2005/08/13 16:13:06 drh Exp $
set testdir [file dirname $argv0] set testdir [file dirname $argv0]
source $testdir/tester.tcl source $testdir/tester.tcl
@@ -238,6 +238,37 @@ do_test where2-6.6 {
} }
} {1 0 4 4 2 1 9 10 sort a i1w b i1zyx} } {1 0 4 4 2 1 9 10 sort a i1w b i1zyx}
# Unique queries (queries that are guaranteed to return only a single
# row of result) do not call the sorter. But all tables must give
# a unique result. If any one table in the join does not give a unique
# result then sorting is necessary.
#
do_test where2-7.1 {
cksort {
create table t8(a unique, b, c);
insert into t8 values(1,2,3);
insert into t8 values(2,3,4);
create table t9(x,y);
insert into t9 values(2,4);
insert into t9 values(2,3);
select y from t8, t9 where a=1 order by a, y;
}
} {3 4 sort}
do_test where2-7.2 {
cksort {
select * from t8 where a=1 order by b, c
}
} {1 2 3 nosort}
do_test where2-7.3 {
cksort {
select * from t8, t9 where a=1 and y=3 order by b, x
}
} {1 2 3 2 3 sort}
do_test where2-7.4 {
cksort {
create unique index i9y on t9(y);
select * from t8, t9 where a=1 and y=3 order by b, x
}
} {1 2 3 2 3 nosort}
finish_test finish_test