What is Snowflake Search Optimization?

Published
July 5, 2024
Author

Snowflake's Search Optimization Service (SOS) is a feature designed to enhance the performance of highly selective queries on the Snowflake data platform. By creating a secondary data structure known as the Search Access Path, SOS tracks which values appear in micro-partitions, significantly optimizing the scanning process for quicker query responses.

Example of creating and optimizing a table with SOS

create or replace table test_table (id int, c1 int, c2 string, c3 date) as select * from values
(1, 3, '4', '1985-05-11'),
(2, 4, '3', '1996-12-20'),
(3, 2, '1', '1974-02-03'),
(4, 1, '2', '2004-03-09'),
(5, null, null, null);

alter table test_table add search optimization;

This code snippet demonstrates how to create a table and apply search optimization in Snowflake. The alter table statement adds the search optimization feature to the table, enabling faster query performance.

What are the benefits of Snowflake Search Optimization?

Snowflake's SOS provides several key benefits:

  • Performance: Reduces query times by up to 95%. For example, Panther Labs achieved over 100x speed improvements.
  • Cost Efficiency: Enables better cost management and significant savings on search optimization.
  • Versatility: Suitable for various use cases, including unique object UUIDs, text substrings, specific IP addresses, and geospatial data searches.

How to implement Snowflake Search Optimization?

1. Evaluation

Assess whether a table is suitable for SOS based on its query patterns and data structure.

Example of evaluating a table for SOS

select * from information_schema.tables where table_name = 'test_table';

This query retrieves information about the table, helping to determine if it is a good candidate for search optimization.

2. Cost Estimation

Use Snowflake tools to estimate the build and maintenance costs of the Search Access Path.

Example of estimating SOS costs

select system$estimate_search_optimization_costs('test_table');

This function provides an estimate of the costs associated with implementing search optimization on the specified table.

3. Build Process

Start building the Search Access Paths for the selected tables and columns.

Example of building the Search Access Path

alter table test_table add search optimization;

This command initiates the creation of the Search Access Path, enhancing query performance for the table.

4. Query Optimization

Write queries that leverage the search paths for better performance.

Example of optimized queries

select * from test_table where id = 2;
select * from test_table where c2 = '1';

These queries utilize the Search Access Path to quickly retrieve the desired data, significantly improving performance.

Common Challenges and Solutions

  • Ensuring the table is fully optimized before measuring performance improvements to achieve accurate results.
  • Managing storage and compute costs associated with maintaining the search access path.
  • Monitoring the progress of the Search Access Path through the search_optimization_progress column in SHOW TABLES.

Recap of Snowflake Search Optimization

  • Snowflake's SOS significantly enhances query performance by creating a Search Access Path.
  • It provides substantial cost savings and is versatile for various use cases.
  • Implementing SOS involves evaluation, cost estimation, building the Search Access Path, and optimizing queries.

Keep reading

See all