Start Now

Download our new eBook, Distributed SQL Databases for Dummies, to discover more about distributed SQL and YugabyteDB!

Download for Free

Distributed SQL Tips and Tricks – Jan 31, 2020

Jimmy Guerrero

VP Developer Relations

Welcome to this week’s tips and tricks blog where we recap some distributed SQL questions from around the Internet. We’ll also review upcoming events, new documentation, and blogs that have been published since the last post. Got questions? Make sure to ask them on our YugabyteDB Slack channel, Forum, GitHub, or Stackoverflow. Ok, let’s dive right in:

Can I specify the number of tablets I want a table to have?

Yes, this can be done by using the SPLIT INTO clause in the CREATE TABLE statement. This clause specifies the number of tablets that will be created for a given table.

For example:


Wait. What’s a tablet?

A tablet is just another name for a shard. In a YugabyteDB cluster, tables are split into tablets so that the data can be distributed onto various nodes. This “splitting of tables” increases the availability and accessibility of the data.

The SPLIT INTO capability is useful if you know ahead of time how big or small your tables are. For example, most SQL databases end up having a large number of tables, of which most are fairly static and small in size. We can then use SPLIT INTO clause to create a small number of tablets (or even one) for such tables. SPLIT INTO is also useful in 2 Data Center (2DC) deployments since the bootstrapping of these setups require that both clusters have the exact same number of tablets.

Visit the Docs for more info about tablets and how data distribution works.

What data partitioning schemes does YugabyteDB support?

YugabyteDB supports both hash (default) and range partitioning. In YugabyteDB, there are two types of primary key columns:

Hash primary key columns: These columns have 1 or more leading hash partitioned columns. When a table is created in YugabyteDB, the first column is treated as the hash partition column by default. This behavior can, however, be modified by explicit use of the HASH annotation.

For example:

CREATE TABLE orders(customer_id INT, product_id INT, date TIMESTAMP, PRIMARY KEY((customer_id, product_id) HASH));

Range primary key columns: A table can have zero or more range primary key columns and it controls the top-level ordering of rows within a table (if there are no hash partition columns) or the ordering of rows among rows that share a common set of hash partitioned column values. By default, the range primary key columns are stored in ascending order. But this behavior can be controlled by explicit use of ASC or DESC.

In working with application developers and DBAs, we’ve determined that the most popular YugabyteDB use cases requiring scalability often do not need to perform range lookups on the primary key. This is why we picked consistent hash sharding as the default. User identity (user ids do not need ordering), product catalog (product ids are not related to one another) and stock ticker data (one stock symbol is independent of all other stock symbol) are some common examples.

In use cases where range lookups are desired, YugabyteDB allows specifying range sharding. In cases when range queries become important after the data is already loaded into a hash sharded table, a range sharded secondary index can be created on that column. Once the secondary index is rebuilt, range queries would become efficient.

For a more detailed discussion on partitioning schemes, check out, “Four Data Sharding Strategies We Analyzed in Building a Distributed SQL Database.”

How can I release all the internal resources associated with a database session?

The DISCARD ALL command will release all the internal resources associated with a YSQL database session. This would include things like cached query plans, cached sequence-related states and the dropping of temp of tables created during the current session.

What row-level locking options does YugabyteDB support in a SELECT statement?

Row-level locks do not affect data querying if you stick with YugabyteDB’s default snapshot isolation mode; they block only writers and lockers to the same row. YugabyteDB supports the following row-level locking on SELECT statements:

  • FOR UPDATE & FOR NO KEY UPDATE both cause the rows retrieved by the SELECT statement to be locked as though for an update. This prevents them from being locked, modified or deleted by other transactions until the current transaction ends. We are in the process of refining the semantics of FOR NO KEY UPDATE. This will behave similar to FOR UPDATE, except that the lock acquired will be weaker: this lock will not block SELECT FOR KEY SHARE commands that attempt to acquire a lock on the same rows.
  • FOR SHARE & FOR KEY SHARE both behave similarly to FOR NO KEY UPDATE, except that they acquire a shared lock rather than exclusive lock on each retrieved row. A shared lock blocks other transactions from performing UPDATE, DELETE, SELECT FOR UPDATE or SELECT FOR NO KEY UPDATE on these rows, but it does not prevent them from performing SELECT FOR SHARE or SELECT FOR KEY SHARE. We are also in the process of refining the semantics of FOR KEY SHARE. This will behave similar to FOR SHARE, except that the lock will be weaker: SELECT FOR UPDATE will be blocked, but not SELECT FOR NO KEY UPDATE. A key-shared lock will block other transactions from performing DELETE or any UPDATE that changes the key values, but not other UPDATE, and neither will it prevent SELECT FOR NO KEY UPDATE, SELECT FOR SHARE, or SELECT FOR KEY SHARE. You can track the progress on the enhancements to FOR NO KEY UPDATE and FOR KEY SHARE with this GitHub issue.

New Documentation, Blogs, Tutorials, and Videos

New Blogs

New Videos

New and Updated Docs

Upcoming Meetups and Conferences

PostgreSQL Meetups

Distributed SQL Webinars


We Are Hiring!

YugaByte is growing fast and we’d like you to help us keep the momentum going! Check out our currently open positions:

Our team consists of domain experts from leading software companies such as Facebook, Oracle, Nutanix, Google and LinkedIn. We have come a long way in a short time but we cannot rest on our past accomplishments. We need your ideas and skills to make us better at every function that is necessary to create the next great software company. All while having tons of fun and blazing new trails!

Get Started

Ready to start exploring YugabyteDB features? Getting up and running locally on your laptop is fast. Visit our quickstart page to get started.

High performance distributed SQL by Yugabyte

What’s Next?

  • Compare YugabyteDB in depth to databases like CockroachDB, Google Cloud Spanner and MongoDB.
  • Get started with YugabyteDB on macOS, Linux, Docker and Kubernetes.
  • Contact us to learn more about licensing, pricing or to schedule a technical overview.

Related Posts

Jimmy Guerrero

VP Developer Relations

Related Posts

Learn More to Accelerate Your Retail Business

Ready to dive deeper into distributed SQL, YugabyteDB, and Yugabyte Cloud?
Learn at Yugabyte University
Learn More
Browse Yugabyte Docs
Read More
Join the Yugabyte Community
Join Now