]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Have sqlite3_stmt_scanstatus() report cycle, loop and row counts separately for creat...
authordan <Dan Kennedy>
Wed, 7 Dec 2022 17:29:17 +0000 (17:29 +0000)
committerdan <Dan Kennedy>
Wed, 7 Dec 2022 17:29:17 +0000 (17:29 +0000)
FossilOrigin-Name: 3bc9df82ea5b0fb085c56a326a65e19c9baf98d48d8fa6344c0d7004747594ba

manifest
manifest.uuid
src/where.c
test/scanstatus2.test

index d4b5f8cb2531164e7c447fb2b0ca0c53f1deea75..2d0b3556cfac2e8c25da988e4217fcf07c4c0d7c 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Streamline\sand\simprove\stesting\sof\sthe\slocking\sin\sthe\smemdb\sVFS.\nFollow-on\sto\s[15f0be8a640e7bfa].
-D 2022-12-07T16:58:04.002
+C Have\ssqlite3_stmt_scanstatus()\sreport\scycle,\sloop\sand\srow\scounts\sseparately\sfor\screating\san\sautomatic\sindex\sand\susing\sthat\sautomatic\sindex.
+D 2022-12-07T17:29:17.346
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -732,7 +732,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c b9df133a705093da8977da5eb202eaadb844839f1c7297c08d33471f5491843d
 F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b
-F src/where.c adc6783cd3b136271a70001182e899d3523e199bda41ead5c3e2ab4a84456af1
+F src/where.c 3ee4050e453dfd03da4a8e4b5561d20b20b2302ec9d02437fb62c6f29bb78401
 F src/whereInt.h e25203e5bfee149f5f1225ae0166cfb4f1e65490c998a024249e98bb0647377c
 F src/wherecode.c 76bca3379219880d2527493b71a3be49e696f75396d3481e4de5d4ceec7886b2
 F src/whereexpr.c 05295b44b54eea76d1ba766f0908928d0e20e990c249344c9521454d3d09c7ae
@@ -1458,7 +1458,7 @@ F test/savepoint6.test f41279c5e137139fa5c21485773332c7adb98cd7
 F test/savepoint7.test cde525ea3075283eb950cdcdefe23ead4f700daa
 F test/savepointfault.test f044eac64b59f09746c7020ee261734de82bf9b2
 F test/scanstatus.test 7dbcfd6adc6a8df6abc59f83d6da5a27e1bce0b2f6fa55147c8176d7c44e0450
-F test/scanstatus2.test 3f45ec1d8b45dcb0df7f98daf3364fc2e03bf89a8c3dd2428251a5e0a34de7af
+F test/scanstatus2.test 07e2d1b86a07cd8f6cbef9546c9d6b27af0de5e613b6f38f98666f9c729bd4c7
 F test/schema.test 5dd11c96ba64744de955315d2e4f8992e447533690153b93377dffb2a5ef5431
 F test/schema2.test 906408621ea881fdb496d878b1822572a34e32c5
 F test/schema3.test 8ed4ae66e082cdd8b1b1f22d8549e1e7a0db4527a8e6ee8b6193053ee1e5c9ce
@@ -2067,8 +2067,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 04af7ef77043702f93cbff23548610759786893bd3d4d6fc08181e1e249c6663
-R 12ec76fa12aeff3e27ce79fb7684d7d6
-U drh
-Z af8ae60fb39ff0cbbaef800ceb1294d9
+P d71a08375aeb525c10037c373b8eeb7e29f7dfaf7c4bfc02f4d99616c5940405
+R aa03a3260eaa51e2eda4cccd90080651
+U dan
+Z 28dcdc9b3132851feb893087f0e4ae04
 # Remove this line to create a well-formed Fossil manifest.
