什么是 MYSQL 分区?

发布于 2024-08-07 22:52:42 字数 367 浏览 4 评论 0原文

我已阅读文档(http://dev.mysql.com/doc /refman/5.1/en/partitioning.html),但我想用您自己的话说,它是什么以及为什么使用它。

  1. 它主要用于多台服务器,这样就不会拖累一台服务器吗?
  2. 因此,部分数据将在 server1 上,部分数据将在 server2 上。服务器 3 将“指向”服务器 1 或服务器 2...它是如何工作的?
  3. 为什么 MYSQL 文档重点关注同一服务器内的分区...如果目的是将其分布在服务器之间?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最丧也最甜 2024-08-14 22:52:42

分区背后的想法不是使用多个服务器,而是使用多个表而不是一张表。您可以将一张表分成许多表,这样您就可以在一个子表中保存旧数据,在另一张表中保存新数据。然后,数据库可以优化您请求新数据的查询,因为知道它们位于第二个表中。此外,您还可以定义数据的分区方式。

来自 MySQL 文档 的简单示例:

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
);

这可以加快速度,例如:

  1. 通过简单的方式删除旧数据:

    ALTER TABLE 员工 DROP PARTITION p0;
    
  2. 数据库可以像这样加速查询:

    <前><代码>选择计数(*)
    来自员工
    '2000-01-01' 和 '2000-12-31' 之间的分隔位置
    按 store_id 分组;

知道所有数据仅存储在 p2 分区上。

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.

£烟消云散 2024-08-14 22:52:42

分区表是由多个物理子表组成的单个逻辑表。
分区代码实际上只是一组 Handler 对象的包装器
代表底层分区,它将请求转发到存储引擎
通过 Handler 对象。分区是一种隐藏底层的黑匣子
尽管您可以很容易地通过以下方式看到它们,但您在 SQL 层进行了分区
查看文件系统,您将在其中看到带有散列分隔的组件表
命名约定。

例如,
这是将每年的销售额放入单独分区的简单方法:

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 );

阅读更多内容 此处< /a>.

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.

看透却不说透 2024-08-14 22:52:42

它实际上并不是关于使用不同的服务器实例(尽管有时这是可能的),它更多的是关于将表划分在不同的物理分区中。

它将您的表和索引分成更小的部分,甚至将其细分为更小的部分。
可以将其视为在一个大仓库(一张大桌子)中拥有数百万本不同主题和不同年份(例如 2000-2019 年)的不同杂志。分区意味着你要把它们组织在那个大仓库内的不同房间里。它们仍然属于同一个仓库,但现在您可以根据数据库分区策略将它们在逻辑级别上分组。

索引实际上就像在您的仓库或仓库内的房间中保留一本表格来记录哪一本杂志。正如您所看到的,数据库分区和索引之间存在很大差异,并且它们可以很好地结合使用。

您可以在我的网站上阅读有关 数据库分区

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文