diff options
author | drh <drh@noemail.net> | 2019-01-30 15:47:38 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2019-01-30 15:47:38 +0000 |
commit | a8614259c17898e60a8f8da73d14bc404c0ebded (patch) | |
tree | 361b80adf0d3e93e71fb4e35ce2cc7afe143cc27 /tool/index_usage.c | |
parent | 5a9c6bcc158633596ea7c7d9778c4e7671fd183b (diff) | |
download | sqlite-a8614259c17898e60a8f8da73d14bc404c0ebded.tar.gz sqlite-a8614259c17898e60a8f8da73d14bc404c0ebded.zip |
Add the --progress, --using, and -q options to the index_usage utility program.
FossilOrigin-Name: a5e6be7cbc5d931308ddcc073c9cd6275c9711cae055d72a7c4aa71c1d15914c
Diffstat (limited to 'tool/index_usage.c')
-rw-r--r-- | tool/index_usage.c | 87 |
1 files changed, 77 insertions, 10 deletions
diff --git a/tool/index_usage.c b/tool/index_usage.c index 9d97c7318..abe9ae42d 100644 --- a/tool/index_usage.c +++ b/tool/index_usage.c @@ -21,7 +21,7 @@ #include <string.h> static void usage(const char *argv0){ - printf("Usage: %s DATABASE LOG\n\n", argv0); + printf("Usage: %s [OPTIONS] DATABASE LOG\n\n", argv0); printf( "DATABASE is an SQLite database against which various statements\n" "have been run. The SQL text is stored in LOG. LOG is an SQLite\n" @@ -36,6 +36,12 @@ static void usage(const char *argv0){ "DATABASE only needs to contain the schema used by the statements in\n" "LOG. The content can be removed from DATABASE.\n" ); + printf( + "\nOPTIONS:\n\n" + " --progress N Show a progress message after every N input rows\n" + " -q Omit error message when parsing log entries\n" + " --using NAME Print SQL statements that use index NAME\n" + ); printf("\nAnalysis will be done by SQLite version %s dated %.20s\n" "checkin number %.40s. Different versions\n" "of SQLite might use different indexes.\n", @@ -49,6 +55,48 @@ int main(int argc, char **argv){ char *zSql; int nErr = 0; int rc; + int bQuiet = 0; + int i, j; + const char *zUsing = 0; + sqlite3_stmt *pIncrCnt = 0; + int nRow = 0; + int iProgress = 0; + + for(i=j=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' ){ + z++; + if( z[0]=='-' ) z++; + if( strcmp(z,"progress")==0 ){ + if( i+1<argc ){ + iProgress = strtol(argv[++i],0,0); + continue; + } + printf("The --progress option requires an argument\n"); + exit(0); + } + if( strcmp(z,"q")==0 ){ + bQuiet = 1; + continue; + } + if( strcmp(z,"using")==0 ){ + if( i+1<argc ){ + zUsing = argv[++i]; + continue; + } + printf("The --using option requires an argument\n"); + exit(0); + } + if( strcmp(z, "help")==0 || strcmp(z, "?")==0 ){ + usage(argv[0]); + } + printf("Unknown command-line option: \"%s\"\n", argv[i]); + exit(0); + }else{ + if( j<i ) argv[j++] = argv[i]; + } + } + argc = j; if( argc!=3 ) usage(argv[0]); rc = sqlite3_open_v2(argv[1], &db, SQLITE_OPEN_READONLY, 0); @@ -66,8 +114,8 @@ int main(int argc, char **argv){ pStmt = 0; rc = sqlite3_exec(db, "CREATE TABLE temp.idxu(\n" - " tbl TEXT,\n" - " idx TEXT,\n" + " tbl TEXT COLLATE nocase,\n" + " idx TEXT COLLATE nocase,\n" " cnt INT,\n" " PRIMARY KEY(idx)\n" ") WITHOUT ROWID;", 0, 0, 0); @@ -100,6 +148,15 @@ int main(int argc, char **argv){ goto errorOut; } + rc = sqlite3_prepare_v2(db, + "UPDATE temp.idxu SET cnt=cnt+1 WHERE idx=?1", + -1, &pIncrCnt, 0); + if( rc ){ + printf("Cannot prepare a statement to increment a counter for " + "indexes used\n"); + goto errorOut; + } + /* Update the counts based on LOG */ while( sqlite3_step(pStmt)==SQLITE_ROW ){ const char *zLog = (const char*)sqlite3_column_text(pStmt, 0); @@ -109,10 +166,18 @@ int main(int argc, char **argv){ rc = sqlite3_prepare_v2(db, zSql, -1, &pS2, 0); sqlite3_free(zSql); if( rc ){ - printf("Cannot compile LOG entry %d (%s): %s\n", + if( !bQuiet ){ + printf("Cannot compile LOG entry %d (%s): %s\n", sqlite3_column_int(pStmt, 1), zLog, sqlite3_errmsg(db)); + fflush(stdout); + } nErr++; }else{ + nRow++; + if( iProgress>0 && (nRow%iProgress)==0 ){ + printf("%d...\n", nRow); + fflush(stdout); + } while( sqlite3_step(pS2)==SQLITE_ROW ){ const char *zExplain = (const char*)sqlite3_column_text(pS2,3); const char *z1, *z2; @@ -123,12 +188,13 @@ int main(int argc, char **argv){ z1 += 13; for(z2=z1+1; z2[1] && z2[1]!='('; z2++){} n = z2 - z1; - zSql = sqlite3_mprintf( - "UPDATE temp.idxu SET cnt=cnt+1 WHERE idx='%.*q'", n, z1 - ); - /* printf("sql: %s\n", zSql); */ - sqlite3_exec(db, zSql, 0, 0, 0); - sqlite3_free(zSql); + if( zUsing && sqlite3_strnicmp(zUsing, z1, n)==0 ){ + printf("Using %s:\n%s\n", zUsing, zLog); + fflush(stdout); + } + sqlite3_bind_text(pIncrCnt,1,z1,n,SQLITE_STATIC); + sqlite3_step(pIncrCnt); + sqlite3_reset(pIncrCnt); } } sqlite3_finalize(pS2); @@ -160,6 +226,7 @@ int main(int argc, char **argv){ pStmt = 0; errorOut: + sqlite3_finalize(pIncrCnt); sqlite3_finalize(pStmt); sqlite3_close(db); return nErr; |