有没有办法在 redshift 中找到 table 创建日期?
Is there any way to find table creation date in redshift?
我在 Amazon Redshift 中查找 table 创建日期时遇到问题。
我知道 svv_table_info 会提供有关 table 的所有信息,但创作 date.Can 有人帮忙吗?
似乎无法在 Redshift 中获取 tables 的创建时间戳。一种解决方法是使用 STL_DDLTEXT table,它记录了包括 CREATE TABLE
在内的 DDL 历史记录。
这是一个例子(test_table
是一个 table 名字):
dev=> select starttime, endtime, trim(text) as ddl from stl_ddltext where text ilike '%create%table%test_table%' order by endtime desc limit 1;
starttime | endtime | ddl
----------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------
2016-04-25 05:38:11.666338 | 2016-04-25 05:38:11.674947 | CREATE TABLE "test_table" (id int primary key, value varchar(24));
(1 row)
在上述情况下,starttime
或 endtime
将是 test_table
table 创建的时间戳。
注意:
- Redshift 不会长期保留STL_DDLTEXT,所以你不能永久使用这种方式。
- 如果 table 是通过重命名 table 名称等其他方式创建的,则不能使用这种方式。
在 Redshift 中,您可以通过搜索任何创建 table sql 运行 的开始和停止时间来获取 table 的创建时间svl_qlog。您可以查看其他 tables 以获得类似的数据,但这种方式的问题是它只保留了几天 (3 - 5)。虽然每个人都希望元数据与 table 本身一起存储以供查询。亚马逊建议保留此数据以将数据从要保留到 S3 的日志中导出到 S3。然后在我看来,您可以将这些 s3 文件导入回您想要称为 aws_table_history 的永久 table 或其他名称,以便您永远保留这些特殊数据。
select * from svl_qlog where substring ilike 'create table%' order by starttime desc limit 100;
select * from stl_query a, stl_querytext b where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc limit 100;
或者只获取 Table 名称和日期,如下所示:
select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename,
starttime as createdate
from stl_query a, stl_querytext b
where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc;
使用您的密钥将您想要创建的 Table 数据历史记录导出到您创建的 S3 存储桶。下面的 select 语句将输出创建的 table 名称和创建的日期时间。
使用要导出到 S3 的数据创建临时 table。
create table temp_history as
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate
from stl_query a, stl_querytext b
where a.query = b.query
and b.text ilike 'create table%' order by a.starttime desc);
然后将此 table 上传到 S3。
unload ('select * from temp_history')
to 's3://tablehistory' credentials 'aws_access_key_id=myaccesskey;aws_secret_access_key=mysecretkey'
DELIMITER '|' NULL AS '' ESCAPE ALLOWOVERWRITE;
在 AWS Redshift 中创建一个新的 table。
CREATE TABLE aws_table_history
(
tablename VARCHAR(150),
createdate DATETIME
);
然后将其导入回您的自定义 table。
copy aws_table_history from 's3://tablehistory' credentials 'aws_access_key_id=MYKEY;aws_secret_access_key=MYID'
emptyasnull
blanksasnull
removequotes
escape
dateformat 'YYYY-MM-DD'
timeformat 'YYYY-MM-DD HH:MI:SS'
maxerror 20;
delimiter '|';
我测试了所有这些,它对我们有用。我希望这可以帮助一些人。
最后一个更简单的方法是使用 Talend Big Data Open Studio 并创建一个新作业,获取组件 tRedshiftRow 并将以下 SQL 粘贴到其中。然后构建作业,您可以在任何您想要的环境中安排 运行 .bat (windows) 或 .sh (unix)。
INSERT INTO temp_history
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate
from stl_query a, stl_querytext b
where a.query = b.query
and b.text ilike 'create table%' order by a.starttime desc);
COMMIT;
insert into historytable
select distinct s.*
from temp_history s;
COMMIT;
--remove duplicates
DELETE FROM historytable USING historytable a2
WHERE historytable.tablename = a2.tablename AND
historytable.createdate < a2.createdate;
COMMIT;
---clear everything from prestage
TRUNCATE temp_history;
COMMIT;
在 Redshift 中有一种获取 table 创建日期和时间的正确方法,它不是基于查询日志:
SELECT
TRIM(nspname) AS schema_name,
TRIM(relname) AS table_name,
relcreationtime AS creation_time
FROM pg_class_info
LEFT JOIN pg_namespace ON pg_class_info.relnamespace = pg_namespace.oid
WHERE reltype != 0
AND TRIM(nspname) = 'my_schema';
出于某种原因,它不适用于非常旧的 tables。我能在我的集群上找到的最旧日期是 2018 年 11 月。也许 tables 的创建日期在此日期之前未记录在 pg_class_info
中。
我在 Amazon Redshift 中查找 table 创建日期时遇到问题。 我知道 svv_table_info 会提供有关 table 的所有信息,但创作 date.Can 有人帮忙吗?
似乎无法在 Redshift 中获取 tables 的创建时间戳。一种解决方法是使用 STL_DDLTEXT table,它记录了包括 CREATE TABLE
在内的 DDL 历史记录。
这是一个例子(test_table
是一个 table 名字):
dev=> select starttime, endtime, trim(text) as ddl from stl_ddltext where text ilike '%create%table%test_table%' order by endtime desc limit 1;
starttime | endtime | ddl
----------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------
2016-04-25 05:38:11.666338 | 2016-04-25 05:38:11.674947 | CREATE TABLE "test_table" (id int primary key, value varchar(24));
(1 row)
在上述情况下,starttime
或 endtime
将是 test_table
table 创建的时间戳。
注意:
- Redshift 不会长期保留STL_DDLTEXT,所以你不能永久使用这种方式。
- 如果 table 是通过重命名 table 名称等其他方式创建的,则不能使用这种方式。
在 Redshift 中,您可以通过搜索任何创建 table sql 运行 的开始和停止时间来获取 table 的创建时间svl_qlog。您可以查看其他 tables 以获得类似的数据,但这种方式的问题是它只保留了几天 (3 - 5)。虽然每个人都希望元数据与 table 本身一起存储以供查询。亚马逊建议保留此数据以将数据从要保留到 S3 的日志中导出到 S3。然后在我看来,您可以将这些 s3 文件导入回您想要称为 aws_table_history 的永久 table 或其他名称,以便您永远保留这些特殊数据。
select * from svl_qlog where substring ilike 'create table%' order by starttime desc limit 100;
select * from stl_query a, stl_querytext b where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc limit 100;
或者只获取 Table 名称和日期,如下所示:
select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename,
starttime as createdate
from stl_query a, stl_querytext b
where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc;
使用您的密钥将您想要创建的 Table 数据历史记录导出到您创建的 S3 存储桶。下面的 select 语句将输出创建的 table 名称和创建的日期时间。
使用要导出到 S3 的数据创建临时 table。
create table temp_history as
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate
from stl_query a, stl_querytext b
where a.query = b.query
and b.text ilike 'create table%' order by a.starttime desc);
然后将此 table 上传到 S3。
unload ('select * from temp_history')
to 's3://tablehistory' credentials 'aws_access_key_id=myaccesskey;aws_secret_access_key=mysecretkey'
DELIMITER '|' NULL AS '' ESCAPE ALLOWOVERWRITE;
在 AWS Redshift 中创建一个新的 table。
CREATE TABLE aws_table_history
(
tablename VARCHAR(150),
createdate DATETIME
);
然后将其导入回您的自定义 table。
copy aws_table_history from 's3://tablehistory' credentials 'aws_access_key_id=MYKEY;aws_secret_access_key=MYID'
emptyasnull
blanksasnull
removequotes
escape
dateformat 'YYYY-MM-DD'
timeformat 'YYYY-MM-DD HH:MI:SS'
maxerror 20;
delimiter '|';
我测试了所有这些,它对我们有用。我希望这可以帮助一些人。 最后一个更简单的方法是使用 Talend Big Data Open Studio 并创建一个新作业,获取组件 tRedshiftRow 并将以下 SQL 粘贴到其中。然后构建作业,您可以在任何您想要的环境中安排 运行 .bat (windows) 或 .sh (unix)。
INSERT INTO temp_history
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate
from stl_query a, stl_querytext b
where a.query = b.query
and b.text ilike 'create table%' order by a.starttime desc);
COMMIT;
insert into historytable
select distinct s.*
from temp_history s;
COMMIT;
--remove duplicates
DELETE FROM historytable USING historytable a2
WHERE historytable.tablename = a2.tablename AND
historytable.createdate < a2.createdate;
COMMIT;
---clear everything from prestage
TRUNCATE temp_history;
COMMIT;
在 Redshift 中有一种获取 table 创建日期和时间的正确方法,它不是基于查询日志:
SELECT
TRIM(nspname) AS schema_name,
TRIM(relname) AS table_name,
relcreationtime AS creation_time
FROM pg_class_info
LEFT JOIN pg_namespace ON pg_class_info.relnamespace = pg_namespace.oid
WHERE reltype != 0
AND TRIM(nspname) = 'my_schema';
出于某种原因,它不适用于非常旧的 tables。我能在我的集群上找到的最旧日期是 2018 年 11 月。也许 tables 的创建日期在此日期之前未记录在 pg_class_info
中。