Introducing Chaos Genius for Databricks Cost Optimization

Get started

HOW TO: Use Snowflake Sequence for Generating Unique Values (2024)

Sequences are database objects. They generate unique, sequential numbers, which are crucial for tasks like creating primary keys, surrogate keys, ensuring data integrity, and generating unique identifiers. Think of them as automatic number-generating counters. Snowflake sequences are particularly powerful because they can generate unique numbers across sessions and concurrent statements, making them robust and reliable for various operations.

In this article, we will cover Snowflake sequences in detail, covering their features, usage, and benefits. On top of that, we will provide a step-by-step guide on creating and managing sequences. We will also cover best practices and potential limitations. These will help you use them well in your Snowflake environment.

What Is Snowflake Sequence?

Snowflake sequence is a special schema-level object designed to generate unique, sequential numbers. It's a powerful tool that allows you to create a series of numeric values that automatically increment based on predefined rules.

As a schema-level object, a sequence exists independently of any specific table. This means you can use the same sequence across multiple tables within a schema, providing a centralized source of unique numbers for various purposes.

The magic of sequences lies in how they generate these unique, sequential numbers. Whenever you create a sequence, you define its starting point and how it should increment. Each time you request a new value from the sequence (using the NEXTVAL function, which we'll discuss later), Snowflake generates the next number in the sequence based on these rules.

Snowflake sequences are super handy in lots of situations. They help with things like:

  • Primary Keys: Giving each row in a table its own special ID number.
  • Unique Identifiers: Generating one-of-a-kind unique values for columns that require distinct values.
  • Data Integrity: Keeping your database clean by making sure no two entries have the same ID

Key characteristics of the Snowflake sequence are:

1) Unique Values: Snowflake sequences guarantee that each generated value is unique within that sequence.

2) Concurrency: They can handle multiple simultaneous requests without generating duplicating values.

3) Persistence: Sequence values persist across database sessions and even system restarts.

4) Gaps: Numbers may have gaps due to transaction rollbacks or concurrent operations.

5) Customizability: You can define the starting point, increment value, and other properties.

6) Independence: Sequences are independent of specific tables, allowing for flexible use across a schema.

7) Order: Values can be generated in an ordered or unordered fashion, depending on the sequence configuration.

Syntax and Basic Usage of Snowflake Sequence

Basic Syntax

Creating a sequence in Snowflake is straightforward. Here's the basic syntax:

CREATE [ OR REPLACE ] SEQUENCE [ IF NOT EXISTS ] <name>
  [ WITH ]
  [ START [ WITH ] [ = ] <initial_value> ]
  [ INCREMENT [ BY ] [ = ] <sequence_interval> ]
  [ { ORDER | NOORDER } ]
  [ COMMENT = '<string_literal>' ]
Snowflake Sequence syntax

Parameters

Let's break down the parameters:

  • name: This is the identifier for your sequence. It must be unique within the schema.
  • START WITH: Specifies the first value the sequence will return. Default is 1.
  • INCREMENT BY: Defines how much the sequence increases with each new value. Default is 1.
  • ORDER | NOORDER: Determines whether values are generated in strict order (ORDER) or not (NOORDER). NOORDER is the default and can improve performance.
  • COMMENT: An optional description of the sequence.

Here's a simple example to create a Snowflake sequence:

CREATE SEQUENCE my_first_sequence
  START WITH 1
  INCREMENT BY 1
  COMMENT = 'Generates unique IDs that start with 1 and increment by 1';
Creating a simple Snowflake Sequence
Creating a simple Snowflake Sequence - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Creating a simple Snowflake Sequence 

You can see, this makes a sequence called my_first_sequence that starts at 1 and goes up by 1 each time you use it.

Want to take Chaos Genius for a spin?

It takes less than 5 minutes.

Enter your work email
Enter your work email
Snowflake Logo

What Is the Difference Between AUTOINCREMENT and SEQUENCE in Snowflake?

