Search in sources :

Example 26 with Explain

use of org.h2.command.dml.Explain in project h2database by h2database.

the class TestMVTableEngine method testExplainAnalyze.

private void testExplainAnalyze() throws Exception {
    if (config.memory) {
        return;
    }
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id identity, name varchar) as " + "select x, space(1000) from system_range(1, 1000)");
    ResultSet rs;
    conn.close();
    conn = getConnection(url);
    stat = conn.createStatement();
    rs = stat.executeQuery("explain analyze select * from test");
    rs.next();
    String plan = rs.getString(1);
    // expect about 1000 reads
    String readCount = plan.substring(plan.indexOf("reads: "));
    readCount = readCount.substring("reads: ".length(), readCount.indexOf('\n'));
    int rc = Integer.parseInt(readCount);
    assertTrue(plan, rc >= 1000 && rc <= 1200);
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint)

Example 27 with Explain

use of org.h2.command.dml.Explain in project h2database by h2database.

the class TestMVTableEngine method testCount.

private void testCount() throws Exception {
    if (config.memory) {
        return;
    }
    Connection conn;
    Connection conn2;
    Statement stat;
    Statement stat2;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE;MVCC=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int)");
    stat.execute("create table test2(id int)");
    stat.execute("insert into test select x from system_range(1, 10000)");
    conn.close();
    ResultSet rs;
    String plan;
    conn2 = getConnection(url);
    stat2 = conn2.createStatement();
    rs = stat2.executeQuery("explain analyze select count(*) from test");
    rs.next();
    plan = rs.getString(1);
    assertTrue(plan, plan.indexOf("reads:") < 0);
    conn = getConnection(url);
    stat = conn.createStatement();
    conn.setAutoCommit(false);
    stat.execute("insert into test select x from system_range(1, 1000)");
    rs = stat.executeQuery("select count(*) from test");
    rs.next();
    assertEquals(11000, rs.getInt(1));
    // not yet committed
    rs = stat2.executeQuery("explain analyze select count(*) from test");
    rs.next();
    plan = rs.getString(1);
    // transaction log is small, so no need to read the table
    assertTrue(plan, plan.indexOf("reads:") < 0);
    rs = stat2.executeQuery("select count(*) from test");
    rs.next();
    assertEquals(10000, rs.getInt(1));
    stat.execute("insert into test2 select x from system_range(1, 11000)");
    rs = stat2.executeQuery("explain analyze select count(*) from test");
    rs.next();
    plan = rs.getString(1);
    // transaction log is larger than the table, so read the table
    assertContains(plan, "reads:");
    rs = stat2.executeQuery("select count(*) from test");
    rs.next();
    assertEquals(10000, rs.getInt(1));
    conn2.close();
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 28 with Explain

use of org.h2.command.dml.Explain 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 29 with Explain

use of org.h2.command.dml.Explain in project h2database by h2database.

the class TestOptimizations method testIdentityIndexUsage.

private void testIdentityIndexUsage() throws Exception {
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("create table test(a identity)");
    stat.execute("insert into test values()");
    ResultSet rs = stat.executeQuery("explain select * from test where a = 1");
    rs.next();
    assertContains(rs.getString(1), "PRIMARY_KEY");
    stat.execute("drop table test");
    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 30 with Explain

use of org.h2.command.dml.Explain 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

ResultSet (java.sql.ResultSet)26 Statement (java.sql.Statement)24 Connection (java.sql.Connection)23 PreparedStatement (java.sql.PreparedStatement)21 SimpleResultSet (org.h2.tools.SimpleResultSet)21 StatementBuilder (org.h2.util.StatementBuilder)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CacheException (javax.cache.CacheException)3 GridCacheSqlQuery (org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)3 GridCacheTwoStepQuery (org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery)3 Query (org.h2.command.dml.Query)3 ValueString (org.h2.value.ValueString)3 Collections.singletonList (java.util.Collections.singletonList)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 IgniteException (org.apache.ignite.IgniteException)2 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 GridIntList (org.apache.ignite.internal.util.GridIntList)2