Streamlit application to automatically analyze and optimize the most expensive SQL queries in Snowflake, using Snowflake Cortex AI (Claude Sonnet) to generate optimization recommendations.
- β Identification of the 20 most expensive queries (last 30 days)
- β Display of execution and performance metrics
- β Automatic analysis of table schemas and statistics
- β AI-powered optimization recommendations (Claude Sonnet)
- β Dual support: Streamlit in Snowflake (SiS) and local development
- Cost optimization: Identify queries that consume the most credits
- Performance improvement: Reduce execution times
- Performance audit: Analyze warehouse usage by user
βββββββββββββββββββββββββββββββββββββββ
β User Interface (Streamlit) β
β app.py β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Business Logic (QueryOptimizer) β
β query_optimizer.py β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Data Access (SnowflakeConnector) β
β snowflake_connector.py β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Snowflake Backend β
β - ACCOUNT_USAGE.QUERY_HISTORY β
β - INFORMATION_SCHEMA β
β - SNOWFLAKE.CORTEX.COMPLETE β
βββββββββββββββββββββββββββββββββββββββ
βββ app.py # Main Streamlit application
βββ snowflake_connector.py # Connection and data access
βββ query_optimizer.py # Business logic and optimization
βββ requirements.txt # Python dependencies
βββ README.md # Documentation
- Python 3.8+
- Streamlit β₯1.28.0
- Snowflake Connector β₯3.0.0
- Pandas β₯2.0.0
Snowflake provides native Git repository integration, allowing you to synchronize your remote Git repository with Snowflake and deploy Streamlit apps directly from Git. This enables version control, collaborative development, and streamlined deployment workflows.
Prerequisites:
- A remote Git repository (e.g., GitHub:
https://github.com/lletourmy/finop) - Snowflake account with appropriate permissions
- ACCOUNTADMIN role or role with CREATE INTEGRATION, CREATE SECRET, and CREATE GIT REPOSITORY privileges
Step 1: Set Up Git Repository in Snowflake
-
Create a secret for Git authentication (if using HTTPS with credentials)
CREATE SECRET git_credentials TYPE = GENERIC_STRING SECRET_STRING = 'your_username:your_personal_access_token';
Or use OAuth integration for GitHub:
CREATE SECRET git_oauth TYPE = OAUTH2 OAUTH_CLIENT_ID = 'your_client_id' OAUTH_CLIENT_SECRET = 'your_client_secret' OAUTH_REFRESH_TOKEN = 'your_refresh_token';
-
Create API integration (for GitHub, GitLab, etc.)
CREATE API INTEGRATION git_api_integration API_PROVIDER = git_https_api API_ALLOWED_PREFIXES = ('https://github.com', 'https://gitlab.com') ALLOWED_AUTHENTICATION_SECRETS = (git_credentials) ENABLED = TRUE;
-
Create Git repository in Snowflake
CREATE GIT REPOSITORY finopt_repo API_INTEGRATION = git_api_integration GIT_CREDENTIALS = git_credentials ORIGIN = 'https://github.com/lletourmy/finop.git';
Step 2: Fetch from Remote Repository
Synchronize the remote repository to the Git repository clone in Snowflake:
ALTER GIT REPOSITORY finopt_repo FETCH;Step 3: Create Streamlit App from Git Repository
Create your Streamlit app referencing files from the Git repository:
CREATE STREAMLIT sql_query_optimizer
ROOT_LOCATION = '@finopt_repo/branches/main'
MAIN_FILE = 'app.py'
QUERY_WAREHOUSE = 'YOUR_WAREHOUSE';Step 4: Grant Permissions
GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE YOUR_ROLE;
GRANT USAGE ON WAREHOUSE YOUR_WAREHOUSE TO ROLE YOUR_ROLE;
GRANT USAGE ON STREAMLIT sql_query_optimizer TO ROLE YOUR_ROLE;Step 5: Update Your App
When you push changes to your remote Git repository, fetch the latest version:
ALTER GIT REPOSITORY finopt_repo FETCH;Then recreate or alter your Streamlit app to use the updated files. Streamlit apps automatically pick up changes from the Git repository clone.
References:
- Snowflake Git Repository Overview
- Setting up Snowflake to use Git
- Git operations in Snowflake
- Streamlit Git Integration
You can also use the Snowflake CLI for deployment:
-
Install Snowflake CLI
pip install snowflake-cli
-
Configure and deploy
snow connection add snow streamlit deploy --replace
If you prefer not to use Git integration:
-
Create a stage and upload files
CREATE STAGE IF NOT EXISTS streamlit_stage; PUT file://app.py @streamlit_stage AUTO_COMPRESS=FALSE OVERWRITE=TRUE; PUT file://snowflake_connector.py @streamlit_stage AUTO_COMPRESS=FALSE OVERWRITE=TRUE; PUT file://query_optimizer.py @streamlit_stage AUTO_COMPRESS=FALSE OVERWRITE=TRUE;
-
Create the Streamlit application
CREATE STREAMLIT sql_query_optimizer ROOT_LOCATION = '@streamlit_stage' MAIN_FILE = 'app.py' QUERY_WAREHOUSE = 'YOUR_WAREHOUSE';
-
Clone the repository
git clone https://github.com/lletourmy/finop.git cd finop -
Install dependencies
pip install -r requirements.txt
-
Configure connection (see Configuration section)
-
Run the application
streamlit run app.py
Location: ~/.snowflake/config.toml
Format:
[dev]
account = "your_account"
user = "your_username"
password = "your_password"
database = "your_database"
schema = "your_schema"
warehouse = "your_warehouse"
role = "your_role"
authenticator = "snowflake"Security:
chmod 600 ~/.snowflake/config.toml-- Access to Account Usage
GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE YOUR_ROLE;
-- Warehouse access
GRANT USAGE ON WAREHOUSE YOUR_WAREHOUSE TO ROLE YOUR_ROLE;
-- Access to databases to analyze
GRANT USAGE ON DATABASE YOUR_DATABASE TO ROLE YOUR_ROLE;
GRANT SELECT ON ALL TABLES IN DATABASE YOUR_DATABASE TO ROLE YOUR_ROLE;- The application automatically connects via
st.connection("snowflake") - Click "π Refresh the list of queries"
- Select a query from the table
- Click "π AI optimization"
- Review the optimization suggestions
- Launch the application:
streamlit run app.py - In the sidebar, select a connection from
config.toml - Click "Connect"
- Use the application as in SiS mode
- "Connection not available" (SiS): Verify you are in SiS mode and check role permissions
- "Config file not found" (Local): Verify
~/.snowflake/config.tomlexists and is properly formatted - "Connection failed": Verify connection parameters (account, user, password, warehouse)
-
"No queries found":
- Verify Account Usage permissions:
GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE TO ROLE YOUR_ROLE - Verify there are queries in the last 30 days
- Wait for data propagation (45-minute delay for Account Usage)
- Verify Account Usage permissions:
-
"Cortex AI error":
- Verify Cortex AI is enabled
- Verify permissions:
GRANT IMPORTED PRIVILEGES ON DATABASE SNOWFLAKE - Try a different model (e.g., 'claude-3-haiku')
-
"Table metadata not found":
- Verify the table exists and is accessible
- Verify SELECT permissions on the table
- Verify the name format (database.schema.table)
- Slow application: Use a larger warehouse or reduce the time window
- AI timeout: Reduce the number of tables analyzed or use a faster model
Manages all interactions with Snowflake.
Main methods:
init_connection(): Initializes connection (SiS or local)execute_query(query, params=None): Executes a SQL querycall_cortex_ai(prompt, model='claude-3-5-sonnet'): Calls Cortex AI
Contains the optimization business logic.
Main methods:
get_expensive_queries(): Retrieves the 20 most expensive queriesget_query_details(query_id): Retrieves query detailsextract_tables_from_sql(sql_text): Extracts tables from SQLget_table_metadata(table_name): Retrieves table metadataoptimize_query(...): Generates optimization recommendations via AI
User interface and component orchestration.
Workflow:
- Loading expensive queries
- Selecting a query from the table
- Displaying SQL and metrics
- AI analysis with table extraction and recommendation generation
- SQL Injection Prevention: Parameterized queries only
- Prompt Injection Prevention: Escaping apostrophes in prompts
- Credential Management:
- SiS Mode: Native authentication
- Local Mode: Credentials in
~/.snowflake/config.toml(restricted permissions)
- Read-Only Operations: The application only performs SELECT operations
- Network Security: HTTPS connections only
- Export recommendations to PDF/CSV
- Analysis history
- Before/after optimization comparison
- Performance trends dashboard
- Automatic alerts
- Unit tests and CI/CD
- GitHub Repository: https://github.com/lletourmy/finop
- Issues: https://github.com/lletourmy/finop/issues
- Snowflake Cortex AI Documentation: https://docs.snowflake.com/en/user-guide/snowflake-cortex/llm-functions
Last updated: December 2025
Author: Laurent Letourmy