|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 8:48 AM
Points: 149,
Visits: 329
|
|
I use the following code to import a table from an Oracle DB to SQL Server every day:
TRUNCATE TABLE mytable INSERT INTO mytable (firstname, lastname, city, state) SELECT * FROM OPENQUERY (mylinkedserver, 'SELECT firstname, lastname, city, state FROM mylinkedtable') The above causes massive fragmentation (90% to 99%) on my nonclustered indexes on "mytable". If I run the below code
SELECT * INTO mytesttable FROM mytable TRUNCATE TABLE mytable INSERT INTO mytable SELECT * FROM mytesttable DROP TABLE mytesttable I end up with fragmentation of between 1% and 3% on my nonclustered indexes. What is causing this? Both statements are truncating the table and inserting new data into a blank table.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 3:12 PM
Points: 890,
Visits: 2,070
|
|
Could it be that data that comes from OPENQUERY is in completely different Order then when you copy table to table?
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 3,164,
Visits: 4,344
|
|
Please bear in mind that there is no guaranteed ORDER BY in any RDBMS. Consider adding an ORDER BY (outside the OPENQUERY function) on the data source column equivalent of your clustered index, if at all possible e.g.
INSERT MyLocalTable (Col1, Col2, Col3) SELECT Col1, Col2, Col3 FROM OPENQUERY(MyLinkedServer,'SELECT Col1,Col2,Col3 FROM RemoteTable') ORDER BY Col1
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 8:48 AM
Points: 149,
Visits: 329
|
|
| I have no clustered index on the table. Will the order of the copy from OpenQuery affect fragmentation even without a clustered index?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 8:48 AM
Points: 149,
Visits: 329
|
|
| Inserted an ORDER BY on the non-clustered indexes. Fragmentation improved a little bit, but still looking at 50%-90% per index
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
|
|
Based on the fragmentation you experienced with OPENQUERY, I truly wonder if OPENQUERY is set-based or some form of "Hidden RBAR". The order of data on a single insert shouldn't matter to the indexes. There shouldn't be any fragmentation on a freshly truncated table that has only had a single insert applied to it.
--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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 8:26 AM
Points: 3,164,
Visits: 4,344
|
|
guerillaunit (8/15/2012) I have no clustered index on the table. Will the order of the copy from OpenQuery affect fragmentation even without a clustered index? Thiis should not cause fragmentation.
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 3:22 PM
Points: 3,678,
Visits: 5,177
|
|
Since you are truncating the table first and then inserting rows, it is SIGNIFICANTLY better to drop the nonclustered indexes, insert the data, and then recreate the nonclustered indexes. Better performance, less logging, less fragmentation.
Best,
Kevin G. Boles SQL Server Consultant SQL MVP 2007-2012 TheSQLGuru at GMail
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, January 08, 2013 8:48 AM
Points: 149,
Visits: 329
|
|
Hi Kevin, Is there a reason why you would need to drop the nonclustered indexes after truncating a table and before inserting new data to prevent fragmentation? I am under the impression that inserting data into a just truncated table with nonclustered indexes would not cause fragmentation
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 3:22 PM
Points: 3,678,
Visits: 5,177
|
|
guerillaunit (8/16/2012) Hi Kevin, Is there a reason why you would need to drop the nonclustered indexes after truncating a table and before inserting new data to prevent fragmentation? I am under the impression that inserting data into a just truncated table with nonclustered indexes would not cause fragmentation
What if the data comes in in "random" order?? Seems logical in that case that you would get (potentially massive, like you see) fragmentation, right?
Best,
Kevin G. Boles SQL Server Consultant SQL MVP 2007-2012 TheSQLGuru at GMail
|
|
|
|