SQLite table disk usage

后端 未结 5 1046
借酒劲吻你
借酒劲吻你 2021-01-30 08:39

How can I find out the disk usage of a single table inside a SQLite database without copying it in a new empty database?

5条回答
  •  盖世英雄少女心
    2021-01-30 08:53

    I ran into issues with the other answers here (namely sqlite_analyzer not working on Linux). 'Ended up creating the following Bash function to (temporarily) write out each table to disk as a way of assessing the on-disk size. Technically this is copying the db, which is not in the spirit of OP's question, but it gave me the information I was after.

    function sqlite_size() {
      TMPFILE="/tmp/__sqlite_size_tmp"
      DB=$1
      IFS=" " TABLES=`sqlite3 $DB .tables`
      for i in $TABLES; do
        \rm -f "$TMPFILE"
        sqlite3 $DB ".dump $i" | sqlite3 $TMPFILE
        echo $i `cat $TMPFILE | wc -c`
        \rm -f "$TMPFILE"
      done
    }
    

    Example:

    $ sqlite_size sidekick.sqlite
    SequelizeMeta 12288
    events 16384
    histograms 20480
    programs 20480
    
    

提交回复
热议问题