Retrieve all Indexes for a given Table with JDBC

前端 未结 4 1339
臣服心动
臣服心动 2021-01-19 21:29

I want to write a SpringBatch Tasklet, that automatically activates or de-activates all indexes for a given database table. The code needs to work independantly of the DBMS

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 21:37

    You have to make a difference between primary keys (using DatabaseMetaData.getPrimaryKeys() to retrieve) and other indexes (via dbMetaData.getIndexInfo(null, null, tableName, true, false)).
    In your loop use:

    • rs.getString("INDEX_NAME") to extract index name
    • rs.getBoolean("NON_UNIQUE") to extract unique information
    • rs.getShort("TYPE") to extract index type
    • rs.getInt("ORDINAL_POSITION") to extract ordinal position

    Use ORDINAL_POSITION as key break (when current value is <= of previous one) to detect index change.
    Read official DatabaseMetaData.getIndexInfo() doc

提交回复
热议问题