🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Optimization Techniques: Clustering, Search Optimization & Materialized Views

đŸŸĸ Free Lesson

Optimization Techniques: Clustering, Search Optimization & Materialized Views

Architecture Diagram 1: Clustering Key Architecture

Clustering Key Impact on Partition OrganizationWithout Clustering KeyRandom distribution: US/EU/APAC mixedDepth {'>'} 2.5 | Full scans neededWith CLUSTER BY (region)US in MP_001-002, EU in MP_003-004Depth {'<'} 1.5 | 80-95% pruningCLUSTER BYGood ClusteringDepth {'<'} 1.5 | Overlap {'<'} 20%Poor ClusteringDepth {'>'} 2.5 | Overlap {'>'} 40%Depth {'<'} 1.5Depth {'>'} 2.5

Architecture Diagram 2: Search Optimization Service

Search Optimization: Point Lookup Index (POI)Problem: Point Lookupsemail = 'john@...' scansALL partitionsEnablePoint Lookup Index (POI)Email → Micro-Partition map10-20% storage overheadIndexResult: 60-600x FasterScan 2 partitionsinstead of 10,000Auto-updateAutomatic MaintenanceBackground updates + Statistics collection

Architecture Diagram 3: Clustering Key Impact

Clustering Key Impact on Partition OrganizationWithout Clustering KeyRandom distribution across partitionsUSEUUSAPACDepth {'>'} 2.5 | Full scans neededWith CLUSTER BY (region)Organized by region in partitionsUSEUAPACDepth {'<'} 1.5 | 80-95% pruningCLUSTER BY

Architecture Diagram 4: Materialized View Performance

Materialized View vs Standard View PerformanceStandard ViewRe-executes aggregation each queryO(n) cost per queryMaterialized ViewPre-computed results stored on diskO(1) cost per queryCREATE MVAutomatic incremental refresh on base table changes

Architecture Diagram 5: Materialized Views Architecture

Materialized View vs Standard View PerformanceStandard ViewRe-executes aggregation each queryO(n) cost per queryMaterialized ViewPre-computed results on diskO(1) cost per queryCREATE MVAutomatic RefreshIncremental updates on base changesManual RefreshALTER MV ... REFRESHBackgroundOn-demand



Detailed Explanation

What are Clustering Keys?

Clustering keys define how data is organized within micro-partitions, optimizing query performance by improving data locality.


Clustering Key Selection

  • Choose columns frequently used in WHERE, JOIN, GROUP BY
  • Low-to-medium cardinality works best (date, region, status)
  • High-cardinality (UUID, microsecond timestamps) less effective
  • Track via CLUSTERING_INFORMATION() — target depth < 1.5

Automatic Clustering

  • Background service continuously optimizes data placement
  • Runs asynchronously — no user intervention
  • Cost-based approach: re-clusters only when benefits outweigh costs
  • Moves rows between micro-partitions to reduce overlap

Search Optimization Service

AspectDescription
PurposePoint lookups on non-clustering columns
MechanismPoint Lookup Index (POI) maps values → micro-partitions
Speedup60–600× for equality searches
Overhead10–20% storage + background compute
Best forHigh-cardinality columns with equality searches
  • Not recommended for low-cardinality or range query columns

Materialized Views

  • Pre-compute and store query results on disk
  • Standard views re-execute each time; MVs return cached results
  • Automatic refresh — incremental updates on base table changes
  • Optimizer transparently rewrites queries to use MVs when possible

Performance Monitoring and Tuning

  1. Use QUERY_HISTORY for execution statistics
  2. Use PROFILE for detailed execution plans
  3. Monitor partition pruning effectiveness, clustering depth, MV freshness
  4. Identify slow queries and optimization opportunities

Key Takeaway: Not all tables need clustering — consider query patterns and cost. Use search optimization only for high-cardinality equality lookups.

Key Concepts Table

OptimizationPurposeOverheadBest For
Clustering KeysData organizationLowRange queries, JOINs
Search OptimizationPoint lookupsHighEquality searches
Materialized ViewsPre-computed resultsMediumComplex aggregations
Result CacheQuery result cachingNoneRepeated identical queries
Clustering Key SelectionCardinalityQuery PatternEffectiveness
Date columnLow-MediumRange queriesHigh
Region/CountryLowGROUP BY, WHEREHigh
Customer IDHighPoint lookupsMedium
TimestampHighRange queriesMedium
Composite keyVariesComplex queriesHigh
Search OptimizationColumn TypeQuery PatternStorage Overhead
High cardinalitySTRING/NUMBEREquality (=)10-20%
Low cardinalitySTRINGEquality (=)5-10%
Range queriesNUMBER/DATERange (>, <)Not recommended

Code Examples

-- Example 1: Create table with clustering key
CREATE TABLE sales_data (
    id INTEGER,
    transaction_date DATE,
    region VARCHAR(10),
    product_id VARCHAR(20),
    amount NUMBER(10,2)
)
CLUSTER BY (transaction_date, region);

-- Example 2: Alter table to add clustering key
ALTER TABLE sales_data CLUSTER BY (transaction_date, region);

-- Example 3: Check clustering information
SELECT *
FROM TABLE(INFORMATION_SCHEMA.CLUSTERING_INFORMATION(
    TABLE_NAME => 'sales_data',
    SCHEMA_NAME => 'PUBLIC'
));

-- Example 4: Create materialized view
CREATE MATERIALIZED VIEW sales_summary_mv
AS
SELECT 
    transaction_date,
    region,
    COUNT(*) as transaction_count,
    SUM(amount) as total_amount,
    AVG(amount) as avg_amount
FROM sales_data
GROUP BY 1, 2;

-- Example 5: Enable search optimization
ALTER TABLE sales_data ADD SEARCH OPTIMIZATION ON EQUALITY (product_id);

-- Example 6: Refresh materialized view
ALTER MATERIALIZED VIEW sales_summary_mv REFRESH;

-- Example 7: Check materialized view freshness
SELECT 
    name,
    refresh_group,
    last_refresh_time,
    seconds_since_refresh
FROM INFORMATION_SCHEMA.MATERIALIZED_VIEWS
WHERE name = 'SALES_SUMMARY_MV';

-- Example 8: Analyze clustering effectiveness
SELECT 
    table_name,
    clustering_key,
    total_partition_count,
    total_constant_partition_count,
    average_partition_depth,
    average_overlap_bytes,
    average_partition_depth * average_overlap_bytes as clustering_score
FROM TABLE(INFORMATION_SCHEMA.CLUSTERING_INFORMATION(
    TABLE_NAME => 'sales_data'
));

-- Example 9: Check search optimization status
SHOW SEARCH OPTIMIZATION IN TABLE sales_data;

-- Example 10: Compare query performance
-- Without materialized view
EXPLAIN SELECT 
    transaction_date,
    region,
    SUM(amount)
FROM sales_data
WHERE transaction_date >= '2024-01-01'
GROUP BY 1, 2;

-- With materialized view (optimizer should rewrite)
EXPLAIN SELECT 
    transaction_date,
    region,
    SUM(amount)
FROM sales_summary_mv
WHERE transaction_date >= '2024-01-01'
GROUP BY 1, 2;

Performance Metrics

MetricTargetWarningCriticalDescription
Clustering Depth< 1.51.5-2.5> 2.5Average micro-partition overlap
Partition Pruning> 80%60-80%< 60%Percentage of partitions eliminated
Search Optimization Hit> 90%70-90%< 70%POI lookup success rate
Materialized View Freshness< 1 hour1-24 hours> 24 hoursTime since last refresh
Cache Hit Rate> 50%30-50%< 30%Result cache effectiveness

Best Practices

  1. Choose clustering keys wisely: Select columns frequently used in WHERE, JOIN, and GROUP BY clauses. Prefer low-to-medium cardinality columns for better pruning.

  2. Monitor clustering depth: Regularly check clustering information to identify tables with poor clustering. Consider re-clustering for tables with depth > 2.0.

  3. Use search optimization selectively: Enable search optimization only for high-cardinality columns used in equality searches. Avoid for low-cardinality or range query columns.

  4. Create materialized views for aggregations: Materialize complex aggregations that are queried frequently. Consider refresh frequency requirements and storage costs.

  5. Monitor materialized view freshness: Ensure materialized views are refreshed within SLA requirements. Use manual refresh for time-critical scenarios.

  6. Leverage result cache: Ensure identical queries can hit the cache by avoiding non-deterministic functions. Use QUERY_TAG to group similar queries.

  7. Analyze query patterns: Use QUERY_HISTORY to identify slow queries and optimize accordingly. Focus on queries with poor partition pruning.

  8. Consider composite clustering keys: Use multiple columns for clustering when queries frequently filter on multiple columns. Order columns by selectivity.

  9. Balance optimization overhead: Consider storage and compute costs when implementing optimization features. Not all tables need clustering or search optimization.

  10. Regular performance reviews: Conduct weekly optimization reviews to identify performance trends and adjust strategies accordingly.


See Also

CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-03

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Snowflake Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement