Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is MYSQL Partitioning?

I have read the documentation (http://dev.mysql.com/doc/refman/5.1/en/partitioning.html), but I would like, in your own words, what it is and why it is used.

  1. Is it mainly used for multiple servers so it doesn't drag down one server?
  2. So, part of the data will be on server1, and part of the data will be on server2. And server 3 will "point" to server1 or server2...is that how it works?
  3. Why does MYSQL documentation focus on partitioning within the same server...if the purpose is to spread it across servers?
like image 834
TIMEX Avatar asked Oct 16 '09 19:10

TIMEX


People also ask

What is the use of partition in MySQL?

MySQL partitioning is about altering – ideally, optimizing – the way the database engine physically stores data. It allows you to distribute portions of table data (a.k.a. partitions) across the file system based on a set of user-defined rules (a.k.a. the “partitioning function”).

Does MySQL have partition?

MySQL supports several types of partitioning as well as subpartitioning; see Section 22.2, “Partitioning Types”, and Section 22.2. 6, “Subpartitioning”. Section 22.3, “Partition Management”, covers methods of adding, removing, and altering partitions in existing partitioned tables.

What is key partitioning in MySQL?

Partitioning by key is similar to partitioning by hash, except that where hash partitioning employs a user-defined expression, the hashing function for key partitioning is supplied by the MySQL server.

When should you partition a table MySQL?

Partitions, generally, are only useful when you have a lot of data. If you only have a couple of million rows inside of your table, partitioning might not be a great idea, but you might benefit from partitioning if you deal with, say, more than 100 or 200 million rows.


1 Answers

The idea behind partitioning isn't to use multiple servers but to use multiple tables instead of one table. You can divide a table into many tables so that you can have old data in one sub table and new data in another table. Then the database can optimize queries where you ask for new data knowing that they are in the second table. What's more, you define how the data is partitioned.

Simple example from the MySQL Documentation:

CREATE TABLE employees (     id INT NOT NULL,     fname VARCHAR(30),     lname VARCHAR(30),     hired DATE NOT NULL DEFAULT '1970-01-01',     separated DATE NOT NULL DEFAULT '9999-12-31',     job_code INT,     store_id INT ) PARTITION BY RANGE ( YEAR(separated) ) (     PARTITION p0 VALUES LESS THAN (1991),     PARTITION p1 VALUES LESS THAN (1996),     PARTITION p2 VALUES LESS THAN (2001),     PARTITION p3 VALUES LESS THAN MAXVALUE ); 

This allows to speed up e.g.:

  1. Dropping old data by simple:

    ALTER TABLE employees DROP PARTITION p0; 
  2. Database can speed up a query like this:

    SELECT COUNT(*) FROM employees WHERE separated BETWEEN '2000-01-01' AND '2000-12-31' GROUP BY store_id; 

Knowing that all data is stored only on the p2 partition.

like image 105
Szymon Lipiński Avatar answered Oct 08 '22 14:10

Szymon Lipiński