Pregunta

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 second partition and so on..

This way I want to create 10 partitions, employee id can be int or varchar which ever is easy and fast in making partition.

Currently we have 10 separate tables employee_01, employee_02... (dont like this)

Although the retrieval and insertion time will be enhanced by no means, but maintainability would be increased by huge factors, thanks

Edit:

How do I specify partitioning criteria based on last digit of employee

¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top