Both AUTOINCREMENT (also called IDENTITY in Snowflake) and SEQUENCE generate unique, incrementing values. But, they have some key differences.

Snowflake AUTOINCREMENT Snowflake SEQUENCE
Snowflake AUTOINCREMENT is a column property that automatically generates unique values for new rows Snowflake SEQUENCE is an independent special schema level that generates unique sequential numbers, which can be used across multiple tables and columns
Snowflake AUTOINCREMENT is table-specific and is applied directly to a column within a table Snowflake SEQUENCE is schema-specific and can be used across multiple tables and columns within the same schema
Snowflake AUTOINCREMENT is best suited for single-table unique key generation Snowflake SEQUENCE is ideal for generating unique values that need to be shared across multiple tables or used in various contexts
Snowflake AUTOINCREMENT does not guarantee gap-free sequences. Gaps can occur due to transactions that are rolled back Snowflake SEQUENCE does not guarantee gap-free sequences either. Gaps can also occur due to rolled-back transactions or skipped values
Snowflake AUTOINCREMENT values are managed internally by Snowflake when inserting new rows Snowflake SEQUENCE values are explicitly managed using NEXTVAL function calls
Snowflake AUTOINCREMENT is less flexible and cannot be used across multiple tables Snowflake SEQUENCE is more flexible and can be reused and referenced in multiple tables and columns
Snowflake AUTOINCREMENT supports ORDER and NOORDER options, with NOORDER providing better performance for concurrent inserts Snowflake SEQUENCE also supports ORDER and NOORDER, with similar performance considerations as AUTOINCREMENT
Syntax:
AUTOINCREMENT (start_value, increment_value) or IDENTITY (start_value, increment_value)
Syntax:
CREATE SEQUENCE sequence_name START start_value INCREMENT increment_value
Example:
CREATE TABLE demo (id NUMBER AUTOINCREMENT, name STRING);
Example:
CREATE SEQUENCE demo_seq START 1 INCREMENT 1;
CREATE TABLE example (id NUMBER DEFAULT demo_seq.NEXTVAL, name STRING);

Snowflake AUTOINCREMENT(or Snowflake IDENTITY) is simpler and more automatic but less flexible, while Snowflake SEQUENCE offers more control and versatility at the cost of slightly more complex management.

Step-by-Step Guide: How Do You Generate Sequential Numbers in Snowflake?

Now that we understand what Snowflake sequence is and how it differs from Snowflake AUTOINCREMENT, let's walk through a step-by-step guide on how to generate sequential numbers using Snowflake sequence.

Step 1—Login, Navigate to Worksheet, Select or Create Database

First things first, log into your Snowflake account and navigate to a worksheet. If you don't have a database and schema set up, create one:

CREATE DATABASE IF NOT EXISTS snowflake_sequence_tutorial;
USE DATABASE snowflake_sequence_tutorial;
CREATE SCHEMA IF NOT EXISTS snowflake_sequence_schema;
USE SCHEMA snowflake_sequence_schema;
Creating Database and Schema - Snowflake Sequence - Snowflake CREATE SEQUENCE

Step 2—Create a Snowflake Sequence

Now, let's create our first sequence using the Snowflake CREATE SEQUENCE command:

CREATE SEQUENCE demo_sequence
  START WITH 1
  INCREMENT BY 1
  ORDER
  COMMENT = 'Demo Snowflake sequence';
Creating Snowflake Sequence
Creating Snowflake Sequence - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Creating Snowflake Sequence

Step 3—Retrieve Sequence Values Using Snowflake NEXTVAL

To get the next value from your sequence, use the Snowflake NEXTVAL function:

SELECT demo_sequence.NEXTVAL;
Retrieving Snowflake Sequence values using Snowflake NEXTVAL
Retrieving Snowflake Sequence values using Snowflake NEXTVAL - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Retrieving Snowflake Sequence values using Snowflake NEXTVAL

Here, this will return the next value in the sequence (in this case, 1).

Step 4—Incrementing the Snowflake Sequence

Each time you call Snowflake NEXTVAL, the sequence will automatically increment:

