Uploaded image for project: 'Ignite'
  1. Ignite
  2. IGNITE-16591

DDL syntax COLOCATE BY

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • None
    • sql

    Description

      Add COLOCATED BY for CREATE TABLE syntaxis:

      Colocation by non-composite primary key

      As a typical example, let’s assume we have the following schema:

      CREATE TABLE Accounts (
        account_id INT,
        name       VARCHAR,
        balance    DOUBLE,
        PRIMARY KEY (account_id)
      )
      
      CREATE TABLE Transactions (
        tx_id      INT,
        account_id INT,
        amount     DOUBLE,
        PRIMARY KEY (tx_id)
      )
      

      Most of the operations on such a schema will be isolated within a single account. Examples:
      1. Add a new transaction, and update the balance of the corresponding account.
      2. Run through all the transactions for an account, and calculate the average amount.

      To achieve better performance, we want each of these operations to execute on a single node. Therefore, we need to group all transactions belonging to the same account within the same Raft group (partition, range, etc). Corresponding records that represent the account itself should also reside in the same group.

      To achieve this, we want to perform affinity mapping for the Transactions table using the account_id field (as opposed to the tx_id, as it would happen currently). We need to allow users to specify the field used for colocation. For example, like this:

      CREATE TABLE Transactions (
        tx_id      INT,
        account_id INT,
        amount     DOUBLE,
        PRIMARY KEY (tx_id, account_id)
      )
      COLOCATE BY (account_id)
      

      NOTE: the affinity key must always be a part of the primary key. Otherwise, an exception is thrown.
      NOTE: Primary key columns, and therefore affinity key columns, MUST NOT permit NULL values according to the SQL standard.

      Colocation by composite primary key

      In case of using the composite primary key the tables also could be colocated. For example:

      CREATE TABLE Orders (
        order_id     INT,
        department_id INT,
        create_date   TIMESTAMP,
        PRIMARY KEY (order_id, department_id)
      )
      
      CREATE TABLE OrderItems (
        order_item_id      INT,
        order_id          INT,
        department_id      INT,
        amount              INT,
        PRIMARY KEY (order_item_id)
      )
      

      For such data model the OrderItems table could be colocated with the Orders table in the following way:

      CREATE TABLE OrderItems (
        order_item_id      INT,
        order_id          INT,
        department_id      INT,
        amount              INT,
        PRIMARY KEY (order_item_id, order_id, department_id)
      )
      COLOCATE BY (order_id, department_id)
      

      Note: COLOCATE BY clause of colocated table (OrderItems table in the example above) must contain the same set of columns and in the same order as PRIMARY KEY clause of the main table (Orders table in the example above) in order to properly colocate the data.

      Attachments

        Issue Links

          Activity

            People

              tledkov-gridgain Taras Ledkov
              jooger Iurii Gerzhedovich
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h 50m
                  1h 50m