Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:37:38+00:00 2026-06-07T10:37:38+00:00

I am in the progress of migrating a database from Microsoft SQL Server to

  • 0

I am in the progress of migrating a database from Microsoft SQL Server to MySQL/MariaDB. On MSSQL, the database uses the uniqueidentifier (GUID) datatype for all primary keys. NHibernate is used to map data between database and application, and the guid.comb strategy is employed for GUID generation to avoid fragmentation of the clustered indexes.

MySQL not having a dedicated GUID data type, the new database schema uses BINARY(16) for all identifiers. Without making any changes to the NHibernate mappings, I can start our application, persist new entities and load them back up from the MySQL database. Great! However, it turns out that the sequantially generated GUIDs are ordered very much non-sequantially in the BINARY(16) column, yielding unacceptable index fragmentation.

Reading up on the issue, it turns out that MSSQL has a quite special method for sorting GUIDs. The 16 bytes are ordered first by the last six bytes, then by the precending in reversed groups, whereas my naïve MySQL implementation sorts on the first byte first, then the next and so forward.

And this leads to my question: How to avoid this fragmentation in the MySQL database, while keeping the existing GUIDs and the guid.comb strategy? I have an idea for a solution myself (posted below) but I cannot help feeling I might have missed something. Surely, others must have dealt with this issue before, and maybe there is a simple way to get around it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:37:41+00:00Added an answer on June 7, 2026 at 10:37 am

    As observed by Alberto Ferrari and discussed here on StackOverflow, Microsoft SQL Server sorts GUIDs by comparing the bytes in a specific order. As MySQL will sort a BINARY(16) “straight-foward”, all we need to do, is to reorder the bytes when reading/writing to the database.

    NHibernate allows us to define custom data types, which can be used in mappings between database and objects. I have implemented a BinaryGuidType, capable of reordering the bytes produced by Guid.ToByteArray() according to the way MSSQL sorts GUIDs and reordering them back into the format accepted by the Guid(byte[]) constructor.

    The byte order looks like this:

    int[] ByteOrder = new[] { 10,11,12,13,14,15,8,9,6,7,4,5,0,1,2,3 };
    

    Saving a System.Guid to a BINARY(16) goes like this:

    var bytes = ((Guid) value).ToByteArray();
    var reorderedBytes = new byte[16];
    
    for (var i = 0; i < 16; i++)
    {
        reorderedBytes[i] = bytes[ByteOrder[i]];
    }
    
    NHibernateUtil.Binary.NullSafeSet(cmd, reorderedBytes, index);
    

    Reading the bytes back into a System.Guid goes like this:

    var bytes = (byte[]) NHibernateUtil.Binary.NullSafeGet(rs, names[0]);
    if (bytes == null || bytes.Length == 0) return null;
    
    var reorderedBytes = new byte[16];
    
    for (var i = 0 ; i < 16; i++)
    {
        reorderedBytes[ByteOrder[i]] = bytes[i];
    }
    

    Full source code for the BinaryGuidType here.

    This seems to work well. Creating and persisting 10.000 new objects in a table, they are stored completely sequentially, with no signs of index fragmentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in progress of migrating an app from CakePHP 1.3 to 2.0, and
I have a progress bar which inherits from JSlider to provide highlighting functionality. Highlights
We are in a migration process from a Progress DB to use the Dataserver
progress dialog doesnt work while listing data with listAdapter i think its all about
My new app in progress uses an instance of CLLocationManager for several purposes. To
We are in the progress of migrating Data Access Layers to a newer, more
Currently, I am in the progress of migrating CVS to Mercurial. http://jstock.hg.sourceforge.net/hgweb/jstock/ I would
I need a progress wheel to show while a lot of database and 3rd
In the name of progress (and learning) how can I rid tables from my
CURRENT PROGRESS TOWARD SOLUTION: This is the updated code: sql = INSERT INTO Strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.