我尝试对表进行分区时出错

时间:2017-09-08 08:13:58

标签: mysql partitioning

这是我的posts表:

CREATE TABLE `posts` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `img` varchar(255) COLLATE utf8_croatian_ci NOT NULL,
 `vid` varchar(255) COLLATE utf8_croatian_ci NOT NULL,
 `title` varchar(255) COLLATE utf8_croatian_ci NOT NULL,
 `subtitle` varchar(255) COLLATE utf8_croatian_ci NOT NULL,
 `auth` varchar(54) COLLATE utf8_croatian_ci NOT NULL,
 `story` longtext COLLATE utf8_croatian_ci NOT NULL,
 `tags` varchar(255) COLLATE utf8_croatian_ci NOT NULL,
 `status` varchar(100) COLLATE utf8_croatian_ci NOT NULL,
 `moder` varchar(50) COLLATE utf8_croatian_ci NOT NULL,
 `rec` varchar(50) COLLATE utf8_croatian_ci NOT NULL,
 `pos` varchar(50) COLLATE utf8_croatian_ci NOT NULL,
 `inde` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=117 DEFAULT CHARSET=utf8 COLLATE=utf8_croatian_ci

我想制作两个分区以提高查询性能 第一个分区应包含所有non-archive行 第二个分区 - 所有archive行。

ALTER TABLE posts
PARTITION BY LIST COLUMNS (status)
(
PARTITION P1 VALUES IN ('admin', 'moder', 'public', 'rec'),
PARTITION P2 VALUES IN ('archive')
);

phpmyadmin错误:

Static analysis:

    1 errors were found during analysis.

    Unrecognized alter operation. (near "" at position 0)
    MySQL said: 

    #1503 - A PRIMARY KEY must include all columns in the table's partitioning function  

任何帮助?

1 个答案:

答案 0 :(得分:1)

您想加快哪些疑问?由于您目前拥有唯一的索引,WHERE id=...WHERE id BETWEEN ... AND ...是唯一快速的查询。 您建议的分区对其他查询无效

你似乎只有几十行;不要考虑分区,除非你期望至少有一百万行。

status只有5个值吗?然后将其ENUM('archive', 'admin', 'moder', 'public', 'rec') NOT NULL。这需要1个字节而不是很多。

如果您要查询date和/或status和/或auth,那么让我们谈谈索引,尤其是“复合”索引。并且,为了实现您想象的“存档”拆分,将status作为索引中的第一列。