【问题标题】:Get ddl type sqls from Snowflake History从雪花历史中获取 ddl 类型的 sqls
【发布时间】:2021-03-24 07:14:30
【问题描述】:

我有兴趣在 Snowflake 数据库架构上执行 ddl,这个 ddl 就像创建/更改类型 SQL 语句。有没有办法使用 Snowflake.Account_Usage 架构或任何其他方法来捕获此元数据。

【问题讨论】:

    标签: snowflake-cloud-data-platform


    【解决方案1】:

    如果您想使用snowflake 共享数据库,那么您可以使用snowflake.account_usage.query_history 视图来执行此操作。它有一个名为query_type 的列,它显示了运行的查询类型。这是一个例子:

    -- Run some sample queries
    create or replace database sample_database;
    create or replace table sample_database.public.test_table (col1 varchar);
    alter table sample_database.public.test_table add column col2 varchar;
    insert overwrite into sample_database.public.test_table values ('row 1 col 1', 'row 1 col 2'), ('row 2 col 1', 'row 2 col 2');
    
    
    -- Use the snowflake.account_usage.query_history view to see what the query types were
    select 
        query_text, 
        database_name, 
        schema_name, 
        query_type
    from snowflake.account_usage.query_history 
    where database_name = 'SAMPLE_DATABASE' order by start_time;
    
    -- results
    +------------------------------------------+-----------------+-------------+------------------------+
    |                QUERY_TEXT                |  DATABASE_NAME  | SCHEMA_NAME |       QUERY_TYPE       |
    +------------------------------------------+-----------------+-------------+------------------------+
    | create or replace table sample_databa... | SAMPLE_DATABASE | PUBLIC      | CREATE_TABLE           |
    | alter table sample_database.public.te... | SAMPLE_DATABASE | PUBLIC      | ALTER_TABLE_ADD_COLUMN |
    | insert overwrite into sample_database... | SAMPLE_DATABASE | PUBLIC      | INSERT                 |
    | select * from snowflake.account_usage... | SAMPLE_DATABASE | PUBLIC      | SELECT                 |
    +------------------------------------------+-----------------+-------------+------------------------+
    

    请注意,account_usage 会查看 have a slight delay,因此您的查询可能不会立即显示。尤其是 query_history 视图,根据我刚刚链接到的文档,延迟大约为 45 分钟。

    另一种方法是使用QUERY_HISTORY, QUERY_HISTORY_BY_* 表函数,据我所知,它不会延迟。这是一个例子:

    -- Run some sample queries
    create or replace database sample_database;
    create or replace table sample_database.public.test_table (col1 varchar);
    alter table sample_database.public.test_table add column col2 varchar;
    insert overwrite into sample_database.public.test_table values ('row 1 col 1', 'row 1 col 2'), ('row 2 col 1', 'row 2 col 2');
    
    -- Use the table functions to see the history and view the query types run in the last hour
    select 
        query_text, 
        database_name, 
        schema_name, 
        query_type
    from table(information_schema.query_history(dateadd('hours',-1,current_timestamp()),current_timestamp()))
    where database_name = 'SAMPLE_DATABASE'
    order by start_time;
    
    -- results
    +------------------------------------------+-----------------+-------------+------------------------+
    |                QUERY_TEXT                |  DATABASE_NAME  | SCHEMA_NAME |       QUERY_TYPE       |
    +------------------------------------------+-----------------+-------------+------------------------+
    | create or replace table sample_databa... | SAMPLE_DATABASE | PUBLIC      | CREATE_TABLE           |
    | alter table sample_database.public.te... | SAMPLE_DATABASE | PUBLIC      | ALTER_TABLE_ADD_COLUMN |
    | insert overwrite into sample_database... | SAMPLE_DATABASE | PUBLIC      | INSERT                 |
    | select * from snowflake.account_usage... | SAMPLE_DATABASE | PUBLIC      | SELECT                 |
    +------------------------------------------+-----------------+-------------+------------------------+
    

    【讨论】:

    • 要仅执行 DDL 更改语句,您可以使用类似 QUERY_TYPE in ('ALTER_TABLE', 'CREATE_TABLE'....在该列上执行 DISTINCT。如果您已经使用 Snowflake 一段时间,大多数或所有 DML 类型都应该在不同的列表中。
    • 谢谢西蒙和格雷格
    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 2020-09-28
    • 2021-12-10
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多