views:

52

answers:

2

In Sql Server, I find that, when I mark a database as read only, its existing large transaction log remains. To fix this I have to set it back to writeable, then dbcc shrinkfile on the log file, then set it read-only again.

What is the use of a transaction log if the database is read only? Is there a reason it doesn't just get deleted/flushed?

+1  A: 

There is no use for a transaction log on a read-only db :-) Assuming you are past some point at which you no longer care about the transactions done to populate it. I imagine it's not flushed because that would not be good default behaviour, imagine if you wanted to go back to write mode? Or you discovered during read-only operation that there was a problem and you needed to use the logs?

fredley
+2  A: 

If your database is still in full recovery mode, the log will not shrink without a proper backup. You should switch your read-only database to simple recovery.

Also, the log file is needed should you ever decide to detach/attach this database. As noted here:

In contrast, for a read-only database, the log cannot be rebuilt because the primary file cannot be updated. Therefore, when you attach a read-only database whose log is unavailable, you must provide the log files or files in the FOR ATTACH clause.

Joe Stefanelli
Switching the database to read-only actually prevents the auto-shrink that would normally happen in simple recovery mode.
GlennS