Add indexes to the H2 cache databases

Many owners of large Gerrit sites complain about the H2 performance but
maybe it is also the lack of indexes which contributes to the bad
performance. Looking at the SELECT statements we need indexes on
(version, k) fields and also on the (accessed) field. Without these
indexes, the database will likely have to perform full table scans which
can be quite expensive for large site with millions of changes.

Release-Notes: Add indexes to the H2 cache databases
Change-Id: I6d67878b81d46cd4b342ba9480e31d3af4b5ce40
diff --git a/java/com/google/gerrit/server/cache/h2/H2CacheImpl.java b/java/com/google/gerrit/server/cache/h2/H2CacheImpl.java
index ad46483..3a732d1 100644
--- a/java/com/google/gerrit/server/cache/h2/H2CacheImpl.java
+++ b/java/com/google/gerrit/server/cache/h2/H2CacheImpl.java
@@ -761,6 +761,8 @@
             "ALTER TABLE data ADD COLUMN IF NOT EXISTS "
                 + "space BIGINT AS OCTET_LENGTH(k) + OCTET_LENGTH(v)");
         stmt.addBatch("ALTER TABLE data ADD COLUMN IF NOT EXISTS version INT DEFAULT 0 NOT NULL");
+        stmt.addBatch("CREATE INDEX IF NOT EXISTS version_key ON data(version, k)");
+        stmt.addBatch("CREATE INDEX IF NOT EXISTS accessed ON data(accessed)");
         stmt.executeBatch();
       }
     }