databasebigdatadata-warehousecreate-tableapache-doris

Why must key columns of a Doris table be placed before other columns?


The create table statement in Apache Doris is confusing to me. I want to create a table, but I must put the key columns before other columns, otherwise I will get an error.

I will follow this rule, but why must the key columns of a Doris table be placed before other columns?

My steps to reproduce are below:

  1. create table statement (wrong statement):
CREATE  TABLE IF NOT EXISTS table_wrong
(name varchar(20), id int)ENGINE = olap
duplicate key(id)
DISTRIBUTED BY HASH(`id`) BUCKETS 2
PROPERTIES ("replication_num" = "1");
  1. Then I get an error like:
ERROR 1105 (HY000): errCode = 2, detailMessage = Key columns should be a ordered prefix of the schema. 
KeyColumns[0] (starts from zero) is id, but corresponding column is name in the previous columns declaration.
  1. If I use the statement below instead, then there is no error:
CREATE  TABLE IF NOT EXISTS table_right
(id int, name varchar(20))ENGINE = olap
duplicate key(id)
DISTRIBUTED BY HASH(`id`) BUCKETS 2
PROPERTIES ("replication_num" = "1");

So, what is the reason for such a requirement?

I have checked the docs for Apache Doris, but I can't find an answer. I will follow the rule of Doris and I wanna know why.


Solution

  • as follows:

    1. Convenient sorting and search

    2. Improve readability and clearly see the unique identification of each row of data

    3. Improve query performance, such as prefix indexing and other scenarios:
    https://doris.apache.org/docs/3.0/table-design/index/prefix-index