From: dan Date: Mon, 21 Feb 2011 17:49:49 +0000 (+0000) Subject: Fix a problem with "EXPLAIN QUERY PLAN SELECT count(*) FROM tbl". X-Git-Tag: version-3.7.6~132 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ef7075de1f356633fa2eba2d01127d3455300772;p=thirdparty%2Fsqlite.git Fix a problem with "EXPLAIN QUERY PLAN SELECT count(*) FROM tbl". FossilOrigin-Name: 9f9f32882501ac9b6e60f81195a64bdbf6e4497b --- diff --git a/install-sh b/install-sh old mode 100755 new mode 100644 diff --git a/manifest b/manifest index 37085376d0..9c2de2183e 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Have\sos_unix.c\scall\ssqlite3_log()\sfollowing\serrors\sin\scertain\ssystem\scalls. -D 2011-02-21T11:46:24 +C Fix\sa\sproblem\swith\s"EXPLAIN\sQUERY\sPLAN\sSELECT\scount(*)\sFROM\stbl". +D 2011-02-21T17:49:49 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 27701a1653595a1f2187dc61c8117e00a6c1d50f F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -98,7 +98,7 @@ F ext/rtree/rtree_util.tcl 06aab2ed5b826545bf215fff90ecb9255a8647ea F ext/rtree/sqlite3rtree.h 1af0899c63a688e272d69d8e746f24e76f10a3f0 F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024 -F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x +F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 F main.mk 54190fab7cdba523e311c274c95ea480f32abfb5 F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a @@ -175,7 +175,7 @@ F src/printf.c df2ff3bb5409e8958136933342c46464fbd017e7 F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50 F src/resolve.c 1c0f32b64f8e3f555fe1f732f9d6f501a7f05706 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697 -F src/select.c ae2f38effc99cfb9d8ebce36249d938487b3230d +F src/select.c d24406c45dd2442eb2eeaac413439066b149c944 F src/shell.c 649c51979812f77f97507024a4cea480c6862b8b F src/sqlite.h.in 388fca88d54577f671dc98bd805ff16085eabac1 F src/sqlite3ext.h c90bd5507099f62043832d73f6425d8d5c5da754 @@ -373,7 +373,7 @@ F test/enc.test e54531cd6bf941ee6760be041dff19a104c7acea F test/enc2.test 6d91a5286f59add0cfcbb2d0da913b76f2242398 F test/enc3.test 5c550d59ff31dccdba5d1a02ae11c7047d77c041 F test/enc4.test 4b575ef09e0eff896e73bd24076f96c2aa6a42de -F test/eqp.test 8f535d902b2df780d22edb95113880480664f976 +F test/eqp.test f14fadd76da53405e9885e2431cacf7191d83cdb F test/eval.test bc269c365ba877554948441e91ad5373f9f91be3 F test/exclusive.test 53e1841b422e554cecf0160f937c473d6d0e3062 F test/exclusive2.test 343d55130c12c67b8bf10407acec043a6c26c86b @@ -597,7 +597,7 @@ F test/permutations.test 5b2a4cb756ffb2407cb4743163668d1d769febb6 F test/pragma.test fdfc09067ea104a0c247a1a79d8093b56656f850 F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47 F test/printf.test 05970cde31b1a9f54bd75af60597be75a5c54fea -F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 +F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 x F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc F test/quick.test 1681febc928d686362d50057c642f77a02c62e57 F test/quota.test ddafe133653093eb9a99ccd6264884ae43f9c9b8 @@ -883,7 +883,7 @@ F tool/genfkey.test 4196a8928b78f51d54ef58e99e99401ab2f0a7e5 F tool/lemon.c dfd81a51b6e27e469ba21d01a75ddf092d429027 F tool/lempar.c 01ca97f87610d1dac6d8cd96ab109ab1130e76dc F tool/mkkeywordhash.c d2e6b4a5965e23afb80fbe74bb54648cd371f309 -F tool/mkopts.tcl 66ac10d240cc6e86abd37dc908d50382f84ff46e +F tool/mkopts.tcl 66ac10d240cc6e86abd37dc908d50382f84ff46e x F tool/mkspeedsql.tcl a1a334d288f7adfe6e996f2e712becf076745c97 F tool/mksqlite3c.tcl cf44512a48112b1ba09590548660a5a6877afdb3 F tool/mksqlite3h.tcl d76c226a5e8e1f3b5f6593bcabe5e98b3b1ec9ff @@ -908,7 +908,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P e87d499a4f8a456111c1f96ca6da31d0810fb7c8 -R fd86ed6c132ac26057f4026c493ad018 +P 01076528a43b61ae20ef6969b7d326de9b5313e4 +R ea0b61d4a154cb9b4ce10e2d0f175950 U dan -Z 2bb27580b8cb6c1cd91c27a584f12f47 +Z bee26eb130eb27a9686da9ca1f7baaf2 diff --git a/manifest.uuid b/manifest.uuid index ba250d2482..fa1d51f17d 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -01076528a43b61ae20ef6969b7d326de9b5313e4 \ No newline at end of file +9f9f32882501ac9b6e60f81195a64bdbf6e4497b \ No newline at end of file diff --git a/src/select.c b/src/select.c index 5eeedc9f95..ae32cce9c5 100644 --- a/src/select.c +++ b/src/select.c @@ -3618,6 +3618,32 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ sqlite3ExprCacheClear(pParse); } +/* +** Add a single OP_Explain instruction to the VDBE to explain a simple +** count(*) query ("SELECT count(*) FROM pTab"). +*/ +#ifndef SQLITE_OMIT_EXPLAIN +static void explainSimpleCount( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being queried */ + Index *pIdx /* Index used to optimize scan, or NULL */ +){ + if( pParse->explain==2 ){ + char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)", + pTab->zName, + pIdx ? "USING COVERING INDEX " : "", + pIdx ? pIdx->zName : "", + pTab->nRowEst + ); + sqlite3VdbeAddOp4( + pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC + ); + } +} +#else +# define explainSimpleCount(a,b,c) +#endif + /* ** Generate code for the SELECT statement given in the p argument. ** @@ -4229,6 +4255,7 @@ int sqlite3Select( } sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem); sqlite3VdbeAddOp1(v, OP_Close, iCsr); + explainSimpleCount(pParse, pTab, pBest); }else #endif /* SQLITE_OMIT_BTREECOUNT */ { diff --git a/test/eqp.test b/test/eqp.test index ca7544b41b..91a18d0026 100644 --- a/test/eqp.test +++ b/test/eqp.test @@ -21,6 +21,8 @@ set testprefix eqp # eqp-2.*: Tests for single select statements. # eqp-3.*: Select statements that execute sub-selects. # eqp-4.*: Compound select statements. +# ... +# eqp-7.*: "SELECT count(*) FROM tbl" statements (VDBE code OP_Count). # proc det {args} { uplevel do_eqp_test $args } @@ -507,6 +509,7 @@ set boilerplate { exit } +# Do a "Print Explain Query Plan" test. proc do_peqp_test {tn sql res} { set fd [open script.tcl w] puts $fd [string map [list %SQL% $sql] $::boilerplate] @@ -529,7 +532,47 @@ do_peqp_test 6.1 { 0 0 0 COMPOUND SUBQUERIES 1 AND 2 (EXCEPT) }] +#------------------------------------------------------------------------- +# The following tests - eqp-7.* - test that queries that use the OP_Count +# optimization return something sensible with EQP. +# +drop_all_tables + +do_execsql_test 7.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(a, b); + CREATE INDEX i1 ON t2(a); +} + +det 7.1 "SELECT count(*) FROM t1" { + 0 0 0 {SCAN TABLE t1 (~1000000 rows)} +} +det 7.2 "SELECT count(*) FROM t2" { + 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1(~1000000 rows)} +} + +do_execsql_test 7.3 { + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + + INSERT INTO t2 VALUES(1, 2); + INSERT INTO t2 VALUES(3, 4); + INSERT INTO t2 VALUES(5, 6); + + ANALYZE; +} + +db close +sqlite3 db test.db + +det 7.4 "SELECT count(*) FROM t1" { + 0 0 0 {SCAN TABLE t1 (~2 rows)} +} + +det 7.5 "SELECT count(*) FROM t2" { + 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1(~3 rows)} +} finish_test diff --git a/test/progress.test b/test/progress.test old mode 100644 new mode 100755 diff --git a/tool/mkopts.tcl b/tool/mkopts.tcl old mode 100644 new mode 100755