Using SQL Server 2005, upgrading to 2012
If I have an ETL the does the following(Simplified)
TRUNCATE TABLE detination
INSERT INTO detination
SELECT *
FROM source
Does this clear the index and rebuild it with the inserts? Will I have fragments?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assume it would not truncate the indexes. That would mean the database was physically inconsistent. So it cannot be this way.
Truncate logically removes all rows and physically creates fresh b-trees for all partitions. As the trees are fresh no fragmentation exists.
Actually I’m not sure if the trees have 0 or 1 pages allocated to them. But it doesn’t matter. I believe for temp tables there is a special case that has to do with temp table caching. Also doesn’t matter.
The insert from your question works the same way as any other insert. It is not influenced by the previous truncate in a cross-statement communication way. Whether it causes fragmentation is dependent on your specific case and, IMHO, best-placed in a new question.