SELECT demo_sequence.NEXTVAL,
       demo_sequence.NEXTVAL,
       demo_sequence.NEXTVAL,
       demo_sequence.NEXTVAL,
       demo_sequence.NEXTVAL,
       demo_sequence.NEXTVAL,
       demo_sequence.NEXTVAL,
       demo_sequence.NEXTVAL; 
Incrementing the Snowflake Sequence
Incrementing the Snowflake Sequence - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Incrementing the Snowflake Sequence

As you can see, this query will return 2, 3, 4, 5, 6, 7, 8, and 9 respectively.

Step 5—Creating Custom Snowflake Sequence

Let's create a more customized sequence:

CREATE SEQUENCE custom_sequence
  START WITH 100
  INCREMENT BY 10
  ORDER
  COMMENT = 'Custom sequence starting at 100 and incrementing by 10';
Creating Custom Snowflake Sequence - Snowflake CREATE SEQUENCE
Creating Custom Snowflake Sequence - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Creating Custom Snowflake Sequence

Step 6—Create Table and Insert Data Using the Snowflake Sequence

Snowflake sequences can also be used to populate columns in a table. Now, let's put our sequence to use in a table:

CREATE OR REPLACE TABLE demo_users (
  user_id INT DEFAULT custom_sequence.NEXTVAL,
  username STRING,
  email STRING
);

INSERT INTO demo_users (username, email)
VALUES ('elon_musk', '[email protected]'),
       ('jeff_bezos', '[email protected]'),
       ('mark_zuck', '[email protected]'),
       ('warren_buffet', '[email protected]'),
       ('bill_gates', '[email protected]');

SELECT * FROM demo_users;
Creating a table and inserting data using the Snowflake Sequence
Creating a table and inserting data using the Snowflake Sequence - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Creating a table and inserting data using the Snowflake Sequence

In this example, you can see that this will create a table with user_id values of 100, 110, 120, 130, and 140 respectively.

Step 7—Customize Snowflake Sequence Properties

You can customize various properties of your sequence using the ALTER SEQUENCE command:

1) Rename a Snowflake Sequence

If you need to rename an existing sequence, you can use the following command. It changes the name of demo_sequence to new_demo_sequence.

ALTER SEQUENCE IF EXISTS demo_sequence RENAME TO new_demo_sequence;
Renaming Snowflake Sequence

2) Alter the Increment Value of a Snowflake Sequence

To adjust the increment value of a sequence, use this command:

ALTER SEQUENCE IF EXISTS new_demo_sequence SET INCREMENT BY 10;
Altering the Increment value of Snowflake Sequence

Here, the increment value for new_demo_sequence is set to 10, meaning each new value generated will be 10 units apart.

3) Set Ordering for a Snowflake Sequence

If you need to ensure the values generated by a sequence are ordered, you can use:

ALTER SEQUENCE IF EXISTS new_demo_sequence SET ORDER;
Setting order for a Snowflake Sequence

This command ensures that new_demo_sequence will generate values in a guaranteed order.

4) Add a Comment to a Snowflake Sequence

Adding a comment to a sequence can be helpful for documentation purposes:

ALTER SEQUENCE IF EXISTS new_demo_sequence SET COMMENT = 'This is a sequence for user IDs';
Adding a comment to Snowflake Sequence

This adds the comment "This is a sequence for user IDs" to new_demo_sequence.

5) Remove a Comment from a Snowflake Sequence

If you need to remove an existing comment from a Snowflake sequence, use:

ALTER SEQUENCE IF EXISTS new_demo_sequence UNSET COMMENT;
Removing the Comment from a Snowflake Sequence

This command removes any comment that was previously set for new_demo_sequence.

To get the full description of the Snowflake sequence, including the sequence’s interval, you can use the DESCRIBE command.

DESC SEQUENCE new_demo_sequence;
Describing Snowflake Sequence
Describing Snowflake Sequence - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Describing Snowflake Sequence

