Clean old personalization statistics

Sitefinity's personalization module is great for targeting specific groups of customers and creating leads and that is why it is widely used. Customer segments are created and the content is personalized based on user's activity. For the module to work properly it logs user's statistical data and here is the problem that comes with it. Sitefinity logs those activities in sf_stat_sentence table and if you have a heavily loaded site it can become huge (10+ million records and more).
This has caused me issues with upgrades and also timeouts on personalized pages.

Fortunately there is a spell (which doesn't include an ear from a bat or a hair from a turtle) for this.
The following sql script will clear all records that are older then 3 months (you can adjust the period as you like) and you can create a schedule job:


Notes:
If sf_stat_sentence table is very big, there is a possibility to receive and exception that the transaction log is full. In order to tackle this you need to shrink the log. There are several solution depending on whether you want to keep transaction or not and your SQL version. For SQL 2008 and if you don't care about the transactions you can run the following script (replace placeholders with your values):