Is there a practical way of migrating from identity columns to hilo keys?

前端 未结 5 1172
面向向阳花
面向向阳花 2021-02-01 08:11

I work with a database that depends heavily on identity columns. However as we have now moved all applications over to NHibernate I wanted to look into using HiLo as seems to be

5条回答
  •  无人及你
    2021-02-01 08:29

    Here is a sample from a recent migration from the increment generator to the MultipleHiLoPerTableGenerator (e.g. a single table is used to store high values for every Entities).

    My application is using Hibernate 3 + mapping files (.hbm.xml). My database is MySQL (innoDB + auto increment pk).

    Step 1 : replace your generator settings in your .hbm files. Replace :

    
    

    By

    
        hilo_values
        sequence_name
        sequence_next_hi_value
        1000
    
    

    Step 2 : create a new table to store the high values

    CREATE TABLE IF NOT EXISTS `hilo_values` (
      `sequence_name` varchar(255) NOT NULL,
      `sequence_next_hi_value` int(11) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    Step 3 : populate the initial high values according to existing data using the following piece of SQL. I assume here that the same max_lo value is used for every table.

    INSERT INTO hilo_values SELECT TABLE_NAME,  ((AUTO_INCREMENT DIV (1000 + 1)) + 1) FROM information_schema.tables WHERE table_schema = 'yourdbname'
    

提交回复
热议问题