Index: ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsPublisher.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsPublisher.java (revision 1175957) +++ ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsPublisher.java (working copy) @@ -269,7 +269,23 @@ String createTable = JDBCStatsUtils.getCreate(""); stmt.executeUpdate(createTable); stmt.close(); + + createIndex(); + } else { + // Check if the index on the timestamp column exists + rs = dbm.getIndexInfo(null, null, JDBCStatsUtils.getStatTableName(), false, true); + boolean idxExists = false; + while (rs.next()) { + if (rs.getString("INDEX_NAME").equals(JDBCStatsUtils.getTsIndexName())) { + idxExists = true; + break; + } + } + if (!idxExists) { + createIndex(); + } } + closeConnection(); } catch (Exception e) { LOG.error("Error during JDBC initialization. ", e); @@ -278,4 +294,11 @@ return true; } + private void createIndex() throws SQLException { + Statement stmt = conn.createStatement(); + stmt.setQueryTimeout(timeout); + stmt.executeUpdate(JDBCStatsUtils.getCreateIndex("")); + stmt.close(); + } + } Index: ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsSetupConstants.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsSetupConstants.java (revision 1175957) +++ ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsSetupConstants.java (working copy) @@ -21,8 +21,10 @@ public static final String PART_STAT_ID_COLUMN_NAME = "ID"; - public static final String PART_STAT_TABLE_NAME = "PARTITION_STATS"; + public static final String PART_STAT_TABLE_NAME = "PARTITION_STATS_V2"; + public static final String PART_STAT_TS_INDEX_NAME = "TS_ASC"; + // supported statistics - column names public static final String PART_STAT_ROW_COUNT_COLUMN_NAME = "ROW_COUNT"; Index: ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsUtils.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsUtils.java (revision 1175957) +++ ql/src/java/org/apache/hadoop/hive/ql/stats/jdbc/JDBCStatsUtils.java (working copy) @@ -76,6 +76,10 @@ return JDBCStatsSetupConstants.PART_STAT_TABLE_NAME; } + public static String getTsIndexName() { + return JDBCStatsSetupConstants.PART_STAT_TS_INDEX_NAME; + } + /** * Returns the column where the statistics for the given type are stored. * @@ -108,11 +112,20 @@ for (int i = 0; i < supportedStats.size(); i++) { create += ", " + getStatColumnName(supportedStats.get(i)) + " BIGINT "; } - create += ")"; + create += ", ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; return create; } /** + * Prepares CREATE INDEX query to create an index on the timestamp + */ + public static String getCreateIndex(String comment) { + String create = "CREATE INDEX /* " + comment + " */ " + JDBCStatsUtils.getTsIndexName() + " ON " + + JDBCStatsUtils.getStatTableName() + " (ts ASC)"; + return create; + } + + /** * Prepares UPDATE statement issued when updating existing statistics */ public static String getUpdate(String comment) { @@ -132,12 +145,19 @@ * Prepares INSERT statement for statistic publishing. */ public static String getInsert(String comment) { - String insert = "INSERT INTO /* " + comment + " */ " + getStatTableName() + " VALUES (?, "; + String columns = JDBCStatsUtils.getIdColumnName() + " , "; + String values = "? , "; + for (int i = 0; i < supportedStats.size(); i++) { - insert += "? , "; + columns += getStatColumnName(supportedStats.get(i)) + " , "; + values += "? , "; } - insert = insert.substring(0, insert.length() - 3); - insert += ")"; + + columns = columns.substring(0, columns.length() - 3); + values = values.substring(0, values.length() - 3); + + String insert = "INSERT INTO /* " + comment + " */ " + getStatTableName() + "(" + + columns + ") VALUES (" + values + ")"; return insert; }