Search in sources :

Example 16 with Analyze

use of org.h2.command.ddl.Analyze in project h2database by h2database.

the class TestOptimizations method testMultiColumnRangeQuery.

private void testMultiColumnRangeQuery() throws SQLException {
    deleteDb("optimizations");
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE Logs(id INT PRIMARY KEY, type INT)");
    stat.execute("CREATE unique INDEX type_index ON Logs(type, id)");
    stat.execute("INSERT INTO Logs SELECT X, MOD(X, 3) " + "FROM SYSTEM_RANGE(1, 1000)");
    stat.execute("ANALYZE SAMPLE_SIZE 0");
    ResultSet rs;
    rs = stat.executeQuery("EXPLAIN SELECT id FROM Logs " + "WHERE id < 100 and type=2 AND id<100");
    rs.next();
    String plan = rs.getString(1);
    assertContains(plan, "TYPE_INDEX");
    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 17 with Analyze

use of org.h2.command.ddl.Analyze in project h2database by h2database.

the class TestOptimizations method testUseCoveringIndex.

private void testUseCoveringIndex() throws SQLException {
    deleteDb("optimizations");
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TABLE_A(id IDENTITY PRIMARY KEY NOT NULL, " + "name VARCHAR NOT NULL, active BOOLEAN DEFAULT TRUE, " + "UNIQUE KEY TABLE_A_UK (name) )");
    stat.execute("CREATE TABLE TABLE_B(id IDENTITY PRIMARY KEY NOT NULL,  " + "TABLE_a_id BIGINT NOT NULL,  createDate TIMESTAMP DEFAULT NOW(), " + "UNIQUE KEY TABLE_B_UK (table_a_id, createDate), " + "FOREIGN KEY (table_a_id) REFERENCES TABLE_A(id) )");
    stat.execute("INSERT INTO TABLE_A (name)  SELECT 'package_' || CAST(X as VARCHAR) " + "FROM SYSTEM_RANGE(1, 100)  WHERE X <= 100");
    stat.execute("INSERT INTO TABLE_B (table_a_id, createDate)  SELECT " + "CASE WHEN table_a_id = 0 THEN 1 ELSE table_a_id END, createDate " + "FROM ( SELECT ROUND((RAND() * 100)) AS table_a_id, " + "DATEADD('SECOND', X, NOW()) as createDate FROM SYSTEM_RANGE(1, 50000) " + "WHERE X < 50000  )");
    stat.execute("CREATE INDEX table_b_idx ON table_b(table_a_id, id)");
    stat.execute("ANALYZE");
    ResultSet rs = stat.executeQuery("EXPLAIN ANALYZE SELECT MAX(b.id) as id " + "FROM table_b b JOIN table_a a ON b.table_a_id = a.id GROUP BY b.table_a_id " + "HAVING A.ACTIVE = TRUE");
    rs.next();
    assertContains(rs.getString(1), "/* PUBLIC.TABLE_B_IDX: TABLE_A_ID = A.ID */");
    rs = stat.executeQuery("EXPLAIN ANALYZE SELECT MAX(id) FROM table_b GROUP BY table_a_id");
    rs.next();
    assertContains(rs.getString(1), "/* PUBLIC.TABLE_B_IDX");
    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 18 with Analyze

use of org.h2.command.ddl.Analyze in project h2database by h2database.

the class TestOptimizations method testDistinctOptimization.

private void testDistinctOptimization() throws SQLException {
    deleteDb("optimizations");
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, " + "NAME VARCHAR, TYPE INT)");
    stat.execute("CREATE INDEX IDX_TEST_TYPE ON TEST(TYPE)");
    Random random = new Random(1);
    int len = getSize(10000, 100000);
    int[] groupCount = new int[10];
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?, ?)");
    for (int i = 0; i < len; i++) {
        prep.setInt(1, i);
        prep.setString(2, "Hello World");
        int type = random.nextInt(10);
        groupCount[type]++;
        prep.setInt(3, type);
        prep.execute();
    }
    ResultSet rs;
    rs = stat.executeQuery("SELECT TYPE, COUNT(*) FROM TEST " + "GROUP BY TYPE ORDER BY TYPE");
    for (int i = 0; rs.next(); i++) {
        assertEquals(i, rs.getInt(1));
        assertEquals(groupCount[i], rs.getInt(2));
    }
    assertFalse(rs.next());
    rs = stat.executeQuery("SELECT DISTINCT TYPE FROM TEST " + "ORDER BY TYPE");
    for (int i = 0; rs.next(); i++) {
        assertEquals(i, rs.getInt(1));
    }
    assertFalse(rs.next());
    stat.execute("ANALYZE");
    rs = stat.executeQuery("SELECT DISTINCT TYPE FROM TEST " + "ORDER BY TYPE");
    for (int i = 0; i < 10; i++) {
        assertTrue(rs.next());
        assertEquals(i, rs.getInt(1));
    }
    assertFalse(rs.next());
    rs = stat.executeQuery("SELECT DISTINCT TYPE FROM TEST " + "ORDER BY TYPE LIMIT 5 OFFSET 2");
    for (int i = 2; i < 7; i++) {
        assertTrue(rs.next());
        assertEquals(i, rs.getInt(1));
    }
    assertFalse(rs.next());
    rs = stat.executeQuery("SELECT DISTINCT TYPE FROM TEST " + "ORDER BY TYPE LIMIT -1 OFFSET 0 SAMPLE_SIZE 3");
    // must have at least one row
    assertTrue(rs.next());
    for (int i = 0; i < 3; i++) {
        rs.getInt(1);
        if (i > 0 && !rs.next()) {
            break;
        }
    }
    assertFalse(rs.next());
    conn.close();
}
Also used : Random(java.util.Random) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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