最近,项目快收尾了,需要清理数据库(Derby):清理不使用的数据,减小数据库大小。
做完清理,发现数据库的物理文件大小并没有减小。不知道Derby提供什么样的机制,对物理文件进行瘦身?本人英文水平不到入门级,不想看官方英文文档,直接google一下,找到这个链接:[url]http://mxh85154.blog.hexun.com/33808923_d.html[/url],原来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';”查询出所有的用户表,然后进行清理。
最后有一个疑问,存储过程中,第三个参数的解释不是很清楚:传入不同的短整数,执行结果会有什么不同?
做完清理,发现数据库的物理文件大小并没有减小。不知道Derby提供什么样的机制,对物理文件进行瘦身?本人英文水平不到入门级,不想看官方英文文档,直接google一下,找到这个链接:[url]http://mxh85154.blog.hexun.com/33808923_d.html[/url],原来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';”查询出所有的用户表,然后进行清理。
最后有一个疑问,存储过程中,第三个参数的解释不是很清楚:传入不同的短整数,执行结果会有什么不同?