简体   繁体   中英

Redshift Table - Find Last Date of Query on a Table

I am trying to clean up my small database and trying to see when the last time some of the tables were actually queried but cannot seem to find any documentation on how to do this. I can get the listing of all of the tables in my schema and the sizes, but cannot identify what might be stale before polling my users.

Does anyone know of a way to get the last date that a table was used/queried in redshift?

select
    schema,  
    "table",
     size as GB
from svv_table_info
    where schema = 'measure' or schema = 'mphd' or schema = 'offer'
order by schema asc;

You can see when the table was last scanned in stl_scan . Almost all select queries will scan. The following is taken from: https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_extended_table_info.sql As you've noted the history is only held for a limited period.

SELECT tbl,
       MAX(endtime) last_scan,
       Nvl(COUNT(DISTINCT query || LPAD(segment,3,'0')),0) num_scans
FROM stl_scan s
WHERE s.userid > 1
AND   s.tbl IN (SELECT oid FROM tbl_ids)
GROUP BY tbl

You will quite possible have to parse entries in STL_QUERYTEXT , which stores SQL queries.

It might be easier to parse STL_EXPLAIN .

Both of these tables can be joined back to STL_QUERY to obtain a time when the query was executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM