了解数据库增长
确定图数据库大小的最简单方法是通过文件系统,并计算命名为 *store.db*
的文件大小总和。例如,在 Linux 环境中可以运行
$ du -hc $NEO4J_HOME/data/databases/graph.db/*store.db*
这应该在数据库停止时运行,或者在最近完成检查点(即备份之后立即)时运行
这会产生类似于以下的输出
5.5M neostore.labelscanstore.db 8.0K neostore.labeltokenstore.db 4.0K neostore.labeltokenstore.db.id 8.0K neostore.labeltokenstore.db.names 4.0K neostore.labeltokenstore.db.names.id 72M neostore.nodestore.db 4.0K neostore.nodestore.db.id 8.0K neostore.nodestore.db.labels 4.0K neostore.nodestore.db.labels.id 196M neostore.propertystore.db 8.0K neostore.propertystore.db.arrays 4.0K neostore.propertystore.db.arrays.id 4.0K neostore.propertystore.db.id 8.0K neostore.propertystore.db.index 4.0K neostore.propertystore.db.index.id 8.0K neostore.propertystore.db.index.keys 4.0K neostore.propertystore.db.index.keys.id 8.0K neostore.propertystore.db.strings 4.0K neostore.propertystore.db.strings.id 8.0K neostore.relationshipgroupstore.db 4.0K neostore.relationshipgroupstore.db.id 0 neostore.relationshipstore.db 4.0K neostore.relationshipstore.db.id 0 neostore.relationshiptypestore.db 4.0K neostore.relationshiptypestore.db.id 8.0K neostore.relationshiptypestore.db.names 4.0K neostore.relationshiptypestore.db.names.id 8.0K neostore.schemastore.db 4.0K neostore.schemastore.db.id 273M total
其中最后一行报告图数据库的总大小为 273M。这不包括 Neo4j 事务日志文件 (neostore.transaction.*) 的大小,因为这些文件的大小和保留策略是用户可配置的。
在上面的列表中,输出取自一个包含 500 万个节点的图数据库,所有节点都有 :Person
标签,每个节点都有一个名为 id
的属性,其值从 1 到 500 万。这些数据是通过运行以下等效命令准备的:
USING PERIODIC COMMIT 50000
LOAD CSV WITH HEADERS FROM 'file:///person.csv' AS row
CREATE (:Person { id: row.id} );
因此您会看到 neostore.labelscanstore*、neostore.nodestore* 和 neostore.propertystore* 文件占用的空间超过了其默认大小。由于该图数据库没有关系,因此名为 neostore.relationship* 的文件实际上是空的。
显然,随着您添加数据,文件大小会增长。但是,这里有一个需要注意的地方,它可以解释为什么添加更多数据实际上可能导致数据库大小减小。
在上面的文件列表中,以 .id 结尾的文件(例如 neostore.nodestore.db.id
)充当可重复使用 ID 的回收站。由于图数据库最初只填充了 500 万个 :Person 节点,并且没有进行删除操作,所以 neostore.nodestore.db.id
是空的,节点记录在 neostore.nodestore.db
中。然而,如果我们随后删除所有 500 万个节点,我们会发现 neostore.nodestore.db
的大小没有减小,但 neostore.nodestore.db.id
的大小会增加到 39M。而由于这次删除,我们看到图数据库的总大小至少增加了 39M。现在,如果我们重新插入这 500 万个节点,那么 neostore.nodestore.db.id
的大小将会减小,因为在添加新节点时,我们会首先检查是否可以重用之前使用过的 ID,如果可以,就将其从 neostore.nodestore.db.id
中移除。由于 neostore.nodestore.db.id
有 500 万个可以重用的 ID,并且我们添加了 500 万个节点,那么完成此操作后,该文件将接近空(即 4.0 K)。通过添加这些节点,将 neostore.nodestore.db.id
的大小从 39M 减小到 4.0k,数据库的总大小也以同样的方式减小了。请注意,在此过程中,neostore.nodestore.db
的大小没有改变。
总而言之,相对于这两个文件(以及在 neo4j stop
后捕获的文件大小),过程如下:
-
步骤 1: 清空图数据库
0 neostore.nodestore.db 4.0K neostore.nodestore.db.id
-
步骤 2: 添加 500 万个 :Person 节点
72M neostore.nodestore.db 4.0K neostore.nodestore.db.id
-
步骤 3: 删除 500 万个 :Person 节点(例如运行
CALL apoc.periodic.commit("match (n:Person) with n limit {limit} delete n return count(*)",{limit:50000});
)—— 此时图数据库变为空72M neostore.nodestore.db 39M neostore.nodestore.db.id
-
步骤 4: 重新添加 500 万个 :Person 节点
72M neostore.nodestore.db 4.0K neostore.nodestore.db.id
现在,如果您已经完成了上面的步骤 #3,并且不打算添加更多节点,但想要缩小文件大小,您会需要考虑使用 copy-store.sh。这个工具会读取离线数据库,并复制出数据,不会包含任何已写入但不再使用的数据或可重复使用的 ID 列表的开销。
在针对图数据库(如上述步骤 #3 完成时的状态)运行 copy-store.sh 以准备新的图数据库后,新创建的 graph.db 现在定义了这两个文件如下:
0 neostore.nodestore.db 4.0K neostore.nodestore.db.id
此页面有帮助吗?