Creating and managing shares in Snowflake - Performance & Efficiency
When creating and managing shares in Snowflake, it's important to know how the time to complete these tasks changes as you add more objects or accounts.
We want to understand how the number of operations grows when we create or update shares.
Analyze the time complexity of the following operation sequence.
CREATE SHARE my_share;
GRANT USAGE ON DATABASE my_db TO SHARE my_share;
GRANT SELECT ON TABLE my_db.public.table1 TO SHARE my_share;
GRANT SELECT ON TABLE my_db.public.table2 TO SHARE my_share;
ALTER SHARE my_share ADD ACCOUNTS = ('account1');
ALTER SHARE my_share ADD ACCOUNTS = ('account2');
ALTER SHARE my_share ADD ACCOUNTS = ('account3');
This sequence creates a share, grants access to a database and its tables, then adds multiple accounts to the share.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Granting privileges on each table and adding accounts to the share.
- How many times: Once per table for grants, once per account for adding to the share.
As the number of tables or accounts grows, the number of grant and add operations grows too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 tables/accounts | About 20 operations (10 grants + 10 account adds) |
| 100 tables/accounts | About 200 operations |
| 1000 tables/accounts | About 2000 operations |
Pattern observation: The operations increase roughly in direct proportion to the number of tables and accounts.
Time Complexity: O(n)
This means the time to create and manage shares grows linearly with the number of tables and accounts involved.
[X] Wrong: "Adding more accounts to a share takes the same time no matter how many accounts there are."
[OK] Correct: Each account added requires a separate operation, so more accounts mean more time.
Understanding how operations scale when managing shares helps you design efficient data sharing strategies and shows you can think about system behavior as it grows.
"What if we granted privileges on schemas instead of individual tables? How would the time complexity change?"
Practice
Solution
Step 1: Understand the concept of shares in Snowflake
Shares allow data to be shared between accounts without duplicating the data itself.Step 2: Identify the main use case of shares
Sharing data without copying saves storage and ensures data consistency.Final Answer:
To share data with other accounts without copying it -> Option CQuick Check:
Shares = Data sharing without copying [OK]
- Thinking shares create data backups
- Confusing shares with data movement
- Assuming shares encrypt data automatically
my_share?Solution
Step 1: Recall the syntax for creating shares in Snowflake
The correct command to create a share isCREATE SHARE <share_name>;.Step 2: Compare options with correct syntax
Only CREATE SHARE my_share; uses the correctCREATE SHAREsyntax.Final Answer:
CREATE SHARE my_share; -> Option DQuick Check:
CREATE SHARE = correct command [OK]
- Using MAKE or NEW instead of CREATE
- Forgetting the semicolon at the end
- Confusing ADD SHARE with creating
CREATE SHARE my_share;
GRANT USAGE ON DATABASE sales_db TO SHARE my_share;
ALTER SHARE my_share ADD ACCOUNTS = ('account_123');What does
account_123 gain access to?Solution
Step 1: Analyze the GRANT USAGE ON DATABASE to share
Granting USAGE on a database to a share allows consumers to view database metadata (e.g., SHOW DATABASES, USE DATABASE) but not schemas, tables, or other internal objects.Step 2: Understand the effect of adding accounts
Addingaccount_123to the share provides access limited to the granted privileges: database metadata only, as no schema or table privileges are granted.Final Answer:
Only metadata about sales_db -> Option BQuick Check:
GRANT USAGE ON DATABASE = metadata access only [OK]
- Assuming full access to all objects in sales_db
- Thinking DB USAGE grants access to internal objects like tables
- Believing roles must be granted separately
CREATE SHARE my_share; GRANT SELECT ON TABLE sales TO SHARE my_share;
But get an error. What is the likely cause?
Solution
Step 1: Check the syntax for granting privileges on objects to shares
Privileges on tables must be granted using fully qualified names:database.schema.table.Step 2: Identify the missing qualification in the command
The command uses justsaleswithout database and schema, causing an error.Final Answer:
The table name must be fully qualified with database and schema -> Option AQuick Check:
Fully qualify table names when granting [OK]
- Granting on database instead of table
- Trying to grant privileges before adding accounts
- Assuming shares can't have table privileges
Solution
Step 1: Understand share scope and management
Shares are scoped to a single database, so sharing tables from different databases requires separate shares.Step 2: Evaluate options for managing multiple accounts and databases
Creating separate shares per database allows clear control and easier management of access.Step 3: Consider drawbacks of other options
Copying tables wastes storage; granting privileges directly is less secure and scalable.Final Answer:
Create separate shares per database and add accounts to each share accordingly -> Option AQuick Check:
Shares are per database, use multiple shares [OK]
- Trying to share multiple databases in one share
- Copying data instead of sharing
- Granting privileges directly to accounts bypassing shares
Ready to prove it?
Pass the test and this pattern moves unknown
