tursodatabase / turso-cli

Command line interface to Turso.
https://turso.tech
MIT License
215 stars 35 forks source link

Support for per-table stats #268

Closed psarna closed 1 year ago

psarna commented 1 year ago

libSQL has a virtual table -- dbstat -- which prints useful information about which tables/indexes use how much storage. Let's integrate it into the CLI to present this information in a more human-readable way.

Example:

[sarna@sarna-pc sqld]$ ~/.turso/turso db shell spin <<< 'select * from dbstat'
Connected to spin at https://psarna:9J41z0x85j7Qbvn2@spin-psarna.turso.io/

Welcome to Turso SQL shell!

Type ".quit" to exit the shell, ".tables" to list all tables, and ".schema" to show table schemas.

NAME                            PATH  PAGENO  PAGETYPE  NCELL  PAYLOAD  UNUSED  MX_PAYLOAD  PGOFFSET  PGSIZE  
sqlite_schema                   /     1       leaf      6      630      3333    141         0         4096    
libsql_wasm_func_table          /     2       leaf      0      0        4088    0           4096      4096    
_litestream_seq                 /     6       leaf      1      4        4080    4           20480     4096    
_litestream_lock                /     7       leaf      0      0        4088    0           24576     4096    
counter                         /     4       leaf      2      59       4023    34          12288     4096    
coordinates                     /     5       leaf      2      70       4010    43          16384     4096    
sqlite_autoindex_coordinates_1  /     3       leaf      2      41       4041    21          8192      4096
psarna commented 1 year ago

Better example, with results aggregated per table:

 sqlite> select * from dbstat where aggregate = true;
 name           path  pageno  pagetype  ncell  payload  unused  mx_payload  pgoffset  pgsize
 -------------  ----  ------  --------  -----  -------  ------  ----------  --------  ------
 sqlite_schema        1                 1      31       3953    31                    4096  
 t                    85                5      337265   10491   171721                348160

Interesting fields are pgsize, which is a misnomer and contains the total size a table occupies. payload contains a sum of useful bytes a table occupies, not counting internal fragmentation inside pages. (So payload/pgsize tells you how well the disk space is utilized)