Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to partition MySQL table by column that is not in the unique index

Let's have a simple table of products. Each produch has its unique ID and category. Users often search by category so I want to partition products by category. Each category in one partition e.g.

How do I do it? Because of course I have a primary key on my ID column and need my ID unique. Not unique in each category.

However partitiong has this limitation that "every unique key on the table must use every column in the table's partitioning expression".

Well, doesn't this make partitioning a bit useless? Or am I missing something? What should I do?

http://dev.mysql.com/doc/refman/5.1/en/partitioning-limitations-partitioning-keys-unique-keys.html

like image 868
Josef Sábl Avatar asked Aug 01 '12 15:08

Josef Sábl


People also ask

Can partition key have to be unique?

DynamoDB stores and retrieves each item based on the primary key value, which must be unique. Items are distributed across 10-GB storage units, called partitions (physical storage internal to DynamoDB). Each table has one or more partitions, as shown in the following illustration.

What is hash partitioning in MySQL?

HASH partitioning. With this type of partitioning, a partition is selected based on the value returned by a user-defined expression that operates on column values in rows to be inserted into the table. The function may consist of any expression valid in MySQL that yields an integer value.

Does partitioning improve performance MySQL?

There are a number of benefits that come with partitioning, but the two main advantages are: Increased performance - during scan operations, the MySQL optimizer knows what partitions contain the data that will satisfy a particular query and will access only those necessary partitions during query execution.

Is partitioning the same as indexing?

Indexes are used to speed the search of data within tables. Partitions provide segregation of the data at the hdfs level, creating sub-directories for each partition. Partitioning allows the number of files read and amount of data searched in a query to be limited.


1 Answers

The trick is to add the field category to your current primary key. (Your primary key will remain a primary key)
Then you can partition your table by category.

Here is the code you may use:

ALTER TABLE `products` DROP PRIMARY KEY , ADD PRIMARY KEY ( `id` , `category` );
ALTER TABLE `products` PARTITION BY KEY(category) PARTITIONS 6;

Add auto_increment option to the id if you want it to be really unique, and don't specify the id value when you insert data in the table. The id will be determined by the database server upon insertion.

Change field names and key names if necessary.

Documentation:
Partitioning types
KEY Partioning

like image 180
Jocelyn Avatar answered Sep 30 '22 21:09

Jocelyn