Массовое удаление очень медленно на SQLite с использованием JDBC - PullRequest
0 голосов
/ 05 февраля 2020

У меня есть программа, которая обрезает элементы из базы данных SQLite на основе поля отметки времени. Кажется, когда я запускаю его на большой существующей базе данных, это занимает довольно много времени и увеличивает ресурсы на моем и без того низкоуровневом встроенном компьютере.

Может кто-нибудь предложить какие-либо идеи о том, как улучшить мой код, чтобы он работал лучше / быстрее?

Вызов из основной программы:

query = SQLiteInterface.getInstance().delete(entry, "SampleInfo_source_timestamp < '" + timestamp + "'");
                            dbCall(query);

Метод удаления SQLiteInterface:

 /**
     * This creates a SQLite formatted 'delete' statement that allows for where clauses
     * 
     * @param table - The table that the items are being selected from
     * @param filters - The list of where clauses
     * @return - A SQLite formatted delete query
     */
    public String delete(String table, String filters)
    {
        if (filters == null)
        {
            return "delete from " + table;
        } else
        {
            return "delete from " + table + " where " + filters;
        }
    }

Код, который фактически отправляет команду DB с использованием JDB C

/**
 * 
 * 
 * @param query - A SQLite formatted DB query
 */
public void dbCall(String query)
{
    // Good practice to create a new statement and result set instead of reusing
    Statement stmt = null;
    Connection conn = null;
    ResultSet rs = null;
    try
    {
        // Fetch the query and the request out of the QueryRequest object
        conn = cpds.getConnection();
        conn.setAutoCommit(false);

        // Make sure the query request isn't empty, if it is, there is no point in sending it to the DB
        if (!query.isEmpty())
        {
            // Initialize the new statement
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
        }

        conn.commit();

    }
    catch (SQLException e)
    {
        if (e.getMessage().contains("no such column"))
        {
            // This table is not one that needs to be cleaned up.
        }
        else
        {
            errorLog.error("", e);
            // Table busy, try again.
            dbCall(query);
        }
    }
    catch (Exception e2)
    {
        errorLog.error("", e2);
    }
    finally
    {
        if (rs != null)
        {
            try
            {
                rs.close();
            }
            catch (SQLException e)
            {
                errorLog.error("Failed to close JDBC result set.");
            }
        }
        if (stmt != null)
        {
            try
            {
                stmt.close();
            }
            catch (SQLException e)
            {
                errorLog.error("Failed to close JDBC statement.");
            }
        }
        if (conn != null)
        {
            try
            {
                conn.close();
            }
            catch (SQLException e)
            {
                errorLog.error("Failed to close JDBC connection.");
            }
        }

    }
...