Mysql table partition based on last digit of a column/id

后端 未结 2 1946
难免孤独
难免孤独 2021-01-22 03:37

I want to create partitions based on last digit of employee id, i.e

all ids ending with 0 go to first partition,

ending with 1 go to seco         


        
相关标签:
2条回答
  • 2021-01-22 04:21

    You will not be able to partition based on the last digit of the EmployeeID, as that would mean that you need to use substring function to retrieve the last digit first, which you can't do, due to limitations on the functions which can be used in partitioning clause.

    MySQL allows a definite list of functions, starting with version 5.1.12, which are listed here.

    0 讨论(0)
  • 2021-01-22 04:35

    I was wondering if there was a mod function allowed while creating a partition, I did it using

    CREATE TABLE ti (id INT, amount DECIMAL(7,2))
    ENGINE=INNODB
    PARTITION BY HASH( MOD(id,10) )
    PARTITIONS 10;
    

    this created 10 partitions each id going in its partition ending with the same number as the last digit of the id

    I added a few rows

        INSERT INTO ti VALUES (23123,343.22);
        INSERT INTO ti VALUES (23123,343.22);
        INSERT INTO ti VALUES (23144,343.22);
        INSERT INTO ti VALUES (23114,343.22);
        INSERT INTO ti VALUES (23124,343.22);
        INSERT INTO ti VALUES (23166,343.22);
        INSERT INTO ti VALUES (23116,343.22);
        INSERT INTO ti VALUES (23112,343.22);
        INSERT INTO ti VALUES (23199,343.22);
    

    then tested it

     SELECT
      partition_name part,
      partition_expression expr,
      partition_description descr,
      table_rows
    FROM
      INFORMATION_SCHEMA.partitions
    WHERE
      TABLE_SCHEMA = SCHEMA()
      AND TABLE_NAME='ti';
    

    OUTPUT:

    part    expr    descr   table_rows
    p0   MOD(id,10) \N  0
    p1   MOD(id,10) \N  0
    p2   MOD(id,10) \N  1
    p3   MOD(id,10) \N  2
    p4   MOD(id,10) \N  3
    p5   MOD(id,10) \N  0
    p6   MOD(id,10) \N  2
    p7   MOD(id,10) \N  0
    p8   MOD(id,10) \N  0
    p9   MOD(id,10) \N  1
    

    exactly what I wanted, thanks for pointing to the right link Incognito, but your answer was wrong, perhaps you misunderstood it

    0 讨论(0)
提交回复
热议问题