{"id":5675,"date":"2021-04-27T13:34:25","date_gmt":"2021-04-27T05:34:25","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=5675"},"modified":"2021-05-18T10:44:49","modified_gmt":"2021-05-18T02:44:49","slug":"clickhouse%e5%bb%ba%e8%a1%a8create-table%e6%8a%a5%e9%94%99dbexception-primary-key-must-be-a-prefix-of-the-sorting-key-but-in-position","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/5675","title":{"rendered":"clickhouse\u5efa\u8868create table\u62a5\u9519DB::Exception: Primary key must be a prefix of the sorting key, but in position"},"content":{"rendered":"<p>\u5728Clickhouse\u7684MergeTree\u5f15\u64ce\u5bb6\u65cf\uff0c\u6392\u5e8f\u5217\u5fc5\u987b\u4ee5\u4e3b\u952e\u5217\u5f00\u5934\uff0c\u5426\u5219\u62a5\u9519DB::Exception: Primary key must be a prefix of the sorting key, but in position 0 its column is seqid, not collectdate.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Directory Navigation<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2b1630eb744\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2b1630eb744\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.gbase8.cn\/en\/5675\/#%E6%8A%A5%E9%94%99%E6%A0%B7%E4%BE%8B\" >\u62a5\u9519\u6837\u4f8b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.gbase8.cn\/en\/5675\/#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88\" >Solution<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%8A%A5%E9%94%99%E6%A0%B7%E4%BE%8B\"><\/span>\u62a5\u9519\u6837\u4f8b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>localhost :) create table t2(seqid int, name varchar(100), collectdate date,value decimal(16,3))engine=MergeTree() primary key seqid order by collectdate partition by toYYYYMMDD(collectdate);\r\n\r\nCREATE TABLE t2\r\n(\r\n    `seqid` int,\r\n    `name` varchar(100),\r\n    `collectdate` date,\r\n    `value` decimal(16, 3)\r\n)\r\nENGINE = MergeTree\r\nPARTITION BY toYYYYMMDD(collectdate)\r\nPRIMARY KEY seqid\r\nORDER BY collectdate\r\n\r\nQuery id: 2aa434f1-531c-449b-a467-6e17a0580295\r\n\r\n\r\n0 rows in set. Elapsed: 0.017 sec.\r\n\r\nReceived exception from server (version 21.4.5):\r\nCode: 36. DB::Exception: Received from localhost:9000. DB::Exception: Primary key must be a prefix of the sorting key, but in position 0 its column is seqid, not collectdate.\r\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88\"><\/span>Solution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5c06\u4e3b\u952e\u5217\u653e\u5230\u6392\u5e8f\u5217\u524d\u9762<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>localhost :) create table t2(seqid int, name varchar(100), collectdate date,value decimal(16,3))engine=MergeTree() primary key seqid partition by toYYYYMMDD(collectdate) order by (seqid,collectdate);\r\n\r\nCREATE TABLE t2\r\n(\r\n    `seqid` int,\r\n    `name` varchar(100),\r\n    `collectdate` date,\r\n    `value` decimal(16, 3)\r\n)\r\nENGINE = MergeTree\r\nPARTITION BY toYYYYMMDD(collectdate)\r\nPRIMARY KEY seqid\r\nORDER BY (seqid, collectdate)\r\n\r\nQuery id: 53da06de-5486-4fd3-b28f-33ab2f1420a3\r\n\r\nOk.\r\n\r\n0 rows in set. Elapsed: 0.044 sec.\r\n\r\nlocalhost :) show create table t2;\r\n\r\nSHOW CREATE TABLE t2\r\n\r\nQuery id: ed164b0c-1bda-42e9-b86b-7bde9f44a932\r\n\r\n\u250c\u2500statement\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502 CREATE TABLE testdb.t2\r\n(\r\n    `seqid` Int32,\r\n    `name` String,\r\n    `collectdate` Date,\r\n    `value` Decimal(16, 3)\r\n)\r\nENGINE = MergeTree\r\nPARTITION BY toYYYYMMDD(collectdate)\r\nPRIMARY KEY seqid\r\nORDER BY (seqid, collectdate)\r\nSETTINGS index_granularity = 8192 \u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\n1 rows in set. Elapsed: 0.004 sec.\r\n\r\nlocalhost :)\r\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u5728Clickhouse\u7684MergeTree\u5f15\u64ce\u5bb6\u65cf\uff0c\u6392\u5e8f\u5217\u5fc5\u987b\u4ee5\u4e3b\u952e\u5217\u5f00\u5934\uff0c\u5426\u5219\u62a5\u9519DB::Exception: Primary key must be a prefix of the sorting key, but in position 0 its column is seqid, not collectdate.<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-5675","post","type-post","status-publish","format-standard","hentry","category-clickhouse"],"_links":{"self":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/5675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/comments?post=5675"}],"version-history":[{"count":1,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/5675\/revisions"}],"predecessor-version":[{"id":5676,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/5675\/revisions\/5676"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=5675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=5675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=5675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}