Search in sources :

Example 11 with Analyze

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));
}
Also used : Random(java.util.Random) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MultiDimension(org.h2.tools.MultiDimension)

Example 12 with Analyze

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));
}
Also used : Random(java.util.Random) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MultiDimension(org.h2.tools.MultiDimension)

Example 13 with Analyze

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");
    }
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 14 with Analyze

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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 15 with Analyze

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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Aggregations

Statement (java.sql.Statement)13 Connection (java.sql.Connection)12 PreparedStatement (java.sql.PreparedStatement)12 ResultSet (java.sql.ResultSet)11 SimpleResultSet (org.h2.tools.SimpleResultSet)6 Random (java.util.Random)3 JdbcConnection (org.h2.jdbc.JdbcConnection)3 Parameter (org.h2.expression.Parameter)2 Table (org.h2.table.Table)2 MultiDimension (org.h2.tools.MultiDimension)2 ValueString (org.h2.value.ValueString)2 File (java.io.File)1 SQLException (java.sql.SQLException)1 Savepoint (java.sql.Savepoint)1 Prepared (org.h2.command.Prepared)1 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)1 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)1 Analyze (org.h2.command.ddl.Analyze)1 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)1