Lots of Key Lookups vs. UniqueIdentifier Clustered Index

  • Hello.

    I'm working on a system that was originally designed with UUID (uniqueIdentifier) clustered indexes.

    Later the clustered indexes were rebuilt on an INT identity column to boost the performance of the index. Because the schema (and code base) reference the UUIDs as primary keys everywhere this was never changed.

    I'm now concerned that we are doing an extraordinary number of key lookups. For example, when ever a table is joined on the UUID primary key, no additional data is in the index so it must be looked up using the new INT.

    My question is: is it better to have all of these key lookups happening or would it be better to just build the clustered indexes on the UUIDs? Alternatively I could include an inordinate amount of columns making the primary key index wide, but at that point wouldn't it be similar to reorganizing my clustered index on the UUID?

    Any thoughts would be appreciated.

    Thanks, Dave

  • Just curious, is this another design where the UUIDs are generated in the app tier within disconnected memory-resident objects and then committed to the database when the user likes what they have created in the application?

    For the database, there is no way to say which would be better without being able to study the differences in performance while a representative workload, before and after changes were made to the indexes in place.

    Ideally the app would be rewritten to make use of clustered surrogate keys so the UUID columns could be sent on their way, i.e. dropped.

    However, that would likely be cost prohibitive. Why not go back to clustering on the UUID and invest in SSD drives which in effect make index fragmentation a non-issue?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Hi opc.three. Thanks for the reply.

    Yeah, for the most part the UUIDs are generated in the app layer, though there is some default constraints in the dB.

    As luck would have it, the dB I'm referring to is running on SSS (Fusion-IO). Do you think that on SSS it's a non-issue to have non-consecutive cluster index keys? Is the only issue index fragmentation? Any performance issues with the dB trying to "rebalance" the tree or is it just doing page splits and inserting the new keys?

    Thanks!

  • You'll have to test because as usual, it will depend on your workload and data. Here is a great place to start:

    Does Index Fragmentation Matter with SSD’s?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • opc.three (11/16/2012)


    You'll have to test because as usual, it will depend on your workload and data. Here is a great place to start:

    Does Index Fragmentation Matter with SSD’s?

    Apologies... I don't understand. What do SSD's have to do with SSS?

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

  • dave-L (11/14/2012)


    Hello.

    I'm working on a system that was originally designed with UUID (uniqueIdentifier) clustered indexes.

    Later the clustered indexes were rebuilt on an INT identity column to boost the performance of the index. Because the schema (and code base) reference the UUIDs as primary keys everywhere this was never changed.

    I'm now concerned that we are doing an extraordinary number of key lookups. For example, when ever a table is joined on the UUID primary key, no additional data is in the index so it must be looked up using the new INT.

    My question is: is it better to have all of these key lookups happening or would it be better to just build the clustered indexes on the UUIDs? Alternatively I could include an inordinate amount of columns making the primary key index wide, but at that point wouldn't it be similar to reorganizing my clustered index on the UUID?

    Any thoughts would be appreciated.

    Thanks, Dave

    You might be able to fix this. I haven't tested it but it might work.

    {EDIT} My apologies for posting a guess instead of a tested solution. This "hack" doesn't work. Please see my post further down for the test code.

    Make the clustered index on the IDENTITY and the GUID column and then change the criteria in the queries to be where SomeIdentityColumn > 0 AND SomeGuidColumn = SomeGuidValue (or whatever). It's a nasty hack but might work.

    The advantage here is that the IDENTITY column is first in the index and would virtually eliminate page splits. The change in criteria I mention would cause the clustered index to still be used thereby eliminating the lookups even when you lookup a GUID.

    Like I said, not sure it'll work but it seems very likely that it would.

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

  • Jeff Moden (11/17/2012)


    opc.three (11/16/2012)


    You'll have to test because as usual, it will depend on your workload and data. Here is a great place to start:

    Does Index Fragmentation Matter with SSD’s?

    Apologies... I don't understand. What do SSD's have to do with SSS?

    Fusion IO is a solid state solution. The ones I have used fit into a PCI Express slot. Therefore I assumed that SSS (sic) meant Solid State Storage which some people say in place of SSD, or Solid State Drives.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • opc.three (11/17/2012)


    Jeff Moden (11/17/2012)


    opc.three (11/16/2012)


    You'll have to test because as usual, it will depend on your workload and data. Here is a great place to start:

    Does Index Fragmentation Matter with SSD’s?

    Apologies... I don't understand. What do SSD's have to do with SSS?

    Fusion IO is a solid state solution. The ones I have used fit into a PCI Express slot. Therefore I assumed that SSS (sic) meant Solid State Storage which some people say in place of SSD, or Solid State Drives.

    Ah... got it. Thanks, Orlando. I haven't had the pleasure of working with SSDs, yet.

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

  • Jeff Moden (11/17/2012)


    You might be able to fix this. I haven't tested it but it might work.

    Make the clustered index on the IDENTITY and the GUID column and then change the criteria in the queries to be where SomeIdentityColumn > 0 AND SomeGuidColumn = SomeGuidValue (or whatever). It's a nasty hack but might work.

    The advantage here is that the IDENTITY column is first in the index and would virtually eliminate page splits. The change in criteria I mention would cause the clustered index to still be used thereby eliminating the lookups even when you lookup a GUID.

    Like I said, not sure it'll work but it seems very likely that it would.

    That should certainly eliminate the fragmentation, but wouldn't it always produce a CI can rather than a CI seek? Is that really less damaging than the fragmentation caused by just using the GUIDs?

    Tom

  • You might be right, Tom. A CI Seek followed by a range scan starting at the first row will be no better than a CI scan. I need to check a bit more deeply.

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

  • Ok... Tom is correct. The "hack" is no good (I should have checked more deeply before I suggested it :blush:). It looks mighty fine on the Execution Plan because it does return as a Clustered Index Seek but it only seeks to the logical "first" row in the table and then continues as a scan.

    It's better to put indexes on the two columns and make sure that you have a good FILL FACTOR on the index for the GUID. The "Key Lookups" for queries against the GUID column are much more efficient than the false seek that the hack produces.

    If you want to see for yourself, here's the code that I should have used before saying anything about the possible hack. Apologies again for suggesting the hack without further testing. :blush:

    --===== Conditionally drop the test table to make reruns easier in SSMS

    IF OBJECT_ID('tempdb..#TestTable','U') IS NOT NULL

    DROP TABLE #TestTable

    ;

    --===== Create and populate the test table on-the-fly

    SELECT TOP 1000000

    SomeID = IDENTITY(INT,1,1),

    SomeGUID = ISNULL(NEWID(),NEWID()), --ISNULL just to make the resulting column NOT NULL

    SomeDate = ABS(CHECKSUM(NEWID()))%DATEDIFF(dd,'2000','2010')+CAST('2000' AS DATETIME),

    SomeInt = ABS(CHECKSUM(NEWID()))%100000

    INTO #TestTable

    FROM master.sys.all_columns ac1

    CROSS JOIN master.sys.all_columns ac2

    ;

    --===== Create the clustered index featured in the "hack".

    -- Used alone, this creates a very misleading Clustered Index Seek

    -- which is really a full table scan in disguise.

    CREATE UNIQUE CLUSTERED INDEX IX_XRef ON #TestTable (SomeID, SomeGUID)

    ;

    --===== If you add this index, you get seeks with Key Lookups. If you

    -- look at the IO statistics it produces, it's much more effecient

    -- than the hack. Apologies for suggesting the hack without testing it.

    --CREATE NONCLUSTERED INDEX [IX_SomeGuid]

    --ON [dbo].[#TestTable] ([SomeGUID])

    --;

    --===== Select a GUID from somewhere in the table.

    -- We're just getting a GUID that's know to exist in the table.

    DECLARE @SomeGUID UNIQUEIDENTIFIER;

    SELECT @SomeGUID = SomeGUID

    FROM #TestTable

    WHERE SomeID = 1

    ;

    SET STATISTICS IO ON;

    --===== Now, demonstrate that we can get a CI seek when looking

    -- for a GUID using the ">" hack... BUT IT'S A FALSE SEEK

    -- BECAUSE IT FINDS THE FIRST ROW WHICH HAS A SomeID > 0

    -- AND THEN GOES INTO A SCAN MODE AS ATTESTED TO BY THE

    -- IO STATISTICS.

    SELECT SomeDate, SomeINT

    FROM #TestTable

    WHERE SomeID > 0

    AND SomeGUID = @SomeGUID

    ;

    --===== Now, demonstrate that we get a CI scan if we don't use

    -- the ">" hack.

    SELECT SomeDate, SomeINT

    FROM #TestTable

    WHERE SomeGUID = @SomeGUID

    ;

    SET STATISTICS IO OFF;

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

  • Thanks for all the comments. I have one more idea for addressing this. I'm thinking about partitioning my table. Everything that has already been created with the random UUID on one partition and all new, sequential UUIDs on a new partition.

    The partition with the old UUIDs will get sorted when I convert it to a CI and there will never be any inserts into it again. All new inserts will go into the new partition where I will ensure the UUIDs are sequential.

  • dave-L (11/19/2012)


    Thanks for all the comments. I have one more idea for addressing this. I'm thinking about partitioning my table. Everything that has already been created with the random UUID on one partition and all new, sequential UUIDs on a new partition.

    The partition with the old UUIDs will get sorted when I convert it to a CI and there will never be any inserts into it again. All new inserts will go into the new partition where I will ensure the UUIDs are sequential.

    Just curious, which column(s) are you evaluating as a partition key?

    You mentioned the application sometimes generates UUIDs outside the database, how would you manage that piece of it?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Jeff Moden (11/18/2012)


    make sure that you have a good FILL FACTOR on the index for the GUID.

    Please explain 🙂

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Bhuvnesh (11/20/2012)


    Jeff Moden (11/18/2012)


    make sure that you have a good FILL FACTOR on the index for the GUID.

    Please explain 🙂

    Sure... it's a huge subject with lots of caveats that you need to read about in Books Online but here's a quick overview concerning GUIDs and FILL FACTORs.

    GUIDs in SQL Server are really just huge random numbers (Type 4 GUIDs are used. Type 1 was used in version 7 but were still mostly random in nature). That means that if you have an index with a GUID in it (especially if it's the first column), new data will usually be inserted somewhere in the middle of a page on an index. If the page is full because of a 0 or 100 FILL FACTOR (or other reason), you get a page split on the index. Page splits use a lot of time and a lot of resources and can actually cause some pretty serious "timeouts" on frequently inserted tables. Lookup "page splits" in Books Online for more information.

    Having a lower FILL FACTOR means that some free space is left on every page after and index rebuild or reorg. That free space is used by inserts instead of doing page splits (until the page is filled, of course).

    An 80% FILL FACTOR (for example) will leave approximated 20% free space (depending on row size) on every page after an index rebuild or reorg. The rowsize thing is another good reason to keep columns narrow by using the correct datatype. More rows per page means faster SELECTs.

    Fill factors other than 0 or 100% (0 is technically the same as 100), make SELECTs take longer because you have to read more pages to get the same amount of data so you need to be careful as to which indexes you assign a fill factor to and how big they are. Again, all of that is much better explained in Books Online under "Page Splits".

    And, no... the new SEQUENTIAL GUID datatype won't help for individual inserts. You can read about that in Books Online, as well.

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

Viewing 15 posts - 1 through 15 (of 41 total)

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