Search in sources :

Example 6 with Analyze

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

the class Parser method parseAnalyze.

private Prepared parseAnalyze() {
    Analyze command = new Analyze(session);
    if (readIf("TABLE")) {
        Table table = readTableOrView();
        command.setTable(table);
    }
    if (readIf("SAMPLE_SIZE")) {
        command.setTop(readPositiveInt());
    }
    return command;
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) Analyze(org.h2.command.ddl.Analyze)

Example 7 with Analyze

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

the class Parser method parseExplain.

private Explain parseExplain() {
    Explain command = new Explain(session);
    if (readIf("ANALYZE")) {
        command.setExecuteCommand(true);
    } else {
        if (readIf("PLAN")) {
            readIf("FOR");
        }
    }
    if (isToken("SELECT") || isToken("FROM") || isToken("(") || isToken("WITH")) {
        Query query = parseSelect();
        query.setNeverLazy(true);
        command.setCommand(query);
    } else if (readIf("DELETE")) {
        command.setCommand(parseDelete());
    } else if (readIf("UPDATE")) {
        command.setCommand(parseUpdate());
    } else if (readIf("INSERT")) {
        command.setCommand(parseInsert());
    } else if (readIf("MERGE")) {
        command.setCommand(parseMerge());
    } else {
        throw getSyntaxError();
    }
    return command;
}
Also used : Query(org.h2.command.dml.Query) Explain(org.h2.command.dml.Explain)

Example 8 with Analyze

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

the class DbUpgrade method upgrade.

private static void upgrade(ConnectionInfo ci, Properties info) throws SQLException {
    String name = ci.getName();
    String data = name + Constants.SUFFIX_OLD_DATABASE_FILE;
    String index = name + ".index.db";
    String lobs = name + ".lobs.db";
    String backupData = data + ".backup";
    String backupIndex = index + ".backup";
    String backupLobs = lobs + ".backup";
    String script = null;
    try {
        if (scriptInTempDir) {
            new File(Utils.getProperty("java.io.tmpdir", ".")).mkdirs();
            script = File.createTempFile("h2dbmigration", "backup.sql").getAbsolutePath();
        } else {
            script = name + ".script.sql";
        }
        String oldUrl = "jdbc:h2v1_1:" + name + ";UNDO_LOG=0;LOG=0;LOCK_MODE=0";
        String cipher = ci.getProperty("CIPHER", null);
        if (cipher != null) {
            oldUrl += ";CIPHER=" + cipher;
        }
        Connection conn = DriverManager.getConnection(oldUrl, info);
        Statement stat = conn.createStatement();
        String uuid = UUID.randomUUID().toString();
        if (cipher != null) {
            stat.execute("script to '" + script + "' cipher aes password '" + uuid + "' --hide--");
        } else {
            stat.execute("script to '" + script + "'");
        }
        conn.close();
        FileUtils.move(data, backupData);
        FileUtils.move(index, backupIndex);
        if (FileUtils.exists(lobs)) {
            FileUtils.move(lobs, backupLobs);
        }
        ci.removeProperty("IFEXISTS", false);
        conn = new JdbcConnection(ci, true);
        stat = conn.createStatement();
        if (cipher != null) {
            stat.execute("runscript from '" + script + "' cipher aes password '" + uuid + "' --hide--");
        } else {
            stat.execute("runscript from '" + script + "'");
        }
        stat.execute("analyze");
        stat.execute("shutdown compact");
        stat.close();
        conn.close();
        if (deleteOldDb) {
            FileUtils.delete(backupData);
            FileUtils.delete(backupIndex);
            FileUtils.deleteRecursive(backupLobs, false);
        }
    } catch (Exception e) {
        if (FileUtils.exists(backupData)) {
            FileUtils.move(backupData, data);
        }
        if (FileUtils.exists(backupIndex)) {
            FileUtils.move(backupIndex, index);
        }
        if (FileUtils.exists(backupLobs)) {
            FileUtils.move(backupLobs, lobs);
        }
        FileUtils.delete(name + ".h2.db");
        throw DbException.toSQLException(e);
    } finally {
        if (script != null) {
            FileUtils.delete(script);
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection) File(java.io.File) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 9 with Analyze

use of org.h2.command.ddl.Analyze 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 10 with Analyze

use of org.h2.command.ddl.Analyze 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)

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