帮Derby数据库物理资料瘦身

帮Derby数据库物理文件瘦身。
        最近,项目快收尾了,需要清理数据库(Derby):清理不使用的数据,减小数据库大小。

        做完清理,发现数据库的物理文件大小并没有减小。不知道Derby提供什么样的机制,对物理文件进行瘦身?本人英文水平不到入门级,不想看官方英文文档,直接google一下,找到这个链接:http://mxh85154.blog.hexun.com/33808923_d.html,原来Derby提供存储过程来瘦身。

        文章中介绍得也不是很详细,只好自己去看Derby的文档,从文档中看出如下的文字:
SYSCS_UTIL.SYSCS_COMPRESS_TABLE
Use the SYSCS_UTIL.SYSCS_COMPRESS_TABLE system procedure to reclaim unused, allocated space in a table and its indexes. Typically, unused allocated space exists when a large amount of data is deleted from a table, or indexes are updated. By default, Derby does not return unused space to the operating system. For example, once a page has been allocated to a table or index, it is not automatically returned to the operating system until the table or index is destroyed. SYSCS_UTIL.SYSCS_COMPRESS_TABLE allows you to return unused space to the operating system.
Syntax
SYSCS_UTIL.SYSCS_COMPRESS_TABLE (IN SCHEMANAME VARCHAR(128),
IN TABLENAME VARCHAR(128), IN SEQUENTIAL SMALLINT)
SCHEMANAME
An input argument of type VARCHAR(128) that specifies the schema of the table. Passing a null will result in an error.
TABLENAME
An input argument of type VARCHAR(128) that specifies the table name of the table. The string must exactly match the case of the table name, and the argument of "Fred" will be passed to SQL as the delimited identifier 'Fred'. Passing a null will result in an error.
SEQUENTIAL
A non-zero input argument of type SMALLINT will force the operation to run in sequential mode, while an argument of 0 will force the operation not to run in sequential mode. Passing a null will result in an error.
SQL example
To compress a table called CUSTOMER in a schema called US, using the SEQUENTIAL option:
call SYSCS_UTIL.SYSCS_COMPRESS_TABLE('US', 'CUSTOMER', 1)

        从文档中可以看出,调用此过程,需要传入3个参数:第一个为数据库的SCHEME,第二个为表名,第三个为一个非零的短整数,而且三个参数都不能为空。按照文档中的介绍,进行尝试,果然奏效,文件大小由850M缩小到40M。不过比较麻烦,需要对每一个表执行一次存储过程。后来,想到可以使用存储过程,一次性将所有表进行清理:在存储过程中,通过语句“SELECT * FROM SYS.SYSTABLES T WHERE T.TABLETYPE = 'T';”查询出所有的用户表,然后进行清理。

        最后有一个疑问,存储过程中,第三个参数的解释不是很清楚:传入不同的短整数,执行结果会有什么不同?
1 楼 macrotea 2011-09-25  
为什么能瘦身?
文件大小由850M缩小到40M?那么恐怖?
是不是把你的表数据压缩了?
2 楼 dk101 2011-09-25  
macrotea 写道
为什么能瘦身?
文件大小由850M缩小到40M?那么恐怖?
是不是把你的表数据压缩了?


瘦身的原理还没去做了解,用得比较浅。
执行存储过程后,seg0目录下的dat文件被缩小,所以起到了压缩的效果。
其实数据库中的数据量很小,应该不到10M,只是Derby在使用一段时间后,其物理文件会变得很大。具体什么原因,现在还没去了解过。
3 楼 macrotea 2011-09-25  
dk101 写道
macrotea 写道
为什么能瘦身?
文件大小由850M缩小到40M?那么恐怖?
是不是把你的表数据压缩了?


瘦身的原理还没去做了解,用得比较浅。
执行存储过程后,seg0目录下的dat文件被缩小,所以起到了压缩的效果。
其实数据库中的数据量很小,应该不到10M,只是Derby在使用一段时间后,其物理文件会变得很大。具体什么原因,现在还没去了解过。

哦,原来这样,感谢指导
不过内嵌数据库我使用的是h2 你可以去了解下,也好用