From 865d4d429086a3dc541d5daead1186a7be93807e Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Mon, 1 Sep 2008 12:46:59 +0000 Subject: [PATCH] Have the rtree module set the estimatedCost output variable. Ticket #3312. (CVS 5649) FossilOrigin-Name: 483932c4e08901a11b7ab671073fd0a048b10d66 --- ext/rtree/rtree.c | 11 ++++- ext/rtree/rtree6.test | 111 ++++++++++++++++++++++++++++++++++++++++++ manifest | 15 +++--- manifest.uuid | 2 +- 4 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 ext/rtree/rtree6.test diff --git a/ext/rtree/rtree.c b/ext/rtree/rtree.c index 39116ceb4b..f306113fcb 100644 --- a/ext/rtree/rtree.c +++ b/ext/rtree/rtree.c @@ -12,7 +12,7 @@ ** This file contains code for implementations of the r-tree and r*-tree ** algorithms packaged as an SQLite virtual table module. ** -** $Id: rtree.c,v 1.7 2008/07/16 14:43:35 drh Exp $ +** $Id: rtree.c,v 1.8 2008/09/01 12:47:00 danielk1977 Exp $ */ #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE) @@ -1116,6 +1116,13 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ pIdxInfo->idxNum = 1; pIdxInfo->aConstraintUsage[ii].argvIndex = 1; pIdxInfo->aConstraintUsage[jj].omit = 1; + + /* This strategy involves a two rowid lookups on an B-Tree structures + ** and then a linear search of an R-Tree node. This should be + ** considered almost as quick as a direct rowid lookup (for which + ** sqlite uses an internal cost of 0.0). + */ + pIdxInfo->estimatedCost = 10.0; return SQLITE_OK; } @@ -1169,6 +1176,8 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){ return SQLITE_NOMEM; } + assert( iIdx>=0 ); + pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1)); return rc; } diff --git a/ext/rtree/rtree6.test b/ext/rtree/rtree6.test new file mode 100644 index 0000000000..d26137bfbd --- /dev/null +++ b/ext/rtree/rtree6.test @@ -0,0 +1,111 @@ +# 2008 Sep 1 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# $Id: rtree6.test,v 1.1 2008/09/01 12:47:00 danielk1977 Exp $ +# + +if {![info exists testdir]} { + set testdir [file join [file dirname $argv0] .. .. test] +} +source $testdir/tester.tcl + +ifcapable !rtree { + finish_test + return +} + +# Operator Byte Value +# ---------------------- +# = 0x41 ('A') +# <= 0x42 ('B') +# < 0x43 ('C') +# >= 0x44 ('D') +# > 0x45 ('E') +# ---------------------- + +proc rtree_strategy {sql} { + set ret [list] + db eval "explain $sql" a { + if {$a(opcode) eq "VFilter"} { + lappend ret $a(p4) + } + } + set ret +} + +proc query_plan {sql} { + set ret [list] + db eval "explain query plan $sql" a { + lappend ret $a(detail) + } + set ret +} + +do_test rtree6-1.1 { + execsql { + CREATE TABLE t2(k INTEGER PRIMARY KEY, v); + CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2); + } +} {} + +do_test rtree6-1.2 { + rtree_strategy {SELECT * FROM t1 WHERE x1>10} +} {Ea} + +do_test rtree6-1.3 { + rtree_strategy {SELECT * FROM t1 WHERE x1<10} +} {Ca} + +do_test rtree6-1.4 { + rtree_strategy {SELECT * FROM t1,t2 WHERE k=ii AND x1<10} +} {Ca} + +do_test rtree6-1.5 { + rtree_strategy {SELECT * FROM t1,t2 WHERE k=+ii AND x1<10} +} {Ca} + +do_test rtree6.2.1 { + query_plan {SELECT * FROM t1,t2 WHERE k=+ii AND x1<10} +} [list \ + {TABLE t1 VIRTUAL TABLE INDEX 2:Ca} \ + {TABLE t2 USING PRIMARY KEY} \ +] + +do_test rtree6.2.2 { + query_plan {SELECT * FROM t1,t2 WHERE k=ii AND x1<10} +} [list \ + {TABLE t1 VIRTUAL TABLE INDEX 2:Ca} \ + {TABLE t2 USING PRIMARY KEY} \ +] + +do_test rtree6.2.3 { + query_plan {SELECT * FROM t1,t2 WHERE k=ii} +} [list \ + {TABLE t2} \ + {TABLE t1 VIRTUAL TABLE INDEX 1:} \ +] + +do_test rtree6.2.4 { + query_plan {SELECT * FROM t1,t2 WHERE v=10 and x1<10 and x2>10} +} [list \ + {TABLE t2} \ + {TABLE t1 VIRTUAL TABLE INDEX 2:CaEb} \ +] + +do_test rtree6.2.5 { + query_plan {SELECT * FROM t1,t2 WHERE k=ii AND x1