use of org.h2.command.ddl.Analyze in project h2database by h2database.
the class TestMultiDimension method testPerformance2d.
private void testPerformance2d() throws SQLException {
deleteDb("multiDimension");
Connection conn;
conn = getConnection("multiDimension");
Statement stat = conn.createStatement();
stat.execute("CREATE ALIAS MAP FOR \"" + getClass().getName() + ".interleave\"");
stat.execute("CREATE TABLE TEST(X INT NOT NULL, Y INT NOT NULL, " + "XY BIGINT AS MAP(X, Y), DATA VARCHAR)");
stat.execute("CREATE INDEX IDX_X ON TEST(X, Y)");
stat.execute("CREATE INDEX IDX_XY ON TEST(XY)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(X, Y, DATA) VALUES(?, ?, ?)");
// the MultiDimension tool is faster for 4225 (65^2) points
// the more the bigger the difference
int max = getSize(30, 65);
long time = System.nanoTime();
for (int x = 0; x < max; x++) {
for (int y = 0; y < max; y++) {
long t2 = System.nanoTime();
if (t2 - time > TimeUnit.SECONDS.toNanos(1)) {
int percent = (int) (100.0 * ((double) x * max + y) / ((double) max * max));
trace(percent + "%");
time = t2;
}
prep.setInt(1, x);
prep.setInt(2, y);
prep.setString(3, "Test data");
prep.execute();
}
}
stat.execute("ANALYZE SAMPLE_SIZE 10000");
PreparedStatement prepRegular = conn.prepareStatement("SELECT * FROM TEST WHERE X BETWEEN ? AND ? " + "AND Y BETWEEN ? AND ? ORDER BY X, Y");
MultiDimension multi = MultiDimension.getInstance();
String sql = multi.generatePreparedQuery("TEST", "XY", new String[] { "X", "Y" });
sql += " ORDER BY X, Y";
PreparedStatement prepMulti = conn.prepareStatement(sql);
long timeMulti = 0, timeRegular = 0;
int timeMax = getSize(500, 2000);
Random rand = new Random(1);
while (timeMulti < timeMax) {
int size = rand.nextInt(max / 10);
int minX = rand.nextInt(max - size);
int minY = rand.nextInt(max - size);
int maxX = minX + size, maxY = minY + size;
time = System.nanoTime();
ResultSet rs1 = multi.getResult(prepMulti, new int[] { minX, minY }, new int[] { maxX, maxY });
timeMulti += System.nanoTime() - time;
time = System.nanoTime();
prepRegular.setInt(1, minX);
prepRegular.setInt(2, maxX);
prepRegular.setInt(3, minY);
prepRegular.setInt(4, maxY);
ResultSet rs2 = prepRegular.executeQuery();
timeRegular += System.nanoTime() - time;
while (rs1.next()) {
assertTrue(rs2.next());
assertEquals(rs1.getInt(1), rs2.getInt(1));
assertEquals(rs1.getInt(2), rs2.getInt(2));
}
assertFalse(rs2.next());
}
conn.close();
deleteDb("multiDimension");
trace("2d: regular: " + TimeUnit.NANOSECONDS.toMillis(timeRegular) + " MultiDimension: " + TimeUnit.NANOSECONDS.toMillis(timeMulti));
}
use of org.h2.command.ddl.Analyze in project h2database by h2database.
the class TestMultiDimension method testPerformance3d.
private void testPerformance3d() throws SQLException {
deleteDb("multiDimension");
Connection conn;
conn = getConnection("multiDimension");
Statement stat = conn.createStatement();
stat.execute("CREATE ALIAS MAP FOR \"" + getClass().getName() + ".interleave\"");
stat.execute("CREATE TABLE TEST(X INT NOT NULL, " + "Y INT NOT NULL, Z INT NOT NULL, " + "XYZ BIGINT AS MAP(X, Y, Z), DATA VARCHAR)");
stat.execute("CREATE INDEX IDX_X ON TEST(X, Y, Z)");
stat.execute("CREATE INDEX IDX_XYZ ON TEST(XYZ)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(X, Y, Z, DATA) VALUES(?, ?, ?, ?)");
// the MultiDimension tool is faster for 8000 (20^3) points
// the more the bigger the difference
int max = getSize(10, 20);
long time = System.nanoTime();
for (int x = 0; x < max; x++) {
for (int y = 0; y < max; y++) {
for (int z = 0; z < max; z++) {
long t2 = System.nanoTime();
if (t2 - time > TimeUnit.SECONDS.toNanos(1)) {
int percent = (int) (100.0 * ((double) x * max + y) / ((double) max * max));
trace(percent + "%");
time = t2;
}
prep.setInt(1, x);
prep.setInt(2, y);
prep.setInt(3, z);
prep.setString(4, "Test data");
prep.execute();
}
}
}
stat.execute("ANALYZE SAMPLE_SIZE 10000");
PreparedStatement prepRegular = conn.prepareStatement("SELECT * FROM TEST WHERE X BETWEEN ? AND ? " + "AND Y BETWEEN ? AND ? AND Z BETWEEN ? AND ? ORDER BY X, Y, Z");
MultiDimension multi = MultiDimension.getInstance();
String sql = multi.generatePreparedQuery("TEST", "XYZ", new String[] { "X", "Y", "Z" });
sql += " ORDER BY X, Y, Z";
PreparedStatement prepMulti = conn.prepareStatement(sql);
long timeMulti = 0, timeRegular = 0;
int timeMax = getSize(500, 2000);
Random rand = new Random(1);
while (timeMulti < timeMax) {
int size = rand.nextInt(max / 10);
int minX = rand.nextInt(max - size);
int minY = rand.nextInt(max - size);
int minZ = rand.nextInt(max - size);
int maxX = minX + size, maxY = minY + size, maxZ = minZ + size;
time = System.nanoTime();
ResultSet rs1 = multi.getResult(prepMulti, new int[] { minX, minY, minZ }, new int[] { maxX, maxY, maxZ });
timeMulti += System.nanoTime() - time;
time = System.nanoTime();
prepRegular.setInt(1, minX);
prepRegular.setInt(2, maxX);
prepRegular.setInt(3, minY);
prepRegular.setInt(4, maxY);
prepRegular.setInt(5, minZ);
prepRegular.setInt(6, maxZ);
ResultSet rs2 = prepRegular.executeQuery();
timeRegular += System.nanoTime() - time;
while (rs1.next()) {
assertTrue(rs2.next());
assertEquals(rs1.getInt(1), rs2.getInt(1));
assertEquals(rs1.getInt(2), rs2.getInt(2));
}
assertFalse(rs2.next());
}
conn.close();
deleteDb("multiDimension");
trace("3d: regular: " + TimeUnit.NANOSECONDS.toMillis(timeRegular) + " MultiDimension: " + TimeUnit.NANOSECONDS.toMillis(timeMulti));
}
use of org.h2.command.ddl.Analyze in project h2database by h2database.
the class TestMultiThread method testConcurrentAnalyze.
private void testConcurrentAnalyze() throws Exception {
if (config.mvcc) {
return;
}
deleteDb(getTestName());
final String url = getURL("concurrentAnalyze;MULTI_THREADED=1", true);
try (Connection conn = getConnection(url)) {
Statement stat = conn.createStatement();
stat.execute("create table test(id bigint primary key) " + "as select x from system_range(1, 1000)");
Task t = new Task() {
@Override
public void call() throws SQLException {
try (Connection conn2 = getConnection(url)) {
for (int i = 0; i < 1000; i++) {
conn2.createStatement().execute("analyze");
}
}
}
};
t.execute();
Thread.yield();
for (int i = 0; i < 1000; i++) {
conn.createStatement().execute("analyze");
}
t.get();
stat.execute("drop table test");
}
}
use of org.h2.command.ddl.Analyze in project h2database by h2database.
the class TestOptimizations method testFastRowIdCondition.
private void testFastRowIdCondition() throws Exception {
Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
stat.executeUpdate("create table many(id int) " + "as select x from system_range(1, 10000)");
ResultSet rs = stat.executeQuery("explain analyze select * from many " + "where _rowid_ = 400");
rs.next();
assertContains(rs.getString(1), "/* scanCount: 2 */");
conn.close();
}
use of org.h2.command.ddl.Analyze in project h2database by h2database.
the class TestOptimizations method testUseIndexWhenAllColumnsNotInOrderBy.
private void testUseIndexWhenAllColumnsNotInOrderBy() throws SQLException {
deleteDb("optimizations");
Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
stat.execute("create table test(id int primary key, account int, tx int)");
stat.execute("insert into test select x, x*100, x from system_range(1, 10000)");
stat.execute("analyze sample_size 5");
stat.execute("create unique index idx_test_account_tx on test(account, tx desc)");
ResultSet rs;
rs = stat.executeQuery("explain analyze " + "select tx from test " + "where account=22 and tx<9999999 " + "order by tx desc limit 25");
rs.next();
String plan = rs.getString(1);
assertContains(plan, "index sorted");
conn.close();
}
Aggregations