Saturday 2 November 2024

Does DDL Statement Generate execution plan during the execution in SQL Server

SQL server does not generate the traditional execution plan for the DDL (Data definition language) command. Due to that CREATE, TRUNCATE, DROP, ALTER etc. do not have execution plan.

See the example.

Creating a table.

CREATE TABLE ddl_operation_demo
  
(
     
id        INT IDENTITY(1, 1),
     
demo_date DATETIME
  
); 

Running this and see the execution plan.

See table has been created but no execution plan is generated.

 Inserting some records

INSERT INTO ddl_operation_demo
            
(demo_date)
SELECT TOP 100 Dateadd(day, Abs(Checksum(Newid())) % 1000, Getdate())
FROM   sys.objects AS o1
       
CROSS JOIN sys.objects AS o2; 

Record Inserted and execution plan is created.

Now altering this table and adding one column

--Table alter
ALTER TABLE ddl_operation_demo
  
ADD column_2 VARCHAR(20); 

No execution plan is generated.


Truncating the table

Run the below script to truncate the table

-- Truncate the table

TRUNCATE TABLE DDL_Operation_Demo;

See the execution plan

Dropping the table

Run below script to drop the table

DROP TABLE ddl_operation_demo; 

See the execution plan


No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts