What is the difference between Shrink and Compact in SQL Server CE?

烈酒焚心 提交于 2019-12-22 04:10:37

问题


I have a method that is run periodically to optimise my application's SQL Server Compact Edition (3.5) database. The existing code uses the Shrink() method:

        SqlCeEngine engine = new SqlCeEngine(dbConnectionString);
        engine.Shrink();

Today I noticed that there's also a Compact() method. Which is preferable for periodic maintenance?


回答1:


From the SQL Server Compact Team Blog:

The difference between these two is much similar to Internal and External Memory Fragmentation.


From SqlCeEngine.Shrink documentation,

Reclaims wasted space in the database by moving empty and unallocated pages to the end of the file, and then truncating the file. You can configure a database to automatically shrink by setting the autoshrink threshold option in connection string. Shrink does not create a temporary database file.

From SqlCeEngine.Compact documentation,

Reclaims wasted space in the database by creating a new database file from the existing file. By creating a new database means, it reclaims the free space between rows.


To be more clear, Shrink claims the pages which are entirely free or unallocated; where as, Compact claims the wasted space with in the page too. Hence Compact requires creating a new database file.



来源:https://stackoverflow.com/questions/707625/what-is-the-difference-between-shrink-and-compact-in-sql-server-ce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!