Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Drop and Create table or Trunc table Expand / Collapse
Author
Message
Posted Monday, October 28, 2013 6:07 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, February 10, 2014 6:04 AM
Points: 52, Visits: 123
Hi All,

I have a scenario in which I have to delete rows in a MS SQL Server table and load it. The rows will be approx. 200,000 with 10 columns.

I'm thinking of two options. 1) Truncate rows and load. 2) Drop and Create table and load. It does not have any Foreign key. Which one is good?

Please suggest..

Cheers
Jim
Post #1508825
Posted Monday, October 28, 2013 7:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:24 PM
Points: 11,990, Visits: 11,007
Truncate and load.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1508871
Posted Monday, October 28, 2013 7:29 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 12:20 PM
Points: 41,529, Visits: 34,445
They'll both do pretty much the same thing, the DROP/CREATE is more typing and more prone to typing errors.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1508873
Posted Friday, November 08, 2013 9:35 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 10, 2014 10:37 AM
Points: 25, Visits: 325
You can do some thing like this

select * into TempTable
from OldTable

-- Here you can use a where condition to get only the records you wanted.
--or you can delete the records in the TempTable

DROP TABLE OldTable
EXEC sp_rename 'TempTable','OldTable'

This will get your table back with the columns you want
Post #1512731
Posted Monday, November 11, 2013 5:30 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, February 16, 2014 12:38 AM
Points: 15, Visits: 51
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.
Post #1513088
Posted Monday, November 11, 2013 2:40 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
Honny (11/11/2013)
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.


I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1513274
Posted Tuesday, November 12, 2013 11:48 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 07, 2014 7:35 AM
Points: 1,172, Visits: 2,413
Jeff Moden (11/11/2013)
Honny (11/11/2013)
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.


I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.


I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.


Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Post #1513577
Posted Wednesday, November 13, 2013 8:06 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
wolfkillj (11/12/2013)
Jeff Moden (11/11/2013)
Honny (11/11/2013)
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.


I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.


I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.


So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1513912
Posted Wednesday, November 13, 2013 8:58 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 07, 2014 7:35 AM
Points: 1,172, Visits: 2,413
Jeff Moden (11/13/2013)
wolfkillj (11/12/2013)
Jeff Moden (11/11/2013)
Honny (11/11/2013)
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.


I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.


I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.


So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?


Jeff Moden (11/13/2013)
wolfkillj (11/12/2013)
Jeff Moden (11/11/2013)
Honny (11/11/2013)
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.


I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.


I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.


So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?


I don't necessarily think that. As I understood it, SQL_Learning proposed this process, which is great when one wants to keep some of the existing data:

1. SELECT * FROM ExistingTable INTO NewTable WHERE (some conditions that define which rows should not be deleted)
2. DxROP TABLE ExistingTable
3. Rename NewTable to ExistingTable.

As I understood it, Jim1234 doesn't want to keep ANY rows from ExistingTable - he just wants to replace that data with new data. SQL_Learning's suggestion solves a different problem.

As an aside, a process involving a DxROP TABLE step isn't possible if there are any schema-bound dependencies on the table - the dependent objects have to dropped first. TRUNCATE is unaffected by schema-bound dependencies. If what you need to do is flush the data from a table in preparation for replacing it with new data, truncating instead of dropping the table means the process doesn't break when someone decides they need an indexed view over the table.


Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Post #1513938
Posted Wednesday, November 13, 2013 5:10 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
wolfkillj (11/13/2013)
Jeff Moden (11/13/2013)
wolfkillj (11/12/2013)
Jeff Moden (11/11/2013)
Honny (11/11/2013)
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.


I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.


I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.


So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?


Jeff Moden (11/13/2013)
wolfkillj (11/12/2013)
Jeff Moden (11/11/2013)
Honny (11/11/2013)
As you mentioned there is no Foreign Keys so truncate and load is better.
1. If you drop the table again you have to create the table and load.
2. You have drop the Table if there is any changes in he table structure from previous load that time it is better way but in your case truncate and load is better.


I don't believe so. I believe that the SELECT/INTO solution that "SQL_Learning" proposed will do the trick and no modifications to the table will be required if the source-query ever changes.


I don't think SQL_Learning's suggestion meets the requirement. It sounds like Jim1234 wants to do is flush ALL the data from a table and replace it with entirely new data. TRUNCATE and INSERT (or BULK INSERT or other means of populating a table) accomplishes that goal. SQL_Learning's suggestion fits the requirement to delete some rows while retaining others from a table.

Now, as to whether truncating the table and reloading it entirely anew is the best choice to meet the *business* requirements, I don't know - Jim1234 didn't provide enough information about his process to determine whether he really ought to be truncating and reloading or doing an INSERT/UPDATE.


So why would you think that dropping the table and rebuilding it using SELECT/INTO wouldn't fit the bill?


I don't necessarily think that. As I understood it, SQL_Learning proposed this process, which is great when one wants to keep some of the existing data:

1. SELECT * FROM ExistingTable INTO NewTable WHERE (some conditions that define which rows should not be deleted)
2. DxROP TABLE ExistingTable
3. Rename NewTable to ExistingTable.

As I understood it, Jim1234 doesn't want to keep ANY rows from ExistingTable - he just wants to replace that data with new data. SQL_Learning's suggestion solves a different problem.

As an aside, a process involving a DxROP TABLE step isn't possible if there are any schema-bound dependencies on the table - the dependent objects have to dropped first. TRUNCATE is unaffected by schema-bound dependencies. If what you need to do is flush the data from a table in preparation for replacing it with new data, truncating instead of dropping the table means the process doesn't break when someone decides they need an indexed view over the table.


I believe you might be a little confused as to what SELECT/INTO does. It does not work on an existing table. It's to build a brand new table and populat it on the fly. Nothing old is kept.

Also, TRUNCATE is certainly affected by dependancies. For example, FK's can be a real problem for Truncate. I do agree about the indexed view over the table causing a problem with a DROP, though.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1514110
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse