Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 536
Daniel Strong Avatar asked Nov 08 '22 18:11

Daniel Strong


1 Answers

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
like image 73
Joe Harris Avatar answered Nov 15 '22 06:11

Joe Harris