How to artificially fragment an index ?

  • I need to increase fragmentation level for an index. This is in production, and the table has a lot of FK relations, so I can't do things like insert/delete. The reason for this is I am debugging my script but the index that I am aiming for has low level of frag.

    Thanks

  • SQL Guy 1 (1/7/2014)


    I need to increase fragmentation level for an index. This is in production, and the table has a lot of FK relations, so I can't do things like insert/delete. The reason for this is I am debugging my script but the index that I am aiming for has low level of frag.

    Thanks

    Why are trying to debug your script in production???

    _______________________________________________________________

    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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • This server is used by 3rd party applications and not frequently used. I want to debug some rare case in my defragmentation script, it happens only in a adatabase that's on this server, and it's not backed up.

  • You can't really fragment an index with modifying the data. Don't test stuff in production.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • OK, let's assume that I am not in production. How can I increase fragmentation without modifying the data?

  • SQL Guy 1 (1/8/2014)


    OK, let's assume that I am not in production. How can I increase fragmentation without modifying the data?

    You can't.

    fragmentation occurs, for example, when data is inserted into the middle of an index, and when rows get added or deleted.

    you could create a new sample table, insert and delete data randomly into that table from the source, and then look at the fragmentation of that test tables indexes both before and after.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • run a dbcc shrinkfile on it, you'll have all the fragmentation you want.

    but not in production 🙂

    ---------------------------------------------------------------------

  • SQL Guy 1 (1/7/2014)


    This server is used by 3rd party applications and not frequently used. I want to debug some rare case in my defragmentation script, it happens only in a adatabase that's on this server, and it's not backed up.

    if you have any control over this, get it backed up even if its only used infrequently

    ---------------------------------------------------------------------

  • Yes, it's always backed up periodically.

    DBCC shrink file did not help. First, it shrinks entire file which also has other tables. And it shrank just little bit because there was not much empty space was there.

    Will alter table with increasing size of varchar columns work?

  • george sibbald (1/8/2014)


    run a dbcc shrinkfile on it, you'll have all the fragmentation you want.

    +1

    but not in production 🙂

    +1000!!!

    --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.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • SQL Guy 1 (1/8/2014)


    Yes, it's always backed up periodically.

    DBCC shrink file did not help. First, it shrinks entire file which also has other tables. And it shrank just little bit because there was not much empty space was there.

    Will alter table with increasing size of varchar columns work?

    Run a select from sys.dm_db_index_physical_stats in the 'Detailed' mode and see just how badly things got fragmented.

    But, I agree... DBCC Shrink File won't help here because it fragments EVERYTHING in the given file.

    Increasing the Size of varchar columns (assuming you mean the width of the column and not the data itself) won't do it.

    To reiterate what others have stated, you really must not do this in prod. Make a copy of the table in a Dev box, instead. As careful as you think you are, doing this in prod is just inviting disaster. Been there and done that. Not going to do it again. 🙂

    --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.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • why does it matter that other tables are fragmented? what is your script trying to do?

    can you force the logic flow to pick up your table?

    ---------------------------------------------------------------------

  • george sibbald (1/8/2014)


    why does it matter that other tables are fragmented? what is your script trying to do?

    can you force the logic flow to pick up your table?

    agreed; what difference does it make WHICH table's indexes you defragment, if you are testing code? if you need to see the amount of time it takes to run, and an estimate won't do, then find the same data on your test server (you have one, right?) and run it there.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Yes, I have in my code: where t.name = <my_table_name>.

    But the level of fragmentation is so low, that my script skips it. And this is exactly the reason why I wanted to increase fragmentation somehow.

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply