Recently, I faced this issue when user open aspx page in SharePoint 2007 page library. The error message is not really helpful and if you look on SharePoint log, it also provide you with same error message “Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries”.
Further googling on the issue, I found other people faced the same issue and it feels related to mine. You could look in here. From that resources, they specified the root of the issue is Workflow History list.
“Workflow History first. The Workflow History list is a hidden list which does exactly what it says on the tin. Items are created each time a workflow runs. It turns out that items in the Workflow History list have a time-to-live and that time is 60 days. That means that any item in the list will automatically be deleted after 60 days. With roughly a 200 item limit before you hit trouble that means about 3 workflows per list item per day is your maximum.”
- Delete items in Workflow History list. You could found simple console application for this here.
- Limit document versions. I deleted old version history and only keep the last 5 major versions.