Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert Into Using OpenQuery Causes Massive Fragmentation


Insert Into Using OpenQuery Causes Massive Fragmentation

Author
Message
guerillaunit
guerillaunit
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
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.
rVadim
rVadim
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1054 Visits: 2299
Could it be that data that comes from OPENQUERY is in completely different Order then when you copy table to table?
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5806 Visits: 7139
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”
guerillaunit
guerillaunit
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
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?
guerillaunit
guerillaunit
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45267 Visits: 39934
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5806 Visits: 7139
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”
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6002 Visits: 8314
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
guerillaunit
guerillaunit
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
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
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6002 Visits: 8314
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search