From 9352cfa30b33bc88a422a05564616b5397d54a97 Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 1 Apr 2024 15:38:15 +0000 Subject: [PATCH] Improved comments in the query planner logic that computes the cost for a particular step in a query plan. No code changes. FossilOrigin-Name: 0b2ac2cdc767db764e3ea8bbc33898cac4e1ec27fe8c9b60ce08a1785f921e6d --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/where.c | 21 ++++++++++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/manifest b/manifest index 1e6bf524c1..4939830a71 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Make\sexplicit\sthat\ssqlite3_keyword_name()'s\sindex\sis\s0-based,\sper\sforum\srequest.\sDoc\schanges\sonly. -D 2024-03-30T14:11:30.350 +C Improved\scomments\sin\sthe\squery\splanner\slogic\sthat\scomputes\sthe\scost\sfor\sa\nparticular\sstep\sin\sa\squery\splan.\s\sNo\scode\schanges. +D 2024-04-01T15:38:15.301 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -835,7 +835,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 F src/wal.c 887fc4ca3f020ebb2e376f222069570834ac63bf50111ef0cbf3ae417048ed89 F src/wal.h ba252daaa94f889f4b2c17c027e823d9be47ce39da1d3799886bbd51f0490452 F src/walker.c 7c7ea0115345851c3da4e04e2e239a29983b61fb5b038b94eede6aba462640e2 -F src/where.c 11a67988c3a5be2e7ee07c958998f3ad4b30dc491c0add894c2933c752415b16 +F src/where.c a15429ae9d42c7b824b9faad7e8234b006abf0a4526166dc4dfc0cc9cd2c9e6a F src/whereInt.h 82a13766f13d1a53b05387c2e60726289ef26404bc7b9b1f7770204d97357fb8 F src/wherecode.c 5d77db30a2a3dd532492ae882de114edba2fae672622056b1c7fd61f5917a8f1 F src/whereexpr.c 7b64295f1d82ad0928df435925dd7bbd5997b44a026153113eace0d9e71ff435 @@ -2183,8 +2183,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 3d4b1f0791384d3e531d6757daecf67e5b873954de61f37032474e3ae23cd22b -R 1ec9bcb0364e01dba28a12b65d50cd52 -U stephan -Z 8fd3b1df728a4b7485ff966dda79d536 +P 090943dc31e7a3af5c11c1c0953cb82ae3ca07ba000189bb85deaecc76921504 +R 27a9be3de32bd888ad6632e320231186 +U drh +Z 13460dee49d687f64f99bd37193c204d # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index a86194fdb9..3fb4e018f6 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -090943dc31e7a3af5c11c1c0953cb82ae3ca07ba000189bb85deaecc76921504 \ No newline at end of file +0b2ac2cdc767db764e3ea8bbc33898cac4e1ec27fe8c9b60ce08a1785f921e6d \ No newline at end of file diff --git a/src/where.c b/src/where.c index 5767b44128..cea134d0e2 100644 --- a/src/where.c +++ b/src/where.c @@ -3252,10 +3252,13 @@ static int whereLoopAddBtreeIndex( } } - /* Set rCostIdx to the cost of visiting selected rows in index. Add - ** it to pNew->rRun, which is currently set to the cost of the index - ** seek only. Then, if this is a non-covering index, add the cost of - ** visiting the rows in the main table. */ + /* Set rCostIdx to the estimated cost of visiting selected rows in the + ** index. The estimate is the sum of two values: + ** 1. The cost of doing one search-by-key to find the first matching + ** entry + ** 2. Stepping forward in the index pNew->nOut times to find all + ** additional matching entries. + */ assert( pSrc->pTab->szTabRow>0 ); if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){ /* The pProbe->szIdxRow is low for an IPK table since the interior @@ -3266,7 +3269,15 @@ static int whereLoopAddBtreeIndex( }else{ rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; } - pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); + rCostIdx = sqlite3LogEstAdd(rLogSize, rCostIdx); + + /* Estimate the cost of running the loop. If all data is coming + ** from the index, then this is just the cost of doing the index + ** lookup and scan. But if some data is coming out of the main table, + ** we also have to add in the cost of doing pNew->nOut searches to + ** locate the row in the main table that corresponds to the index entry. + */ + pNew->rRun = rCostIdx; if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){ pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); }