Index Primer - Just what statistics are kept?

  • Comments posted to this topic are about the item Index Primer - Just what statistics are kept?

    Josef Richberg
    2009 Exceptional DBA
    http://www.josef-richberg.squarespace.com
    http://twitter.com/sqlrunner

  • There's something I'm a bit confused about here...

    It's the following:

    AVG_RANGE_ROWS: 176.7533 -- number of records for each distinct value (those within the DISTINCT_RANGE_ROWS)

    Now how can you have a part of a row? Surely the number of rows for each distinct value whole come out to be a whole number? This seems, as the name suggests, an average.

    And in fact... I just checked this out and this is what Microsoft have to say about this:

    AVG_RANGE_ROWS - "Average number of rows with duplicate column values within a histogram step, excluding the upper bound (RANGE_ROWS / DISTINCT_RANGE_ROWS for DISTINCT_RANGE_ROWS > 0)."

    Random Technical Stuff[/url]

  • Very cool. I never knew any of this stuff and haven't seen very many articles on statistics. From the article I guess I inferred that the statistics help especially if you do have several columns in the index as long as you always include the Prefix column in your queries.

    Of course you always want your most used/important filter column to be first in the index, but this is great to see WHY!

  • Actually this brought up another issue I have always wanted to solve. I have a store/product table mapping, ~83M rows and always growing as stores are added. The stores can have anywhere from a couple to 2.5M products in each. Of course the large stores make up the majority of the 83M rows.

    The problem is that whenever I update the product mix, I'm deleting/updating/inserting up to 2.5M rows in the 83M row table at a time. We threw lots of hardware at it, and brought 15 minutes down to 2-3 minutes, but still I'm trying to insert 2.5M records into an 83M record table and even with 36 hard drives, it just takes time.

    I wanted to use partitioned tables and functions, but that's only in SQL Enterprise edition. But in my research on partitioned tables, I needed to come up with a mechism to group my stores, because you have a limit of 1000 partitions (I think, it's been a while).

    In the statistics they group your prefix column into at most 200 steps. That's pretty much the same thing I wanted to do for my partitioned function.

    I just wish there was another way that when inserting/deleting the 2.5M records in my table it wouldn't do a total table lock and at 2-3 minutes it causes timeouts on any other store retrieving data from that table.

    Any ideas?

    Thanks!

  • Great article. It discusses index stats in just enough detail to make things clear without bogging down.

  • rbramhall (10/28/2009)


    I just wish there was another way that when inserting/deleting the 2.5M records in my table it wouldn't do a total table lock and at 2-3 minutes it causes timeouts on any other store retrieving data from that table.

    Any ideas?

    Thanks!

    Sounds like this should be desirable behaviour! Because if you are deleting so many records, and one of your stores reads that deleted record and does something with it, then they'd have invalid data... of course, I'm not totally sure what you are trying to do. Same with inserts - if the store did a report that relied on knowing what you had at a point in time, and it *under* reported, then again they aren't getting accurate info.

    Sorry, just realised the way that your table has been put together.

    You can actually stop lock escalation in SQL Server... though that seems a bit drastic. But if you look at the following article it will tell you how to do this. Use at your own risk though!

    However.. I've never really tried using it, but perhaps snapshot isolation might help? Of course, that would put contention on TempDB... someone might want to chime in here if I'm telling anything misleading 🙂

    Random Technical Stuff[/url]

  • rbramhall (10/28/2009)


    The problem is that whenever I update the product mix, I'm deleting/updating/inserting up to 2.5M rows in the 83M row table at a time. We threw lots of hardware at it, and brought 15 minutes down to 2-3 minutes, but still I'm trying to insert 2.5M records into an 83M record table and even with 36 hard drives, it just takes time.

    I would think this question is better suited to one of the code forums than to discussion on an excellent article on index statistics, but...

    If all you have is a product-store mapping (storeid, productid) I can't see that inserting 2.5 M rows would take 3 minutes. Therefore I suspect that you have a lot of other stuff in the table.

    That being the case, I think my first instinct would be to partition the table on storeid. That doesn't mean that you have to have one partition per store: maybe the bit stores each have their own and the smaller ones are grouped together. If you've got tables with ~100M rows in them, you probably should be using Enterprise.

    If there is no way to make that work for some reason (cultural or otherwise) I would try a phased insert/update/delete, working with 10,000 rows at a time. That won't improve the end-to-end speed of your process, but if you choose the right number it will prevent lock escalation.

    Other people may have ideas regarding such things as BULK INSERT, but I haven't worked with it much.

  • ta.bu.shi.da.yu (10/28/2009)


    rbramhall (10/28/2009)


    I just wish there was another way that when inserting/deleting the 2.5M records in my table it wouldn't do a total table lock and at 2-3 minutes it causes timeouts on any other store retrieving data from that table.

    Any ideas?

    Thanks!

    Sorry, just realised the way that your table has been put together.

    You can actually stop lock escalation in SQL Server... though that seems a bit drastic. But if you look at the following article it will tell you how to do this. Use at your own risk though!

    However.. I've never really tried using it, but perhaps snapshot isolation might help? Of course, that would put contention on TempDB... someone might want to chime in here if I'm telling anything misleading 🙂

    I'll check out the article. I looked into the Snapshot Isolation before we went from 4 RAID 5 drives to 36 drives. Now that I have the TempDB Data on 4 drives in RAID 10 and TempDB Log on 4 drives in RAID 10, I'll definitely look back into the Snapshot Isolation. Thanks.

  • Dean Cochrane (10/28/2009)


    rbramhall (10/28/2009)


    The problem is that whenever I update the product mix, I'm deleting/updating/inserting up to 2.5M rows in the 83M row table at a time. We threw lots of hardware at it, and brought 15 minutes down to 2-3 minutes, but still I'm trying to insert 2.5M records into an 83M record table and even with 36 hard drives, it just takes time.

    I would think this question is better suited to one of the code forums than to discussion on an excellent article on index statistics, but...

    Sorry, will do next time, this literally just popped into my head when I read it, and not used to posting to this site.

    If all you have is a product-store mapping (storeid, productid) I can't see that inserting 2.5 M rows would take 3 minutes. Therefore I suspect that you have a lot of other stuff in the table.

    Yes, I have a couple money columns, date columns and a few other flags and 9 indexes on it too.

    That being the case, I think my first instinct would be to partition the table on storeid. That doesn't mean that you have to have one partition per store: maybe the bit stores each have their own and the smaller ones are grouped together. If you've got tables with ~100M rows in them, you probably should be using Enterprise.

    We're getting there, my latest plan is to completely rewrite and go a more rules driven and utilize temp tables or my own static tables per store. For individual stores that solves the problem, but then we would have to build a data warehouse for doing system wide reporting.

    The customer would rather spend $100k on services, than spend $100k buying the needed Enterprise licenses from MS and then still have to do services on top of it to utilize the Enterprise licenses!

    If there is no way to make that work for some reason (cultural or otherwise) I would try a phased insert/update/delete, working with 10,000 rows at a time. That won't improve the end-to-end speed of your process, but if you choose the right number it will prevent lock escalation.

    Definitely another option. Thanks!

  • If you're inserting 2.5 million records into a table then you're index will be severly defragmented. You should consider rebuilding all indexes involved or at least reorganize them. And aslo: While you're deleting and inserting 2.5 million records, does your other select-queries really need to be read committed? how about putting in a WITH (NOLOCK) in your select statements - that way your deletes and inserts will have an exclusive table lock while working, but your selects will still work - though you'll might get some dirty reads..

  • Another possibility is to use a while statement and delete/insert a smaller amount of records on each iteration so that the optimizer will decide to use a lower level of locking like page locks. This way you can avoid dirty reads because select statements will wait, but only until the current batch is complete.

    While 0=0

    Begin

    Delete/Insert top (50000) TableAlias

    From TableName TableAlias

    Join etc

    Where etc

    If @@RowCount < 50000

    Break

    End

    One implication of this method is that each batch is it's own transaction, so the whole thing cannot be committed/rolled back. Additionally this will ultimately take longer than executing full delete/insert in one fell swoop, but the duration on any particular row or page being locked will be decreased.

    P.S. It probably goes without saying, but - you would need to change the 50000 to whatever makes sense.

  • Well Done.

    I get better understanding with your examples plus your calculations.

    One question:

    Does it mean that index for C105_CompanyCode, and index for C105_Statement_Acct might be 'dropped', when a composite index of "C105_CompanyCode,C105_StatementAcct " is available?

    C105_CompanyCode

    C105_Statement_Acct

    C105_CompanyCode,C105_StatementAcct

    One suggestion:

    If you could combine with dbcc showcontig, that might be very interesting for a cross over understanding.

    Thanks for your contribution.

    -David

  • Thank you very much for an extremely informative and eye opening article. Much appreciated and, for me, it's quite a well timed read.

    Thank you.

Viewing 13 posts - 1 through 12 (of 12 total)

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