Search in sources :

Example 11 with Explain

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

the class TestOptimizations method testSortIndex.

private void testSortIndex() throws SQLException {
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("drop table test if exists");
    stat.execute("create table test(id int)");
    stat.execute("create index idx_id_desc on test(id desc)");
    stat.execute("create index idx_id_asc on test(id)");
    ResultSet rs;
    rs = stat.executeQuery("explain select * from test " + "where id > 10 order by id");
    rs.next();
    assertContains(rs.getString(1), "IDX_ID_ASC");
    rs = stat.executeQuery("explain select * from test " + "where id < 10 order by id desc");
    rs.next();
    assertContains(rs.getString(1), "IDX_ID_DESC");
    rs.next();
    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 12 with Explain

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

the class TestOptimizations method testConvertOrToIn.

private void testConvertOrToIn() throws SQLException {
    deleteDb("optimizations");
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar(255))");
    stat.execute("insert into test values" + "(1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5')");
    ResultSet rs = stat.executeQuery("EXPLAIN PLAN FOR SELECT * " + "FROM test WHERE ID=1 OR ID=2 OR ID=3 OR ID=4 OR ID=5");
    rs.next();
    assertContains(rs.getString(1), "ID IN(1, 2, 3, 4, 5)");
    rs = stat.executeQuery("SELECT COUNT(*) FROM test " + "WHERE ID=1 OR ID=2 OR ID=3 OR ID=4 OR ID=5");
    rs.next();
    assertEquals(5, rs.getInt(1));
    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 13 with Explain

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

the class TestOptimizations method testLike.

private void testLike() throws Exception {
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("create table test(name varchar primary key) as " + "select x from system_range(1, 10)");
    ResultSet rs = stat.executeQuery("explain select * from test " + "where name like ? || '%' {1: 'Hello'}");
    rs.next();
    // ensure the ID = 10 part is evaluated first
    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 14 with Explain

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

the class TestOptimizations method testOptimizeInJoin.

private void testOptimizeInJoin() throws SQLException {
    deleteDb("optimizations");
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key)");
    stat.execute("insert into test select x from system_range(1, 1000)");
    ResultSet rs = stat.executeQuery("explain select * " + "from test where id in (400, 300)");
    rs.next();
    String plan = rs.getString(1);
    if (plan.indexOf("/* PUBLIC.PRIMARY_KEY_") < 0) {
        fail("Expected using the primary key, got: " + plan);
    }
    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 Explain

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

the class TestOptimizations method testExistsSubquery.

private void testExistsSubquery() throws Exception {
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int) as select x from system_range(1, 10)");
    ResultSet rs = stat.executeQuery("explain select * from test " + "where exists(select 1 from test, test, test) and id = 10");
    rs.next();
    // ensure the ID = 10 part is evaluated first
    assertContains(rs.getString(1), "WHERE (ID = 10)");
    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)

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