Step 8—Verify the Snowflake Sequence Using SHOW SEQUENCES

To see all sequences in your current schema:

SHOW SEQUENCES;
Verifying Snowflake Sequence using SHOW SEQUENCES
Verifying Snowflake Sequence using SHOW SEQUENCES - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Verifying Snowflake Sequence using SHOW SEQUENCES

Step 9—Manage the Snowflake Sequence (as needed)

Alter the Sequence (Optional):

Refer to the detailed examples in the steps above.

Drop the Sequence (Optional):

DROP SEQUENCE IF EXISTS new_demo_sequence;
Dropping Snowflake Sequence
Dropping Snowflake Sequence - Snowflake Sequence - Snowflake CREATE SEQUENCE - Create Sequence in Snowflake - Snowflake IDENTITY - Snowflake AUTOINCREMENT - Snowflake NEXTVAL
Dropping Snowflake Sequence

Follow these steps to make, use, and manage sequences in Snowflake. They generate numbers for various uses.

Best Practices for Using Snowflake Sequence

Here's how you can get the best out of Snowflake sequence and keep things running smoothly:

1) Use sequences for primary keys and unique identifiers

When you need unique values for primary keys or other columns, sequences are the way to go. This keeps your data in check and makes management a whole lot easier.

2) Leverage sequences for generating unique values across tables

Since sequences are schema-level objects. You can use a single sequence to generate unique values across multiple tables. This is particularly useful for maintaining consistency in related data across different tables.

3) Use NOORDER for improved performance

Unless you specifically need values to be generated in strict order, use the NOORDER option when creating sequences. This can significantly improve performance, especially during concurrent insert operations.

4) Modify properties with ALTER SEQUENCE as needed

Don't hesitate to alter sequence properties if your requirements change. You can modify the increment value, cycle option, and other properties using the ALTER SEQUENCE command.

5) Monitor sequence usage

Regularly use the SHOW SEQUENCES and DESCRIBE SEQUENCE commands to monitor your sequences. This helps you keep track of how many values have been used and whether you're approaching any limits.

6) Handle gaps in sequences

Remember, Snowflake sequences may have gaps. This is due to rolled-back transactions or concurrent operations. Design your applications to handle these gaps well. Don't rely on sequence values being strictly consecutive.

7) Avoid hardcoding sequence values

Never hardcode sequence values in your application logic. Always use the NEXTVAL function to retrieve the next value from a sequence.

8) Use appropriate data types

When creating tables that will use sequence values, ensure the column data type can accommodate the full range of values the sequence might generate.

9) Implement error handling

When using sequences, handle errors properly in your apps. This will help you deal with issues like sequence exhaustion.

10) Document your sequences

Keep clear documentation about your sequences. It should include their purpose, usage, and any specific behaviors or limits.

11) Regular maintenance

Periodically review and optimize your Snowflake sequences.

Limitations of Snowflake SEQUENCE

Snowflake Sequence is super powerful, but let's be real—it's not perfect. There are some things it just can't do.

1) Gaps in Values

Sequences don't guarantee gap-free values. Rolled-back transactions, concurrent operations, or system failures can result in gaps in the sequence.

2) No Backwards Order Change

Once a sequence is set to NOORDER, it cannot be changed to ORDER. If you need ordered values, you must specify this when creating the sequence.

3) Duplicate Values with Interval Sign Change

Changing the sequence interval from positive to negative (or vice versa) may result in duplicate values. Be very cautious when modifying the increment direction.

4) Transaction Independence

Sequence operations are independent of transactions. This means that even if a transaction is rolled back, the sequence value is not reversed.

5) Limited Configuration Changes After Creation

Some attributes, like the ordering property, cannot be modified after the sequence is created. Plan your sequence properties carefully.

6) 64-bit Integer Limit

Sequences are limited to the range of a 64-bit integer (-2^63 to 2^63 - 1). While this is a vast range, it's theoretically possible to exhaust it in high-volume scenarios.

7) No currval Support

