如何使用存储过程在redshift上创建10000个表?

问题描述 投票:1回答:1

我需要使用存储过程在Redshift数据库上创建10000个表。您能否提供样本存储过程?

这里是为雪花创建100000表的存储过程

create or replace procedure tablecheck()
returns string
language javascript
strict
execute as owner
as
$$
var i = 1;
while (i < 1001) {  
  var sql_command = 
   'create table performance.al55260.tab'+i+'(col1 int,col2 int,col3 int,col4 int,col5 int,col6 int,col7 int,col8 int,col9 int,col10 int,col11 int,col12 int,col13 int,col14 int,col15 int,col16 int,col17 int,col18 int,col19 int,col20 int,col21 int,col22 int,col23 int,col24 int,col25 int,col26 int,col27 int,col28 int,col29 int,col30 int,col31 int,col32 int,col33 int,col34 int,col35 int,col36 int,col37 int,col38 int,col39 int,col40 int,col41 int,col42 int,col43 int,col44 int,col45 int,col46 int,col47 int,col48 int,col49 int,col50 int);'
  try {
      snowflake.execute (
          {sqlText: sql_command}
          );
         // Return a success/error indicator.


      }
  catch (err)  {
      return "Failed: " + err;   // Return a success/error indicator.
      }
  i++;
}
return 'yes';
$$;

我正在Redshift上寻找存储的proc或函数来实现相同的目的。

如果您有任何以任何其他编程方式创建数百万个表的想法,那都很好。

stored-procedures amazon-redshift stored-functions
1个回答
1
投票

[请注意,当前每个Redshift集群的最大表数为9,900(对于较小的实例类型)或20,000(对于较大的实例类型)。记录在"Quotas and limits in Amazon Redshift"

这是您对Redshift的存储过程的翻译:

CREATE OR REPLACE PROCEDURE tablecheck( 
      table_count IN  INTEGER
    , return_val  OUT VARCHAR
) 
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
    schema_check BOOLEAN;
    loop_count   INTEGER;
    sql_command  VARCHAR(MAX);
BEGIN
    SELECT NVL(TRUE,FALSE) INTO schema_check 
    FROM pg_namespace 
    WHERE nspowner > 1 
    AND nspname = 'tablecheck';
    IF schema_check IS NULL THEN
        CREATE SCHEMA tablecheck;
    ELSE
        DROP SCHEMA tablecheck CASCADE;
        CREATE SCHEMA tablecheck;
    END IF;
    loop_count := 0;
    WHILE (loop_count < table_count) LOOP
      loop_count := loop_count + 1;
      sql_command := 'CREATE TABLE tablecheck.tbl_' || loop_count
            ||'(col1  int, col2  int, col3  int, col4  int, col5  int,'
            ||' col6  int, col7  int, col8  int, col9  int, col10 int,'
            ||' col11 int, col12 int, col13 int, col14 int, col15 int,'
            ||' col16 int, col17 int, col18 int, col19 int, col20 int,'
            ||' col21 int, col22 int, col23 int, col24 int, col25 int,'
            ||' col26 int, col27 int, col28 int, col29 int, col30 int,'
            ||' col31 int, col32 int, col33 int, col34 int, col35 int,'
            ||' col36 int, col37 int, col38 int, col39 int, col40 int,'
            ||' col41 int, col42 int, col43 int, col44 int, col45 int,'
            ||' col46 int, col47 int, col48 int, col49 int, col50 int);';
      EXECUTE sql_command;
      RAISE INFO 'Create table: %', loop_count;
    END LOOP;
    SELECT 'Complete' INTO return_val;
    DROP SCHEMA tablecheck CASCADE;
END
$$;

您在Redshift中按如下方式调用此存储过程:

BEGIN; CALL tablecheck(100); END;
-- …
-- INFO:  Create table: 99
-- INFO:  Create table: 100
--  return_val
-- ------------
--  Complete
--
-- Time: 720.729 ms

更多信息,请参见"Overview of stored procedures in Amazon Redshift"

© www.soinside.com 2019 - 2024. All rights reserved.