# Put some data into the same tablet in a specified manner

**URL:** <https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040>\
**Category:** General\
**Created:** [March 29, 2023, 4:10am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040 "2023-03-29T04:10:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [March 29, 2023, 4:10am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/1 "2023-03-29T04:10:18Z")

</div>

Hi  
Excuse me.  
If I use the hash sharding method and specify the id as the primary key，as shown in the following example:  
`CREATE TABLE tracking (id int PRIMARY KEY) `

What method can I use to place records with ids such as 10,11,12,13 in the same tablet ?  
Can I use split into? For example:  
`CREATE TABLE tracking (id int PRIMARY KEY) split into 1 tablet`

Thanks.

---

<div class="post-metadata">

**Author:** ![vaibhav\_yb](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/vaibhav_yb/32/361_2.png) [@vaibhav\_yb](https://forum.yugabyte.com/u/vaibhav_yb)\
**Post date:** [March 29, 2023, 4:31am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/2 "2023-03-29T04:31:00Z")

</div>

Hi @ZhenNan2016 You can split tablets using range sharding to place records into the same tablet.

For example:

```sql
CREATE TABLE tracking (id INT, PRIMARY KEY (id asc)) SPLIT AT ((100));

```

The above will cause the table to be split at the given value and will let you insert all your specified values in the same tablet.

---

<div class="post-metadata">

**Author:** ![vaibhav\_yb](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/vaibhav_yb/32/361_2.png) [@vaibhav\_yb](https://forum.yugabyte.com/u/vaibhav_yb)\
**Post date:** [March 29, 2023, 4:31am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/3 "2023-03-29T04:31:37Z")

</div>

Reference:

> **[CREATE TABLE \[YSQL\]](https://docs.yugabyte.com/preview/api/ysql/the-sql-language/statements/ddl_create_table/#split-at-values)**
>
> Use the CREATE TABLE statement to create a table in a database.

---

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [March 29, 2023, 5:14am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/4 "2023-03-29T05:14:53Z")

</div>

> [@ZhenNan2016](#):
>
> hash sharding

Hi @vaibhav_yb

Yes, it’s a good idea.  
However, I hope that the processing method of hash sharding also supports placing specified data in the same table tablet.  
Do you have any good suggestions?  
Thanks.

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [March 29, 2023, 5:46am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/5 "2023-03-29T05:46:45Z")

</div>

@ZhenNan2016

> [@ZhenNan2016](#):
>
> Do you have any good suggestions?

The only way to guarantee is by having a table like below and making sure they have the same `partition_key` column value:

```sql
CREATE TABLE sample(partition_key int, id int, PRIMARY KEY (partition_key, id));

```

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [March 29, 2023, 6:07am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/6 "2023-03-29T06:07:57Z")

</div>

@ZhenNan2016

Why is the requirement for both rows being in the same tablet?

---

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [March 29, 2023, 6:12am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/7 "2023-03-29T06:12:43Z")

</div>

Hi @dorian_yugabyte  
Excuse me.  
I can’t follow you, sorry.  
Are k1 and k2 two column fields?  
Is the partition\_key a combination of k1 and k2 ?  
Is this id field a normal column?  
Is this the correct table creation statement as follow ?  
`CREATE TABLE（k1 int，k2 int，id int，PRIMARY KEY(k1，k2));`

---

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [March 29, 2023, 6:14am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/8 "2023-03-29T06:14:54Z")

</div>

@dorian_yugabyte  
Because it is necessary to test some operations on two records in the same tablet, including deadlock testing

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [March 29, 2023, 6:24am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/9 "2023-03-29T06:24:57Z")

</div>

@ZhenNan2016

> [@ZhenNan2016](#):
>
> I can’t follow you, sorry.

I edited the query above, please check again.

Only `partition_key` column is used for partitioning.

---

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [March 29, 2023, 6:38am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/10 "2023-03-29T06:38:35Z")

</div>

> [@dorian\_yugabyte](#):
>
> I edited the query above, please check again.
> 
> Only `partition_key` column is used for partitioning.

I got it now.  
Thanks.

---

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [March 30, 2023, 8:26am UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/11 "2023-03-30T08:26:49Z")

</div>

> [@dorian\_yugabyte](#):
>
> `CREATE TABLE sample(partition_key int, id int, PRIMARY KEY (partition_key, id));`

Hi @dorian_yugabyte  
I’m sorry to trouble you.  
The data is written to the data table according to the algorithm corresponding to hash-sharing.  
What tools do we have to quickly calculate the hash value of the partition\_key?  
I want to see how a data record calculates the hash value of the partition\_key and writes it to which tablet.  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [March 30, 2023, 12:20pm UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/12 "2023-03-30T12:20:12Z")

</div>

> [@ZhenNan2016](#):
>
> The data is written to the data table according to the algorithm corresponding to hash-sharing.  
> What tools do we have to quickly calculate the hash value of the partition\_key?  
> I want to see how a data record calculates the hash value of the partition\_key and writes it to which tablet.

See `yb_hash_code` [yb\_hash\_code() function [YSQL] | YugabyteDB Docs](https://docs.yugabyte.com/preview/api/ysql/exprs/func_yb_hash_code/)

---

<div class="post-metadata">

**Author:** ![dorian\_yugabyte](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/dorian_yugabyte/32/206_2.png) [@dorian\_yugabyte](https://forum.yugabyte.com/u/dorian_yugabyte)\
**Post date:** [March 30, 2023, 12:20pm UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/13 "2023-03-30T12:20:56Z")

</div>

You can also use table partitioning that is inherited from PostgreSQL: [Table partitioning | YugabyteDB Docs](https://docs.yugabyte.com/preview/explore/ysql-language-features/advanced-features/partitions/)

---

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [March 30, 2023, 12:46pm UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/14 "2023-03-30T12:46:54Z")

</div>

OK，I got it now.  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![johnsmi4ri](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@johnsmi4ri](https://forum.yugabyte.com/u/johnsmi4ri)\
**Post date:** [November 28, 2023, 12:06pm UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/15 "2023-11-28T12:06:05Z")

</div>

Hi there!

You can achieve this by customizing the hash function used for sharding. By default, hash functions distribute data evenly across all shards, so assigning multiple records to the same shard would require modifying the hash function’s behavior.

One approach would be to create a custom hash function that takes the `id` value and maps it to a specific shard, regardless of the numerical value of `id` . For instance, you could create a function that always returns 0 for any `id` value. This would ensure that all records with any `id` value are placed in the same shard.

Here’s an example of how you might implement this in SQL:

```auto
SQL
CREATE FUNCTION custom_hash(id INT) RETURNS INT
BEGIN
  RETURN 0; -- Always return 0 to place all records in the same shard
END;

CREATE TABLE tracking (
  id INT PRIMARY KEY,
  -- Other columns...
)
PARTITION BY HASH(custom_hash(id));

```

This code defines a custom hash function `custom_hash` that always returns 0. It then creates a table `tracking` with a primary key `id` and partitions the table using the custom hash function. This ensures that all records with any `id` value are placed in the same shard.

Alternatively, you could use the `split into` clause to create a single-shard table, which would effectively place all records in the same shard. However, this approach might not be suitable for long-term scalability, as it would limit the ability to distribute data across multiple shards as the data volume grows.

In summary, the most appropriate method for placing records with specific IDs in the same shard depends on your specific requirements and considerations. If you need to ensure that all records with any `id` value are always placed in the same shard, regardless of the numerical value of `id` , then customizing the hash function is the most suitable approach. If you anticipate needing to scale the data across multiple shards in the future, then using the `split into` clause might not be the best choice.

---

<div class="post-metadata">

**Author:** ![ZhenNan2016](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.yugabyte.com/zhennan2016/32/623_2.png) [@ZhenNan2016](https://forum.yugabyte.com/u/ZhenNan2016)\
**Post date:** [November 28, 2023, 12:18pm UTC](https://forum.yugabyte.com/t/put-some-data-into-the-same-tablet-in-a-specified-manner/2040/17 "2023-11-28T12:18:48Z")

</div>

Hi @johnsmi4ri  
I got it now.  
Thank you very much.
