Tips for Optimizing Data Manipulation Language

  • Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/kThaker/tipsforoptimizingdatamanipulationlanguage.asp

  • that article is bookmark worthy. thanks Kalpesh.

  • Great article but I would add a caution on using query hints as they can become invalid as data changes.

  • Hi!

    Thanks for this worthy article.

    I have a question about it: the author states that TRUNCATE TABLE "invalidates the transaction log".  I'm certainly missing the point here; indeed, TRUNCATE TABLE is a fully logged operation that can, for instance, be rollbacked.

    The three troubles underlined, in the BOL, with TRUNCATE TABLE are:

    • It resets IDENTITY columns to the seed.
    • It cannot be used on a table referenced by a FOREIGN KEY.
    • It cannot activate a trigger.

    Kalpesh, maybe you meant one of these three?

    Thanks!

    Xavier

  • Hey,

    Thanks for the info,

    I thing Xavier comments are not 100% accurate. I just wanted to clarify.

    This is from SQL Help. Truncate table is not logged see below

    Thanks

    Joe

    TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause: both remove all rows in the table. But TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE.

    The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log.

    TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.

    You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead, use DELETE statement without a WHERE clause. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.

    TRUNCATE TABLE may not be used on tables participating in an indexed view.

  • TRUNCATE TABLE is logged but in a different way than a normal DELETE.

    From BOL: "The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log."

    It is perfectly possible to rollback a truncate table. 

    create table test

    (f varchar(10))

    insert into test values ('truncate')

    insert into test values ('table')

    BEGIN TRAN

    TRUNCATE TABLE test

    SELECT * FROM test

    ROLLBACK TRAN

    SELECT * FROM test

    drop table test

    I also don't understand the 'invalidates transaction log' statement.  Could you elaborate this please?

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply