Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are indexes on temporary tables deleted when the table is deleted?

Would the following SQL remove also the index - or does it have to be removed separately?

CREATE TABLE #Tbl (field int)

CREATE NONCLUSTERED INDEX idx ON #Tbl (field)

DROP TABLE #Tbl
like image 411
Manu Avatar asked Sep 18 '08 11:09

Manu


People also ask

Does deleting table DELETE index?

Dropping a table removes the table definition from the data dictionary. All rows of the table are no longer accessible. All indexes and triggers associated with a table are dropped.

Do temporary tables get deleted?

Temporary tables are similar to permanent tables, except temporary tables are stored in tempdb and are deleted automatically when they are no longer used. There are two types of temporary tables: local and global. They differ from each other in their names, their visibility, and their availability.

Can we use index in temporary table?

You can define indexes on temporary tables. In many cases, these indexes can improve the performance of queries that use tempdb. The optimizer uses these indexes just like indexes on ordinary user tables.

When a table is dropped its indexes are automatically dropped?

All rows from the table are dropped. All table indexes and domain indexes are dropped, as well as any triggers defined on the table, regardless of who created them or whose schema contains them. If table is partitioned, then any corresponding local index partitions are also dropped.


2 Answers

Yes they are. You can search in MSSQL help for CREATE INDEX article it is said there:

"Indexes can be created on a temporary table. When the table is dropped or the session ends, all indexes and triggers are dropped."

like image 132
sdkpoly Avatar answered Sep 24 '22 03:09

sdkpoly


It will be removed automatically, as there is nothing left to index. Think of it as a child object in this respect.

like image 39
Nick Craver Avatar answered Sep 25 '22 03:09

Nick Craver