|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | += Data Colocation |
| 16 | + |
| 17 | +In many cases you may want to store related data on the same node. This way multi-entry queries do not need to pull data from other nodes and are thus executed faster. |
| 18 | + |
| 19 | +When the table is created, you can choose the key that will be used to colocate data. |
| 20 | + |
| 21 | +For example, if you have `Person` and `Company` objects, and each person has the companyId field that indicates the company the person works for. By specifying the `Person.companyId` and `Company.ID` as colocation keys, you ensure that all the persons working for the same company are stored on the same node, where the company object is stored as well. Queries that request persons working for a specific company are processed on a single node. |
| 22 | + |
| 23 | +== Configuring Colocation Key |
| 24 | + |
| 25 | +Data colocation is configured during table creation by using the `COLOCATE BY` clause. The colummns used to colocate data must be in the primary key and must be specified in the same order as the `PRIMARY KEY` of the main table. |
| 26 | + |
| 27 | +For example, the table below will colocate data for people based on the `city_id` column: |
| 28 | + |
| 29 | +---- |
| 30 | +CREATE TABLE IF NOT EXISTS Person ( |
| 31 | + id int, |
| 32 | + city_id int primary key, |
| 33 | + name varchar, |
| 34 | + age int, |
| 35 | + company varchar |
| 36 | +) COLOCATE BY (city_id) |
| 37 | +---- |
| 38 | + |
| 39 | +When using composite primary keys, you can specify multiple columns to colocate data by: |
| 40 | + |
| 41 | +---- |
| 42 | +CREATE TABLE Company ( |
| 43 | + company_id int, |
| 44 | + department_id int, |
| 45 | + city_id int, |
| 46 | + company_name timestamp, |
| 47 | + PRIMARY KEY (company_id, city_id) |
| 48 | +) |
| 49 | +
|
| 50 | +CREATE TABLE IF NOT EXISTS Person ( |
| 51 | + id int, |
| 52 | + city_id int, |
| 53 | + name varchar, |
| 54 | + age int, |
| 55 | + company_id int, |
| 56 | + PRIMARY KEY (id, company_id, city_id) |
| 57 | +) |
| 58 | +COLOCATE BY (company_id, city_id) |
| 59 | +---- |
| 60 | + |
| 61 | +In this case, Ignite will try to colocate these tables together for storage. |
| 62 | + |
| 63 | +NOTE: The `COLOCATE BY` clause of colocated table (`Person` table in the example above) must contain the same set of columns and in the same order as the `PRIMARY KEY` clause of the main table (Company table in the example above) to colocate the data. |
0 commit comments