Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/tool/index_usage.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / tool / index_usage.c` 233 lines ยท 7084 bytes Raw Blame History 1 /* 2 ** 2018-12-04 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This file implements a utility program used to help determine which 14 ** indexes in a database schema are used and unused, and how often specific 15 ** indexes are used. 16 */ 17 #include "sqlite3.h" 18 #include 19 #include 20 #include 21 #include 22 23 static void usage(const char *argv0){ 24 printf("Usage: %s [OPTIONS] DATABASE LOG\n\n", argv0); 25 printf( 26 "DATABASE is an SQLite database against which various statements\n" 27 "have been run. The SQL text is stored in LOG. LOG is an SQLite\n" 28 "database with this schema:\n" 29 "\n" 30 " CREATE TABLE sqllog(sql TEXT);\n" 31 "\n" 32 "This utility program analyzes statements contained in LOG and prints\n" 33 "a report showing how many times each index in DATABASE is used by the\n" 34 "statements in LOG.\n" 35 "\n" 36 "DATABASE only needs to contain the schema used by the statements in\n" 37 "LOG. The content can be removed from DATABASE.\n" 38 ); 39 printf( 40 "\nOPTIONS:\n\n" 41 " --progress N Show a progress message after every N input rows\n" 42 " -q Omit error message when parsing log entries\n" 43 " --using NAME Print SQL statements that use index NAME\n" 44 ); 45 printf("\nAnalysis will be done by SQLite version %s dated %.20s\n" 46 "checkin number %.40s. Different versions\n" 47 "of SQLite might use different indexes.\n", 48 sqlite3_libversion(), sqlite3_sourceid(), sqlite3_sourceid()+21); 49 exit(1); 50 } 51 52 int main(int argc, char **argv){ 53 sqlite3 *db = 0; /* The main database */ 54 sqlite3_stmt *pStmt = 0; /* a query */ 55 char *zSql; 56 int nErr = 0; 57 int rc; 58 int bQuiet = 0; 59 int i, j; 60 const char *zUsing = 0; 61 sqlite3_stmt *pIncrCnt = 0; 62 int nRow = 0; 63 int iProgress = 0; 64 65 for(i=j=1; i0 && (nRow%iProgress)==0 ){ 178 printf("%d...\n", nRow); 179 fflush(stdout); 180 } 181 while( sqlite3_step(pS2)==SQLITE_ROW ){ 182 const char *zExplain = (const char*)sqlite3_column_text(pS2,3); 183 const char *z1, *z2; 184 int n; 185 /* printf("EXPLAIN: %s\n", zExplain); */ 186 z1 = strstr(zExplain, " USING INDEX "); 187 if( z1==0 ) continue; 188 z1 += 13; 189 for(z2=z1+1; z2[0] && z2[1]!='('; z2++){} 190 n = z2 - z1; 191 if( zUsing && sqlite3_strnicmp(zUsing, z1, n)==0 ){ 192 printf("Using %s:\n%s\n", zUsing, zLog); 193 fflush(stdout); 194 } 195 sqlite3_bind_text(pIncrCnt,1,z1,n,SQLITE_STATIC); 196 sqlite3_step(pIncrCnt); 197 sqlite3_reset(pIncrCnt); 198 } 199 } 200 sqlite3_finalize(pS2); 201 } 202 sqlite3_finalize(pStmt); 203 204 /* Generate the report */ 205 rc = sqlite3_prepare_v2(db, 206 "SELECT tbl, idx, cnt, " 207 " (SELECT group_concat(name,',') FROM pragma_index_info(idx))" 208 " FROM temp.idxu, main.sqlite_schema" 209 " WHERE temp.idxu.tbl=main.sqlite_schema.tbl_name" 210 " AND temp.idxu.idx=main.sqlite_schema.name" 211 " ORDER BY cnt DESC, tbl, idx", 212 -1, &pStmt, 0); 213 if( rc ){ 214 printf("Cannot query the result table - %s\n", 215 sqlite3_errmsg(db)); 216 goto errorOut; 217 } 218 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 219 printf("%10d %s on %s(%s)\n", 220 sqlite3_column_int(pStmt, 2), 221 sqlite3_column_text(pStmt, 1), 222 sqlite3_column_text(pStmt, 0), 223 sqlite3_column_text(pStmt, 3)); 224 } 225 sqlite3_finalize(pStmt); 226 pStmt = 0; 227 228 errorOut: 229 sqlite3_finalize(pIncrCnt); 230 sqlite3_finalize(pStmt); 231 sqlite3_close(db); 232 return nErr; 233 }