Unlike some other database systems, Snowflake does not support a currval function for sequences. This can make certain operations more complex.

8) Performance Trade-offs with ORDER

While NOORDER can improve performance, it removes the guarantee of strictly ordered values. This can impact scenarios where the exact order of values is critical.

9) Schema-Level Scope

Sequences are schema-level objects, which means they can't be easily shared across different schemas or databases without additional logic.

10) Potential for Contention

In very high-concurrency scenarios, sequences can become a point of contention. This can hurt performance.

Further Reading

If you want to get more info about Snowflake CREATE SEQUENCE, here are some great resources:

Save up to 30% on your Snowflake spend in a few minutes!

Enter your work email
Enter your work email
Snowflake Background Snowflake Background

Conclusion

And that's a wrap! Snowflake sequence is a super handy tool for generating unique numbers in a sequence. You can use it to create primary keys or assign unique IDs across multiple tables. It's a reliable and efficient solution to many data management headaches.

In this article, we have covered:

  • What is Snowflake sequence?
  • What is the difference between autoincrement and sequence in Snowflake?
  • How do you generate sequential numbers in Snowflake?
  • Best practices for implementing Snowflake Sequence
  • Limitations of Snowflake Sequence

…and so much more!

FAQs

What is a SEQUENCE in Snowflake?

Snowflake Sequence is a schema-level object used to generate unique, sequential numbers. These are useful for primary keys or any columns requiring unique values.

How can you alter an existing Sequence?

Use the ALTER SEQUENCE command to modify properties such as increment interval.

Can you change a SEQUENCE from NOORDER to ORDER after creation?

No, once a Snowflake Sequence is set to NOORDER, it cannot be changed to ORDER.

What is the purpose of the GETNEXTVAL function?

It allows precise control over sequence generation in complex queries involving joins.

Can Snowflake Sequences have gaps in the values?

Yes, Snowflake does not guarantee gap-free sequences, especially with transaction rollbacks and concurrent inserts.

What happens if a Snowflake Sequence is dropped but still referenced in a table?

Any insert or update operations using the Snowflake Sequence will result in an error.

Can Snowflake Sequences be used across multiple tables?

Yes, a single Snowflake Sequence can generate unique values for multiple tables and columns.

What is the maximum supported range for Snowflake sequences?

Snowflake sequences support 64-bit two’s complement integers, ranging from -2^63 to 2^63 - 1.

How do sequences differ from autoincrement functionality?

Sequences are schema-level objects used across multiple tables, while AUTOINCREMENT is a column-level attribute specific to a single table.

Can sequences be used across different schemas or databases?

Sequences are defined within a schema but can be referenced in any table within that schema. They cannot be directly used across different databases or schemas.

How does Snowflake handle sequence exhaustion?

When a sequence reaches its limit, Snowflake throws an error. To handle this, create a new sequence or use a smaller increment value.

Are sequence values guaranteed to be unique across all tables?

Yes, as long as the sequence's interval sign does not change, the values are guaranteed to be unique across all tables using that sequence.

Can I use negative increments with sequences?

Yes, you can create a sequence with a negative increment.

Is it possible to use Snowflake Sequences with other SQL commands?

Yes, Snowflake Sequences can be used in combination with commands like INSERT, UPDATE, and SELECT.

Pramit Marattha

Technical Content Lead

Pramit is a Technical Content Lead at Chaos Genius.

People who are also involved

“Chaos Genius has been a game-changer for our DataOps at NetApp. Thanks to the precise recommendations, intuitive interface and predictive capabilities, we were able to lower our Snowflake costs by 28%, yielding us a 20X ROI

Chaos Genius has given us a much better understanding of what's driving up our data-cloud bill. It's user-friendly, pays for itself quickly, and monitors costs daily while instantly alerting us to any usage anomalies.

Anju Mohan

Director, IT

Simon Esprit

Chief Technology Officer

Join today to get upto
30% Snowflake
savings

Join today to get upto 30% Snowflake savings

Unlock Snowflake Savings Join waitlist
Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.