Skip to content

Instantly share code, notes, and snippets.

@kr153
Last active February 7, 2022 19:54
Show Gist options
  • Select an option

  • Save kr153/5946818 to your computer and use it in GitHub Desktop.

Select an option

Save kr153/5946818 to your computer and use it in GitHub Desktop.
IBM DB2 RunStat and ReOrg
--ReOrg and RunStat for IBM DB2
--All Programs > IBM DB2 > DB2COPY1 (Default) > Command Line Tools > Command Window - Administrator
-- Connect to Database
db2 connect to <DATABASE NAME>
-- Generate output for RunStat on Tables
db2 -x "SELECT 'RUNSTATS ON TABLE ' || TRIM(TABSCHEMA) || '.' || TRIM(TABNAME) || ' AND INDEXES ALL;' FROM SYSCAT.TABLES WHERE TYPE = 'T' AND TABSCHEMA NOT LIKE 'SYS%' ORDER BY TABSCHEMA, TABNAME" > db2_runstats.sql.out
-- Use the genereated file for RunStat on Tables.
db2 -tvf db2_runstats.sql.out
-- Generate output for ReOrg on Tables
db2 -x "SELECT 'REORG TABLE ' || TRIM(TABSCHEMA) || '.' || TRIM(TABNAME) || ';' FROM SYSCAT.TABLES WHERE TYPE = 'T' AND TABSCHEMA NOT LIKE 'SYS%' ORDER BY TABSCHEMA, TABNAME" > db2_reorg.sql.out
-- Use the genereated file for Runstat
db2 -tvf db2_reorg.sql.out
-- Use the genereated file for Runstat again.
db2 -tvf db2_runstats.sql.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment