Tuesday, July 9, 2013

How to delete old partitions from Range partition table

Below qry will give you all the partitions older then certain date. this can be used to drop partitions

SELECT TABSCHEMA,TABNAME,DATAPARTITIONNAME,LOWVALUE,DAYNAME(LOWVALUE) DAY_NAME,DAYOFWEEK(LOWVALUE)DAY_OF_WEEK ,
       TRIM(CHAR(DAY(LOWVALUE)))||'_'||monthname(LOWVALUE) MONTH_ENDS
FROM
(
select TABSCHEMA,TABNAME,DATAPARTITIONNAME, DATE(REPLACE(LOWVALUE,'''','')) AS LOWVALUE from syscat.datapartitions
where tabname ='TABNAME'
AND LOWVALUE NOT IN ('MINVALUE','MAXVALUE')
)DT
WHERE LOWVALUE < DATE(date('2012-08-01') - 90 days)
AND DAYOFWEEK(LOWVALUE) != 6
with ur;

-- Qry to drop parition leaving weekend and monthend.
select
        'db2ts detach table partition -dbname NYPD_LMFR  -schema '||dp.TABSCHEMA||' -name '||TABNAME||' -partition '||DATAPARTITIONNAME||' -cleanup drop -cmrs batch -mailto lmfr_dba'
from
    syscat.datapartitions  dp

where
    dp.HIGHVALUE != 'MAXVALUE'
    AND TABNAME='HUB_PENDING_PART'
    AND  date(REPLACE(HIGHVALUE,'''',''))  < current date -3 months
--Month end
AND (date(REPLACE(HIGHVALUE,'''','')) != date(REPLACE(HIGHVALUE,'''','')) + 1 days - day(date(REPLACE(HIGHVALUE,'''',''))) days
--Weekend
 OR DAYNAME(date(REPLACE(HIGHVALUE,'''','')) )!='Friday')
with ur    ;

No comments:

Post a Comment