SQLite 表磁盘使用情况
如何找出 SQLite 数据库中单个表的磁盘使用情况,而不将其复制到新的空数据库中?
How can I find out the disk usage of a single table inside a SQLite database without copying it in a new empty database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 sqlite3_analyzer www.sqlite.org/download.html" rel="noreferrer">https://www.sqlite.org/download.html。
这是一个非常酷的工具。它显示每个带索引和不带索引的表使用的页数(默认情况下,每个页为 1024 字节)。
这是 Northwind 数据库的 sqlite3_analyzer 输出示例:
它还生成 SQL 语句,可用于创建包含使用情况统计信息的数据库,然后您可以对其进行分析。
You can use sqlite3_analyzer from https://www.sqlite.org/download.html.
It's a really cool tool. It shows you the number of pages used by each table with and without indexes (each page, by default, is 1024 bytes).
This is a sample sqlite3_analyzer output for the Northwind database:
It also generates SQL statements which can be used to create a database with usage statistics, which you can then analyze.
可以从 DBSTAT 虚拟中获取每个表或索引使用的所有页面的详细信息表,也可以聚合它来获取每个表或索引的磁盘使用情况。
例如,运行此查询以获取使用最多磁盘空间的前 10 个表:
It is possible to get details about all the pages used by each table or index from the DBSTAT virtual table, and it is also possible to aggregate that to get the disk usage of each table or index.
For example, run this query to get the first 10 tables using the most disk space:
如果您使用的是 Linux 或 OSX,或者有 unix 实用程序 awk(以及可选的排序)可用,您可以执行以下操作以通过转储分析获取计数和估计大小:
返回:
并使用 awk 脚本:
估计大小为基于“INSERT INTO”命令的字符串长度,因此不会等于磁盘上的实际大小,但对我来说,计数加上估计大小比其他替代方案(例如页计数)更有用。
If you are on linux or OSX, or otherwise have the unix utilities awk (and optionally, sort) available, you can do the following to get counts and estimated size via dump analysis:
which returns:
and uses awk script:
The estimated size is based on the string length of the "INSERT INTO" command, and so is not going to equal the actual size on disk, but for me, count plus the estimated size is more useful than other alternatives such as page count.
我意识到这个答案完全违反了问题的精神,但它确实可以让您在不复制文件的情况下获得大小......
I realize that this answer totally violates the spirit of the question, but it does get you the size without copying the file...
我在这里遇到了其他答案的问题(即 sqlite_analyzer 不适用于 Linux)。 '最终创建了以下 Bash 函数来(临时)将每个表写入磁盘,作为评估磁盘大小的一种方式。从技术上讲,这是复制数据库,这不符合OP问题的精神,但它给了我我想要的信息。
例子:
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.
Example: