What is MYSQL Partitioning?

后端 未结 3 744
野的像风
野的像风 2021-01-29 18:55

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
相关标签:
3条回答
  • 2021-01-29 19:43

    It is not really about using different server instances (although that is sometimes a possibility), it is more about dividing your tables in different physical partitions.

    It's dividing your tables and indexes into smaller pieces, and even subdivide it into even smaller pieces. Think of it as having several million different magazines of different topics and different years (say 2000-2019) all in one big warehouse (one big table). Partitioning would mean that you would put them organized in different rooms inside that big warehouse. They still belong together inside the one warehouse, but now you group them on a logical level, depending on your database partitioning strategy.

    Indexing is actually like keeping a table of which magazine is where in your warehouse, or in your rooms inside your warehouse. As you can see, there is a big difference between database partitioning and indexing, and they can be very well used together.

    You can read more about it on my website on this article about Database Partitioning

    0 讨论(0)
  • 2021-01-29 19:44

    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.

    0 讨论(0)
  • 2021-01-29 19:47

    A partitioned table is a single logical table that’s composed of multiple physical subtables. The partitioning code is really just a wrapper around a set of Handler objects that represent the underlying partitions, and it forwards requests to the storage engine through the Handler objects. Partitioning is a kind of black box that hides the underlying partitions from you at the SQL layer, although you can see them quite easily by looking at the filesystem, where you’ll see the component tables with a hash-delimited naming convention.

    For example, here’s a simple way to place each year’s worth of sales into a separate partition:

    CREATE TABLE sales (
     order_date DATETIME NOT NULL,
     -- Other columns omitted
    ) ENGINE=InnoDB PARTITION BY RANGE(YEAR(order_date)) (
     PARTITION p_2010 VALUES LESS THAN (2010),
     PARTITION p_2011 VALUES LESS THAN (2011),
     PARTITION p_2012 VALUES LESS THAN (2012),
     PARTITION p_catchall VALUES LESS THAN MAXVALUE );
    

    read more here.

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