如何删除雪花中创建的临时表

时间:2020-01-23 16:14:55

标签: database snowflake-cloud-data-platform

我正在通过ODI将数据加载到用c $创建的雪花临时表中,加载成功后需要删除,如何删除这些临时表会引起您的建议

2 个答案:

答案 0 :(得分:2)

如果您仍然需要这样做,我编写了一个存储过程,该过程将获取动态生成的SQL列表,并一次执行一行。您可以使用它来运行任何由选择查询生成的SQL语句的列表,包括删除所有与模式(例如c $%)匹配的表。首先,这是存储过程:

create or replace procedure RunBatchSQL(sqlCommand String)
returns string
language JavaScript
as
$$
/**
 * Stored procedure to execute multiple SQL statements generated from a SQL query
 * Note that this procedure will always use the column named "SQL_COMMAND"
 *
 * @param {String} sqlCommand: The SQL query to run to generate one or more SQL commands 
 * @return {String}: A string containing all the SQL commands executed, each separated by a newline. 
 */
      cmd1_dict = {sqlText: SQLCOMMAND};
      stmt = snowflake.createStatement(cmd1_dict);

      rs = stmt.execute();

      var s = '';

      while (rs.next())  {
          cmd2_dict = {sqlText: rs.getColumnValue("SQL_COMMAND")};
          stmtEx = snowflake.createStatement(cmd2_dict);
          stmtEx.execute();
          s += rs.getColumnValue(1) + "\n";
          }

      return s;

$$

您可以使用此存储过程,使用以下脚本批量运行任何动态生成的SQL语句。运行最上面的查询,显然可以执行以该查询测试作为参数的存储过程:

-- This is a select query that will generate a list of SQL commands to execute in batch. 
-- This SQL will generate rows to drop all tables starting with c$. With minor edits
-- you could limit it to a specific database or schema.
select 'drop table ' || TABLE_CATALOG || '.' || TABLE_SCHEMA || '.' || "TABLE_NAME" as SQL_COMMAND
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like 'c$%';

-- As a convenience, this grabs the last SQL run so that it's easier to insert into 
-- the parameter used to call the stored procedure. 
set query_text = (  select QUERY_TEXT
                    from table(information_schema.query_history(result_limit => 2))
                    where SESSION_ID = Current_Session() and QUERY_TYPE = 'SELECT' order by START_TIME desc);

-- Confirm that the query_text variable has the correct SQL query to generate our SQL commands (grants in this case) to run.
select $query_text;

-- Run the stored procedure. Note that to view its output better, double click on the output to see it in multi-line format,
Call RunBatchSQL($query_text);

--Check the last several queries run to make sure it worked.
select QUERY_TEXT
from table(information_schema.query_history(result_limit => 100))
where SESSION_ID = Current_Session() order by START_TIME desc;

答案 1 :(得分:1)

带有前缀C$的工作表是ODI使用的产物,但是它们并不是作为实际的Snowflake temporary tables创建的,因此它们不能从JDBC会话终止时自动删除中受益。

关于C$I$工作表的ODI publishers note this

方案成功完成后,它将自动删除这些表,因为它们是暂时的,不再需要。但是,在方案未成功完成的情况下,这些表有可能被抛在后面,因此有时可能需要清理这些表以回收空间。

对于不成功的方案,在使用ODI时很可能导致雪花上的剩余表,遵循上面的链接应该可以帮助您运行一个删除剩余工作表的过程(手动或按计划) )。为了方便起见,在此处复制相关过程:

要运行该过程:

  • 打开ODI Studio并连接到BI Apps ODI存储库。
  • 导航到“设计器”选项卡,然后使用导航器导航到BI Apps Project -> Components -> DW -> Oracle -> Clean Work and Flow Tables文件夹
  • 在文件夹中找到“清理工作和流量表”程序包,该程序包中是UTILITIES_CLEAN_WORK_AND_FLOW_TABLES场景。
  • 右键单击该方案,然后选择“执行”选项。在提示符下,提供所需的天数,以便在删除表格之前返回