index 96dc67715e49cf49d52f8a2388dd4c8602bca1df..a5f6f520a52e998eef2c2bcef642827678e8c217 100644 (file)
@@ -1 +1 @@
-d71a08375aeb525c10037c373b8eeb7e29f7dfaf7c4bfc02f4d99616c5940405
\ No newline at end of file
+3bc9df82ea5b0fb085c56a326a65e19c9baf98d48d8fa6344c0d7004747594ba
\ No newline at end of file
index 1cf959207090c90505c0e987d653e19213ebc51b..25ad18b282c2a8ef1af3379fd6be4ca439d9a54a 100644 (file)
@@ -812,6 +812,51 @@ static int termCanDriveIndex(
 
 
 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
+
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
+/*
+** Argument pIdx represents an automatic index that the current statement
+** will create and populate. Add an OP_Explain with text of the form:
+**
+**     CREATE AUTOMATIC INDEX ON <table>(<cols>) [WHERE <expr>]
+**
+** This is only required if sqlite3_stmt_scanstatus() is enabled, to
+** associate an SQLITE_SCANSTAT_NCYCLE and SQLITE_SCANSTAT_NLOOP
+** values with. In order to avoid breaking legacy code and test cases, 
+** the OP_Explain is not added if this is an EXPLAIN QUERY PLAN command.
+*/
+static void explainAutomaticIndex(
+  Parse *pParse,
+  Index *pIdx,                    /* Automatic index to explain */
+  int bPartial,                   /* True if pIdx is a partial index */
+  int *pAddrExplain               /* OUT: Address of OP_Explain */
+){
+  if( pParse->explain!=2 ){
+    Table *pTab = pIdx->pTable;
+    const char *zSep = "";
+    char *zText = 0;
+    int ii = 0;
+    zText = sqlite3_mprintf("CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
+    assert( pIdx->nColumn>1 );
+    assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID );
+    for(ii=0; ii<(pIdx->nColumn-1); ii++){
+      const char *zName = 0;
+      int iCol = pIdx->aiColumn[ii];
+
+      zName = pTab->aCol[iCol].zCnName;
+      zText = sqlite3_mprintf("%z%s%s", zText, zSep, zName);
+      zSep = ", ";
+    }
+    *pAddrExplain = sqlite3VdbeExplain(
+        pParse, 0, "%s)%s", zText, (bPartial ? " WHERE <expr>" : "")
+    );
+    sqlite3_free(zText);
+  }
+}
+#else
+# define explainAutomaticIndex(a,b,c,d)
+#endif
+
 /*
 ** Generate code to construct the Index object for an automatic index
 ** and to set up the WhereLevel object pLevel so that the code generator
@@ -847,6 +892,9 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
   SrcItem *pTabItem;          /* FROM clause term being indexed */
   int addrCounter = 0;        /* Address where integer counter is initialized */
   int regBase;                /* Array of registers where record is assembled */
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
+  int addrExp = 0;            /* Address of OP_Explain */
+#endif
 
   /* Generate code to skip over the creation and initialization of the
   ** transient index on 2nd and subsequent iterations of the loop. */
@@ -970,6 +1018,7 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
   pIdx->azColl[n] = sqlite3StrBINARY;
 
   /* Create the automatic index */
+  explainAutomaticIndex(pParse, pIdx, pPartial!=0, &addrExp);
   assert( pLevel->iIdxCur>=0 );
   pLevel->iIdxCur = pParse->nTab++;
   sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
@@ -1005,6 +1054,7 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
     sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
                          regBase, pLoop->u.btree.nEq);
   }
+  sqlite3VdbeScanStatusCounters(v, addrExp, addrExp, sqlite3VdbeCurrentAddr(v));
   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
   if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
@@ -1025,6 +1075,7 @@ static SQLITE_NOINLINE void constructAutomaticIndex(
   
   /* Jump here when skipping the initialization */
   sqlite3VdbeJumpHere(v, addrInit);
+  sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
 
 end_auto_index_create:
   sqlite3ExprDelete(pParse->db, pPartial);
index 9a4758b6d4eeb30e73e36d5f4a7ba48ae7afaece..1b5c18cb8ba7f71bec8b5f81b2288eae2a1c40fb 100644 (file)
@@ -170,9 +170,66 @@ do_graph_test 2.1 {
 } {
 QUERY (nCycle=nnn)
 --SCAN x1 (nCycle=nnn)
+--CREATE AUTOMATIC INDEX ON x2(c, d) (nCycle=nnn)
 --SEARCH x2 USING AUTOMATIC COVERING INDEX (c=?) (nCycle=nnn)
 }
 
+#-------------------------------------------------------------------------
+reset_db
+do_execsql_test 4.0 {
+  CREATE TABLE rt1 (id INTEGER PRIMARY KEY, x1, x2);
+  CREATE TABLE rt2 (id, x1, x2);
+}
+
+do_graph_test 4.1 {
+  SELECT * FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1;
+} {
+QUERY (nCycle=nnn)
+--SCAN rt1 (nCycle=nnn)
+--CREATE AUTOMATIC INDEX ON rt2(x1, id, x2) (nCycle=nnn)
+--SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
+}
+
+do_graph_test 4.2 {
+  SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1;
+} {
+QUERY (nCycle=nnn)
+--SCAN rt1 (nCycle=nnn)
+--CREATE AUTOMATIC INDEX ON rt2(x1, id) (nCycle=nnn)
+--SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
+}
+
+do_graph_test 4.3 {
+  SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND (rt2.x1+1)=(rt1.x1+1);
+} {
+QUERY (nCycle=nnn)
+--SCAN rt1 (nCycle=nnn)
+--SCAN rt2 (nCycle=nnn)
+}
+
+do_graph_test 4.4 {
+  SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=(rt1.x1+1) AND rt2.id>5;
+} {
+QUERY (nCycle=nnn)
+--SCAN rt1 (nCycle=nnn)
+--CREATE AUTOMATIC INDEX ON rt2(x1, id) WHERE <expr> (nCycle=nnn)
+--SEARCH rt2 USING AUTOMATIC PARTIAL COVERING INDEX (x1=?) (nCycle=nnn)
+}
+
+do_graph_test 4.5 {
+  SELECT v1.cnt FROM rt1, (
+    SELECT count(*) AS cnt, rt2.x1 AS x1 FROM rt2 GROUP BY x1
+  ) AS v1 WHERE rt1.x1=v1.x1
+} {
+QUERY (nCycle=nnn)
+--MATERIALIZE v1 (nCycle=nnn)
+----SCAN rt2 (nCycle=nnn)
+----USE TEMP B-TREE FOR GROUP BY
+--SCAN rt1 (nCycle=nnn)
+--CREATE AUTOMATIC INDEX ON v1(x1, cnt) (nCycle=nnn)
+--SEARCH v1 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
+}
+
 finish_test