Add the showstat4.exe utility program for decoding and displaying the

content of the sqlite_stat4 table in a database.

FossilOrigin-Name: b4d9f6053d1d95fdc1eab8ce610b51e7df8d896d
This commit is contained in:
drh 2014-06-24 00:59:15 +00:00
parent c1971541e4
commit f127814eae
3 changed files with 162 additions and 6 deletions

View File

@ -1,5 +1,5 @@
C Add\sthe\s.fullschema\scommand\sto\sthe\ssqlite3.exe\sutility.\s\sThis\scommand\sshows\nthe\sschema\sand\sthe\scontent\sof\sthe\ssqlite_stat\stables,\sall\sin\sone\sgo.\s\sUseful\nwhen\sreporting\sproblems\swith\sthe\squery\splanner.
D 2014-06-23T23:28:13.644
C Add\sthe\sshowstat4.exe\sutility\sprogram\sfor\sdecoding\sand\sdisplaying\sthe\s\ncontent\sof\sthe\ssqlite_stat4\stable\sin\sa\sdatabase.
D 2014-06-24T00:59:15.223
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in b03432313a3aad96c706f8164fb9f5307eaf19f5
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -1160,6 +1160,7 @@ F tool/restore_jrnl.tcl 6957a34f8f1f0f8285e07536225ec3b292a9024a
F tool/rollback-test.c 9fc98427d1e23e84429d7e6d07d9094fbdec65a5
F tool/showdb.c b018a8a69d07050fc0fe9afcf17313cdef0cc599
F tool/showjournal.c b62cecaab86a4053d944c276bb5232e4d17ece02
F tool/showstat4.c 47662af1e2c9567fd821da910b3a247c5c138021
F tool/showwal.c 3f7f7da5ec0cba51b1449a75f700493377da57b5
F tool/soak1.tcl 8d407956e1a45b485a8e072470a3e629a27037fe
F tool/space_used.tcl f714c41a59e326b8b9042f415b628b561bafa06b
@ -1179,7 +1180,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 07dda49c1bf8997a18c3368acb81b6d863ea38d6
R 4c1bf033e13f45ce881081fa1f198f33
P ebec48921c092e20c9d7608242b63db40b40be5e
R 0ce5547c26474f508ca16bdace146cd3
U drh
Z 8c91ced329194a39e8cdf52b6d2ec4dd
Z 0e7559d34a6f25f8be2f321b29251255

View File

@ -1 +1 @@
ebec48921c092e20c9d7608242b63db40b40be5e
b4d9f6053d1d95fdc1eab8ce610b51e7df8d896d

155
tool/showstat4.c Normal file
View File

@ -0,0 +1,155 @@
/*
** This utility program decodes and displays the content of the
** sqlite_stat4 table in the database file named on the command
** line.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "sqlite3.h"
typedef sqlite3_int64 i64; /* 64-bit signed integer type */
/*
** Convert the var-int format into i64. Return the number of bytes
** in the var-int. Write the var-int value into *pVal.
*/
static int decodeVarint(const unsigned char *z, i64 *pVal){
i64 v = 0;
int i;
for(i=0; i<8; i++){
v = (v<<7) + (z[i]&0x7f);
if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
}
v = (v<<8) + (z[i]&0xff);
*pVal = v;
return 9;
}
int main(int argc, char **argv){
sqlite3 *db;
sqlite3_stmt *pStmt;
char *zIdx = 0;
int rc, j, x, y, mxHdr;
const unsigned char *aSample;
int nSample;
i64 iVal;
const char *zSep;
if( argc!=2 ){
fprintf(stderr, "Usage: %s DATABASE-FILE\n", argv[0]);
exit(1);
}
rc = sqlite3_open(argv[1], &db);
if( rc!=SQLITE_OK || db==0 ){
fprintf(stderr, "Cannot open database file [%s]\n", argv[1]);
exit(1);
}
rc = sqlite3_prepare_v2(db,
"SELECT tbl||'.'||idx, nEq, nLT, nDLt, sample "
"FROM sqlite_stat4 ORDER BY 1", -1,
&pStmt, 0);
if( rc!=SQLITE_OK || pStmt==0 ){
fprintf(stderr, "%s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
while( SQLITE_ROW==sqlite3_step(pStmt) ){
if( zIdx==0 || strcmp(zIdx, (const char*)sqlite3_column_text(pStmt,0))!=0 ){
if( zIdx ) printf("\n");
sqlite3_free(zIdx);
zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0));
printf("%s:\n", zIdx);
}else{
printf(" -----------------------------------------------------------\n");
}
printf(" nEq = %s\n", sqlite3_column_text(pStmt,1));
printf(" nLt = %s\n", sqlite3_column_text(pStmt,2));
printf(" nDLt = %s\n", sqlite3_column_text(pStmt,3));
printf(" sample = x'");
aSample = sqlite3_column_blob(pStmt,4);
nSample = sqlite3_column_bytes(pStmt,4);
for(j=0; j<nSample; j++) printf("%02x", aSample[j]);
printf("'\n ");
zSep = " ";
x = decodeVarint(aSample, &iVal);
if( iVal<x || iVal>nSample ){
printf(" <error>\n");
continue;
}
y = mxHdr = (int)iVal;
while( x<mxHdr ){
int sz;
i64 v;
x += decodeVarint(aSample+x, &iVal);
if( x>mxHdr ) break;
if( iVal<0 ) break;
switch( iVal ){
case 0: sz = 0; break;
case 1: sz = 1; break;
case 2: sz = 2; break;
case 3: sz = 3; break;
case 4: sz = 4; break;
case 5: sz = 6; break;
case 6: sz = 8; break;
case 7: sz = 8; break;
case 8: sz = 0; break;
case 9: sz = 0; break;
case 10:
case 11: sz = 0; break;
default: sz = (int)(iVal-12)/2; break;
}
if( y+sz>nSample ) break;
if( iVal==0 ){
printf("%sNULL", zSep);
}else if( iVal<=7 ){
v = (signed char)aSample[y];
for(j=1; j<sz; j++){
v = (v<<8) + aSample[y+j];
}
if( iVal==7 ){
double r;
memcpy(&r, &v, sizeof(r));
printf("%s%#g", zSep, r);
}else{
printf("%s%lld", zSep, v);
}
}else if( (iVal&1)==0 ){
printf("%sx'", zSep);
for(j=0; j<sz; j++){
printf("%02x", aSample[y+j]);
}
printf("'");
}else{
printf("%s\"", zSep);
for(j=0; j<sz; j++){
char c = (char)aSample[y+j];
if( isprint(c) ){
if( c=='"' || c=='\\' ) putchar('\\');
putchar(c);
}else if( c=='\n' ){
printf("\\n");
}else if( c=='\t' ){
printf("\\t");
}else if( c=='\r' ){
printf("\\r");
}else{
printf("\\%03o", c);
}
}
printf("\"");
}
zSep = ",";
y += sz;
}
printf("\n");
}
sqlite3_free(zIdx);
sqlite3_finalize(pStmt);
sqlite3_close(db);
return 0;
}