CodeSmith Community
Your Code. Your Way. Faster!

SQL 2005 NewSequentialID Support

Latest post 06-04-2008 10:09 AM by evolved. 7 replies.
  • 12-11-2007 9:40 AM

    SQL 2005 NewSequentialID Support

     SQL2005NewSequentialID.zip

    Original Thread is loacted here http://community.codesmithtools.com/forums/t/2902.aspx 

    I have run into the newsequentialid() as I to need to create a disconnected pc application.  I have made changes to the latest SVN templates 677 I belive is what I start with.

    The changes I made were to do the following.

     Have Guid Columns with newsequentialid as the default be treated as Identity Rows. This allows you to do an insert with out providing the guid and have the server create the guid and return it to the Nettiers Entities. This is done using the SQL 2005 Output clause and creating a table var as is recommended by microsoft in the SQL 2005 Books Online. I will post my Patch and title it as SQL 2005 NewSequentialID Support . 

    Now its time to move on to modifiying the templates to use SQL Compact with Microsoft Sync Framework. Something tells me this will not be as easy.

     Andy

    Filed under: ,
    • Post Points: 65
  • 12-22-2007 4:47 PM In reply to

    • hesham_d
    • Not Ranked
    • Joined on 11-07-2007
    • Posts 6
    • Points 120

    Re: SQL 2005 NewSequentialID Support

    I read about GUIDs and is use as primary key to solve aoffline and replication problems.

    I need your advice if GUIDs will provide any benefit for the following design.

    I am currently working in an application where  it has mulitple actors (users accounts) types.we used guids as primary keys for actors.

    For other tables (not acotrs) we used IDENTITY columns.

    Me and the team was wondering, is this really benefit if the applicaiton will use central database and will not work in disconnected mode? 

    And if GUIDs is the new trend to use as primary keys, should we use GUIDs as primary keys for all the tables (not just the actors)?

    I read several articles about the GUIDs pros and cons. But I didn't get conclusion about the performance impact of using GUIDs. 

    the NewSequentialID support you provide will solve the indexing issues, is this will solve performace issues?

    Regards 

    Filed under: ,
    • Post Points: 35
  • 12-26-2007 1:49 PM In reply to

    Re: SQL 2005 NewSequentialID Support

    If you do not have a need to use guids such a for a disconnected client application where the client is inserting records or because the table is being shared between different database servers and you need to guarantee the key is unique. Then I would not use GUID as the key in my table. You never want to use a non-sequential key as the cluster index of a table that will have frequent inserts as it will cause performance issues. A Clustered index is used to determine the order to store the data. Inserting a key that is out of order (not sequential) will cause all records that come after the new record in sequence to be shifted downwards before it is stored . If you only have 100 record then no big deal but what if you have 10,000 and the key causes 9000 to need to be shifted while performance will drop off. The next question I normally get asked is why do I need them stored in any particular order any ways.

    A clustered index is the fastest way to search a collection of records with out having to table scan (search each record). Since primary keys are the most searched, typically they are also the clustered index for the table. It is always faster to find or search a sorted list then it is to search unsorted list as you can jump to the middle and compare that key to the key being searched and immediately cut the list in half knowing that the value you are looking for is in the first half or second half based on if the value is greater or less then the middle value.

    The NetTiers patch I posted just causes the NetTiers to treat the Guid primary key with the default value set to NewSequentialID to be treated more like an identity then it previously did. I also added the code to retrieve the NewSequantialID and update the entity similar to how Identity works.My patch will only work with SQL 2005 or greater. Hope this helps. Andy 

     

    • Post Points: 35
  • 03-27-2008 8:27 PM In reply to

    • Malakai
    • Not Ranked
    • Joined on 12-12-2007
    • Posts 3
    • Points 55

    Re: SQL 2005 NewSequentialID Support

    Can someone with source control rights please integrate this patch?

    It's getting very difficult to integrate by hand as the branche diverges.

    Being that NewSequentialId is a SQL 2005 feature there should be no issue in using 2005 OUTPUT clause to support server generated sequential GUID's.

    I have to wonder why this was never implemented before for any PK column with a default value. The following should occur:

    [ for each PK column with a default value ]
    DECLARE @PK_WITH_DEFAULT [TYPE]
    SET @PK_WITH_DEFAULT = [DEFAULT_CODE]

    [Build normal INSERT INTO clause]
    [Augment VALUES clause to include temporary variables]

    [Build SELECT statement for any generated key column (outputs) 

     e.g.

    DECLARE @PK_WITH_DEFAULT UNIQUEIDENTIFIER
    SET @PK_WITH_DEFAULT = (newsequentialid())

    INSERT INTO [dbo].[Table]
     (
     [PK_WITH_DEFAULT]
     ,[COLOUMN_A]
     ,[COLOUMN_B]
     )
    VALUES
     (
     @PK_WITH_DEFAULT
     ,[COLOUMN_A]
     ,[COLOUMN_B]
     )


    SELECT @PK_WITH_DEFAULT

     The output clause is nice in you don't have to declare a var and select it out, but honestly, being it's generated SQL who cares. The above concept would work for any server generated default, not just guid based ones. You could have some function calld fn_get_nex_raffle_id() and the above code would still work.

     

     

    • Post Points: 35
  • 03-28-2008 9:09 AM In reply to

    Re: SQL 2005 NewSequentialID Support

    Just a heads up to anyone using this patch to work with the Sync Framework from Microsoft. The Compact Database version 3.5 does not support NewSequentialID. Also if you made the same mistake I did you would think that Microsoft would have allowed for bidirection support of the NewSequentialID but they did not. What I mean buy this is that on the client when a new record is created while disconnected a new sequentialid would be created client side but after a sync this guid would get updated to the real id that is created server side. Since the ID is unique (its a guid) they could look it up by its client guid then update it to its new server generated guid. Then using the foreign key relationship also go update the guid in all the foreign keys. Well from the posts I have seen they are still working on this and it may make the next version of the sync frame work.

     

    Also if you are trying to use the Sync Framework and Combact Datbase version 3.5 with nettiers you will need to down load the Microsoft Enterprise Library sorce code and update the Microsoft.Practices.EnterpriseLibrary.Data.Sqlce.dll to use the 3.5 System.Data.SqlServerCE dll as no matter what I tried with application config I could not get it to use the correct compact dll. Updating the Enterprise library is as easy as removing a reference and adding the correct version, building and replacing.

    Hope this help

    Andy

     

    • Post Points: 5
  • 05-22-2008 8:04 PM In reply to

    • blake05
    • Top 25 Contributor
    • Joined on 04-03-2008
    • Wisconsin
    • Posts 352
    • Points 6,210

    Re: SQL 2005 NewSequentialID Support

     Hello,

    Thank you for your contribution, we have added this to our to-do list. You can check the status of this here

    Thanks

    -Blake

    Blake Niemyjski

    CodeSmith Tools, LLC Support Specialist

    Blog: http://windowscoding.com/blogs/blake/

    ----------------------------------------------------------------------
     Member of the .NetTiers team | Visit http://www.nettiers.com
    ----------------------------------------------------------------------

    • Post Points: 35
  • 05-29-2008 10:23 PM In reply to

    Re: SQL 2005 NewSequentialID Support

    Blake

    Along the same lines of NewSequentialID, I have created a SQLCEProvider with a Background Sync Engine based on the Microsoft Sync Framework 3.5. I have made so many changes to the templetes to remove calls and syntax not supported be CE that its been hard for me to keep track.  The biggest changes are SQL Syntax and that SQLCE does not support out parameters so you have to return parmemeters with a select and then set the entity properties in code.

     Also there are some SyncFramework Templates that do thing like create the Tombstone tables and trigger and optional create the alter sql scripts to convert the int ids to Guids as if you are like me you never use guids as ids until you run into a project that requires disconnected clients.

    Plus I am certain I am only using about half of the features of the nettiers data layers so I know there is going to be things I missed in the CE provider. An example is I never use bulk insert so I am not sure if it will work or not in SQLCE but I left it in the provider. 

    Is there a nettiers Admin who wants to take on merging my changes as without me explaining how it works it could get confusing? So I don't think just posting a patch work.

     Thanks

     

    Andy

     

    Andy

    • Post Points: 35
  • 06-04-2008 10:09 AM In reply to

    • evolved
    • Top 50 Contributor
    • Joined on 12-27-2004
    • South River, NJ
    • Posts 93
    • Points 1,825

    Re: SQL 2005 NewSequentialID Support

    I really just wanted to thank you for your contribution, I am keeping a keen eye on the sync work you are doing too. Thanks so much for your time and effort.

     

    .j 

    http://www.jheidt.com
    ------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    ------------------------------

    • Post Points: 5
Page 1 of 1 (8 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems