You are on page 1of 10

Snowflake DBA scripts

By Poguttu
List databases in Snowflake
select database_name,
created as create_date,
database_owner,
comment
from information_schema.databases
order by database_name;

List schemas in Snowflake


select catalog_name as database,
schema_name,
schema_owner,
created,
last_altered
from information_schema.schemata
order by schema_name;

User List
select catalog_name as database, schema_name,
schema_owner, created, last_altered
from information_schema.schemata
where schema_name not in ('INFORMATION_SCHEMA', 'PUBLIC')
order by schema_name;

show users;
select "name", "login_name", "default_role", "last_success_login", "expires_at_time",from table(result_scan(
last_query_id()))
order by "name";
List of Table
select table_schema,
table_name,
created as create_date,
last_altered as modify_date
from information_schema.tables
where table_type = 'BASE TABLE'
order by table_schema,
table_name;

List tables in Snowflake schema


select t.table_name
from information_schema.tables t
where t.table_schema = 'schema_name' -- put schema name here
and t.table_type = 'BASE TABLE'
order by t.table_name;

Find recently created tables in Snowflake


select table_schema,
table_name,
created,
last_altered
from information_schema.tables
where created > DATEADD(DAY, -30, CURRENT_TIMESTAMP)
and table_type = 'BASE TABLE'
order by created desc;

select table_schema,
table_name,
last_altered as modify_time
from information_schema.tables
where last_altered > DATEADD(DAY, -30, CURRENT_TIMESTAMP)
and table_type = 'BASE TABLE'
order by last_altered desc;
Compare default values of different columns with the same name in Snowflake database
with result as (
select col.table_schema || '.' || col.table_name as table_1,
col.column_default as default_1,
col.column_name,
cmp_col.column_default as default_2,
(cmp_col.table_schema || '.' || cmp_col.table_name) as table_2,
case when (col.table_schema || '.' || col.table_name) <
(cmp_col.table_schema || '.' || cmp_col.table_name)
then 1
else 0 end as duplicate
from information_schema.columns col
join information_schema.columns cmp_col
on cmp_col.column_name = col.column_name
and not (col.table_schema = cmp_col.table_schema
and col.table_name = cmp_col.table_name)
where (col.column_default <> cmp_col.column_default
or ((col.column_default is null and cmp_col.column_default is not null )
or (cmp_col.column_default is null
and col.column_default is not null)))
and col.table_schema not in ('INFORMATION_SCHEMA')
and cmp_col.table_schema not in ('INFORMATION_SCHEMA')
) select table_1,
default_1,
column_name,
default_2,
table_2
from result
where duplicate = 0
order by table_1, column_name;
List table default values in Snowflake
select table_schema,
table_name,
column_name,
column_default
from information_schema.columns
where column_default is not null
order by table_schema,
table_name, column_name

select count(*) as tables


from information_schema.tables
where table_type = 'BASE TABLE';
Find number of columns in Snowflake
select columns,
tables,
cast(1.0 * columns / tables as decimal (14,2)) as average_column_count
from
(select count(*) as columns,
count(distinct t.table_schema || '.' || t.table_name) as tables
from information_schema.tables t
inner join information_schema.columns c on
c.table_schema = t.table_schema and c.table_name = t.table_name
where t.table_type = 'BASE TABLE'
)q
List tables with the largest number of columns in Snowflake
select t.table_schema || '.' || t.table_name as table_name,
count(*) as columns
from information_schema.tables t
inner join information_schema.columns c on
c.table_schema = t.table_schema and c.table_name = t.table_name
where t.table_type = 'BASE TABLE'
group by t.table_schema, t.table_name
order by count(*) desc;

List sessions / active connections in Snowflake

List active sessions

select query_text,
warehouse_name,
database_name,
schema_name,
user_name,
role_name,
execution_status,
error_code,
error_message,
start_time,
end_time
from table(information_schema.query_history_by_session(session_id))
order by start_time desc -- put session id here
limit 1;
Find queries executed in specific warehouse in Snowflake
select query_text,
warehouse_name,
database_name,
schema_name,
user_name,
role_name,
execution_status,
error_code,
error_message,
start_time,
end_time
from table(information_schema.QUERY_HISTORY_BY_WAREHOUSE('DATAHOUSE'))
order by start_time desc;

List tables by their size in Snowflake


select table_schema,
table_name,
round(bytes/1024/1024, 2) as table_size
from information_schema.tables
where table_type = 'BASE TABLE'
order by table_size desc;
List 10 largest tables in Snowflake
select table_schema,
table_name,
round(bytes/1024/1024, 2) as table_size
from information_schema.tables
where table_type = 'BASE TABLE'
order by table_size desc
limit 10;
Find unused tables in Snowflake
select table_schema,
table_name,
'IS EMPTY' as empty
from information_schema.tables
where row_count = 0
and table_type = 'BASE TABLE'
order by table_schema,
table_name;

Find tables not accessed for past n months in Snowflake


select table_schema,
table_name,
last_altered
from information_schema.tables
where table_type = 'BASE TABLE'
and last_altered < dateadd( 'MONTH', -2, current_timestamp() )
order by table_schema,
table_name;
Schema compare
Compare tables and columns in two databases in Snowflake
select ifnull(db1.table_name, db2.table_name) as table_name,
ifnull(db1.column_name, db2.column_name) as column_name,
db1.column_name as database1,
db2.column_name as database2
from
(select table_schema || '.' || table_name as table_name,
column_name as column_name
from DATABASE_1.information_schema.columns) db1
full join
(select table_schema || '.' || table_name as table_name,
column_name as column_name
from DATABASE_2.information_schema.columns
) db2 on db1.table_name = db2.table_name
and db1.column_name = db2.column_name
where (db1.column_name is null or db2.column_name is null)
order by 1, 2
Compare tables and columns in two schemas in Snowflake
select COALESCE(c1.table_name, c2.table_name) as table_name,
COALESCE(c1.column_name, c2.column_name) as table_column,
c1.column_name as schema1,
c2.column_name as schema2
from
(select table_name,
column_name
from information_schema.columns c
where c.table_schema = 'SCHEMA_1') c1 -- PUT SCHEMA NAME HERE
full join
(select table_name,
column_name
from information_schema.columns c
where c.table_schema = 'SCHEMA_2') c2 -- PUT SCHEMA NAME TO COMPARE HERE
on c1.table_name = c2.table_name and c1.column_name = c2.column_name
where c1.column_name is null
or c2.column_name is null
order by table_name,
table_column;

You might also like