Click here to Skip to main content
15,891,375 members
Home / Discussions / Database
   

Database

 
QuestionReporting Service - bug with drilldown? Pin
devvvy30-Mar-05 23:56
devvvy30-Mar-05 23:56 
GeneralNHibernate Child-Parent relationship -- Key happens to be a composite key Pin
devvvy29-Mar-05 23:23
devvvy29-Mar-05 23:23 
GeneralPackage and deployment Pin
Salman Sheikh29-Mar-05 19:00
Salman Sheikh29-Mar-05 19:00 
GeneralLaunching SQL Server job xp_cmdshell Pin
devvvy29-Mar-05 17:32
devvvy29-Mar-05 17:32 
GeneralOracleClient and Timestamp datatype Pin
Yos Nugroho29-Mar-05 17:21
Yos Nugroho29-Mar-05 17:21 
GeneralGUIDs as PK's Pin
Marc Clifton29-Mar-05 10:04
mvaMarc Clifton29-Mar-05 10:04 
GeneralRe: GUIDs as PK's Pin
rwestgraham29-Mar-05 10:25
rwestgraham29-Mar-05 10:25 
GeneralRe: GUIDs as PK's Pin
Carl Mercier1-Apr-05 10:24
Carl Mercier1-Apr-05 10:24 
It works like a charm!

We have a database with about 100 tables, all of which have GUIDs as their primary key.

In order to improve performance, we use a special form of GUID we call "Smart GUID" internally. We found this information somewhere on the web [^]. Basically, the GUID is generated based on the date/time of the client machine. It helps sorting and indexing things because it groups rows created around the same time together. It improves indexing drastically.

Anyway, here's the code we came up with.

<br />
//  --------------------------------------------------------------<br />
//   SmartGUID for C#, by Carl Mercier<br />
//   June 16, 2003.<br />
// <br />
//   Inspired by Jimmy Nilsson's article entitled The Cost Of<br />
//   GUIDs as Primary Keys.<br />
//   http://www.informit.com/content/index.asp?product_id=%7BE3D24CE5%2DF2A0%2D4B16%2DA39C%2D7AB17525F07C%7D<br />
// <br />
//   This class is a wrapper of the .NET System.Guid structure.<br />
//   It returns a SmartGUID that speeds up indexing in MS SQL <br />
//   Server 2000, as discussed in Nilsson's article.<br />
// <br />
//   Part of this class was borrowed from glapointe's code<br />
//   posted on Informit's Discussion Board on Aug 16, 2002.<br />
// <br />
//   SmartGUIDs (a.k.a COMB) can also be generated in SQL 2000<br />
//   with the following code:<br />
//       DECLARE @aGuid UNIQUEIDENTIFIER<br />
//       SET @aGuid = CAST(CAST(NEWID() AS BINARY(10)) + <br />
//          CAST(GETDATE() AS BINARY(6)) AS UNIQUEIDENTIFIER)<br />
//  --------------------------------------------------------------<br />
<br />
using Microsoft.VisualBasic;<br />
using System;<br />
using System.Collections;<br />
using System.Data;<br />
using System.Diagnostics;<br />
namespace AdonisTech.Adonis2005.Core.Business {<br />
    [ Serializable() ]<br />
    public sealed class SmartGuid  { <br />
        private System.Guid mGuid; <br />
        <br />
        public SmartGuid() { <br />
            mGuid = NewComb(); <br />
        } <br />
        public SmartGuid( byte[] b ) { <br />
            mGuid = new Guid( b ); <br />
        } <br />
        public SmartGuid( UInt32 a, UInt16 b, UInt16 c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k ) { <br />
            mGuid = new Guid( a, b, c, d, e, f, g, h, i, j, k ); <br />
        } <br />
        public SmartGuid( string g ) { <br />
            mGuid = new Guid( g ); <br />
        } <br />
        public SmartGuid( int a, short b, short c, byte[] d ) { <br />
            mGuid = new Guid( a, b, c, d ); <br />
        } <br />
        public SmartGuid( int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k ) { <br />
            mGuid = new Guid( a, b, c, d, e, f, g, h, i, j, k ); <br />
        } <br />
        <br />
        public override int GetHashCode() { <br />
            return mGuid.GetHashCode(); <br />
        } <br />
        <br />
        <br />
        public override bool Equals( object o ) { <br />
            return mGuid.Equals( o ); <br />
        } <br />
        <br />
        public static bool Equals( object objA, object objB ) { <br />
            return System.Guid.Equals( objA, objB ); <br />
        } <br />
        <br />
        <br />
        public int CompareTo( object value ) { <br />
            return mGuid.CompareTo( value ); <br />
        } <br />
        <br />
        <br />
        public override string ToString() { <br />
            return mGuid.ToString(); <br />
        } <br />
        <br />
        public string ToString( string format ) { <br />
            return mGuid.ToString( format ); <br />
        } <br />
        <br />
        public string ToString( string format, System.IFormatProvider provider ) { <br />
            return mGuid.ToString( format, provider ); <br />
        } <br />
        <br />
        <br />
        public static System.Guid NewGuid() { <br />
            return NewComb(); <br />
        } <br />
        <br />
        private static Guid NewComb() { <br />
            byte[] guidArray = System.Guid.NewGuid().ToByteArray(); <br />
            DateTime baseDate = new DateTime( 1900, 1, 1 ); <br />
            DateTime now = DateTime.Now; <br />
            <br />
            //  Get the days and milliseconds which will be used to build the byte string <br />
            TimeSpan days = new TimeSpan( now.Ticks - baseDate.Ticks ); <br />
            TimeSpan msecs = new TimeSpan( now.Ticks - ( new DateTime( now.Year, now.Month, now.Day ).Ticks ) ); <br />
            <br />
            //  Convert to a byte array <br />
            //  Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333 <br />
            <br />
            byte[] daysArray = BitConverter.GetBytes( days.Days ); <br />
            byte[] msecsArray = BitConverter.GetBytes( System.Convert.ToInt64( msecs.TotalMilliseconds / 3.333333 ) ); <br />
            <br />
            //  Reverse the bytes to match SQL Servers ordering <br />
            Array.Reverse( daysArray ); <br />
            Array.Reverse( msecsArray ); <br />
            <br />
            //  Copy the bytes into the guid <br />
            Array.Copy( daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2 ); <br />
            Array.Copy( msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4 ); <br />
            <br />
            return new System.Guid( guidArray ); <br />
        } <br />
        <br />
        <br />
        public DateTime GetDateTime() { <br />
            return GetCombDateTime( mGuid ); <br />
        } <br />
        <br />
        public static DateTime GetDateTime( Guid o ) { <br />
            return GetCombDateTime( o ); <br />
        } <br />
        <br />
        <br />
        private static DateTime GetCombDateTime( Guid guid ) { <br />
            DateTime baseDate = new DateTime( 1900, 1, 1 ); <br />
            byte[] daysArray = new byte[ 5 ]; <br />
            byte[] msecsArray = new byte[ 5 ]; <br />
            byte[] guidArray = guid.ToByteArray(); <br />
            <br />
            //  Copy the date parts of the guid to the respective byte arrays. <br />
            Array.Copy( guidArray, guidArray.Length - 6, daysArray, 2, 2 ); <br />
            Array.Copy( guidArray, guidArray.Length - 4, msecsArray, 0, 4 ); <br />
            <br />
            //  Reverse the arrays to put them into the appropriate order <br />
            Array.Reverse( daysArray ); <br />
            Array.Reverse( msecsArray ); <br />
            <br />
            //  Convert the bytes to ints <br />
            int days = BitConverter.ToInt32( daysArray, 0 ); <br />
            int msecs = BitConverter.ToInt32( msecsArray, 0 ); <br />
            <br />
            DateTime myDate = baseDate.AddDays( days ); <br />
            myDate = myDate.AddMilliseconds( msecs * 3.333333 ); <br />
            <br />
            return myDate; <br />
        } <br />
        <br />
    } <br />
    <br />
} <br />

GeneralRe: GUIDs as PK's Pin
Marc Clifton4-Apr-05 5:16
mvaMarc Clifton4-Apr-05 5:16 
GeneralRe: GUIDs as PK's Pin
Andy Brummer29-Mar-05 11:14
sitebuilderAndy Brummer29-Mar-05 11:14 
GeneralRe: GUIDs as PK's Pin
turbochimp29-Mar-05 19:19
turbochimp29-Mar-05 19:19 
GeneralRe: GUIDs as PK's Pin
Scott Serl30-Mar-05 13:58
Scott Serl30-Mar-05 13:58 
GeneralRe: GUIDs as PK's Pin
Michael P Butler31-Mar-05 6:24
Michael P Butler31-Mar-05 6:24 
GeneralRe: GUIDs as PK's Pin
Michael Potter1-Apr-05 4:56
Michael Potter1-Apr-05 4:56 
GeneralRe: GUIDs as PK's Pin
Carl Mercier1-Apr-05 10:27
Carl Mercier1-Apr-05 10:27 
Questionhow to find server name of oracle Pin
satishrg29-Mar-05 8:55
satishrg29-Mar-05 8:55 
AnswerRe: how to find server name of oracle Pin
Colin Angus Mackay29-Mar-05 11:00
Colin Angus Mackay29-Mar-05 11:00 
AnswerRe: how to find server name of oracle Pin
Andy Brummer29-Mar-05 11:30
sitebuilderAndy Brummer29-Mar-05 11:30 
GeneralPermissions Pin
vuthaianh28-Mar-05 17:50
vuthaianh28-Mar-05 17:50 
GeneralRe: Permissions Pin
Edbert P28-Mar-05 20:11
Edbert P28-Mar-05 20:11 
QuestionWhat kind of Errors the SELECT statement can raise?? Pin
_J_28-Mar-05 10:51
_J_28-Mar-05 10:51 
AnswerRe: What kind of Errors the SELECT statement can raise?? Pin
Colin Angus Mackay29-Mar-05 0:57
Colin Angus Mackay29-Mar-05 0:57 
GeneralRe: What kind of Errors the SELECT statement can raise?? Pin
_J_29-Mar-05 1:09
_J_29-Mar-05 1:09 
GeneralRe: What kind of Errors the SELECT statement can raise?? Pin
Colin Angus Mackay29-Mar-05 1:32
Colin Angus Mackay29-Mar-05 1:32 
GeneralRe: What kind of Errors the SELECT statement can raise?? Pin
_J_29-Mar-05 1:44
_J_29-Mar-05 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.