Android SQL,你用對了嗎(一)——條件限定

Geedio發表於2016-10-28

提到SQLiteDatabase這個類,大家都不陌生。對資料庫進行增刪改查,免不了跟它打交道,其中:

刪改查三個操作往往需要進行條件限定。限定條件通過引數String whereClause, String[] whereArgs來指定。

whereClause的取值形如_id = ? AND condition1 >= ? OR condition2 != ?,其中的?用於引數繫結,按順序,填入whereArgs陣列內。

但說實話,使用這種方式,需要:

  1. 先將限定部分的SQL語句寫出來,將限定的引數替換為?
  2. 記住次序,填入引數陣列內。

這麼做一次還好,寫多了挺煩人的,如果後期修改的話,還需要仔細確保SQL語句書寫正確,再三確保修改不會弄錯引數順序。如果弄錯了?那隻能慢慢debug了。

為了方便,有的同學就直接放棄了whereClausewhereArgs這種搭配,直接傳入完整的SQL限定字串作為whereClause引數的值,放棄了引數化,在whereArgs引數傳入了null

這種用法一樣能達成我們的需求,甚至用起來更加方便,為什麼SDK無端端要搞得這麼複雜呢?答:一切都是為了效能。

原始碼探究

下面我們來看下原始碼,探究下處理過程,扒一扒SDK在哪一步優化了效能。

int delete(String table, String whereClause, String[] whereArgs)這個方法為切入點,相關實現在SQLiteDatabase.java裡。

    /**
     * Convenience method for deleting rows in the database.
     *
     * @param table the table to delete from
     * @param whereClause the optional WHERE clause to apply when deleting.
     *            Passing null will delete all rows.
     * @param whereArgs You may include ?s in the where clause, which
     *            will be replaced by the values from whereArgs. The values
     *            will be bound as Strings.
     * @return the number of rows affected if a whereClause is passed in, 0
     *         otherwise. To remove all rows and get a count pass "1" as the
     *         whereClause.
     */
    public int delete(String table, String whereClause, String[] whereArgs) {
        acquireReference();
        try {
            // 組裝成完整的SQL語句,例項化SQLiteStatement
            SQLiteStatement statement =  new SQLiteStatement(this, "DELETE FROM " + table +
                    (!TextUtils.isEmpty(whereClause) ? " WHERE " + whereClause : ""), whereArgs);
            try {
                // 執行SQL語句
                return statement.executeUpdateDelete();
            } finally {
                statement.close();
            }
        } finally {
            releaseReference();
        }
    }複製程式碼

原始碼裡並沒做什麼神奇的事情,僅僅是組裝成完整的SQL語句,和引數陣列一起,例項化SQLiteStatement,然後執行這個語句。

執行的過程實現在SQLiteStatement.java

    /**
     * Execute this SQL statement, if the the number of rows affected by execution of this SQL
     * statement is of any importance to the caller - for example, UPDATE / DELETE SQL statements.
     *
     * @return the number of rows affected by this SQL statement execution.
     * @throws android.database.SQLException If the SQL string is invalid for
     *         some reason
     */
    public int executeUpdateDelete() {
        acquireReference();
        try {
            // 獲取各個引數:sql語句、要繫結的引數等
            // 然後才是真正的執行
            return getSession().executeForChangedRowCount(
                    getSql(), getBindArgs(), getConnectionFlags(), null);
        } catch (SQLiteDatabaseCorruptException ex) {
            onCorruption();
            throw ex;
        } finally {
            releaseReference();
        }
    }複製程式碼

獲取引數,然後呼叫executeForChangedRowCount方法。這個方法在SQLiteSession.java


    /**
     * Executes a statement that returns a count of the number of rows
     * that were changed.  Use for UPDATE or DELETE SQL statements.
     *
     * @param sql The SQL statement to execute.
     * @param bindArgs The arguments to bind, or null if none.
     * @param connectionFlags The connection flags to use if a connection must be
     * acquired by this operation.  Refer to {@link SQLiteConnectionPool}.
     * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
     * @return The number of rows that were changed.
     *
     * @throws SQLiteException if an error occurs, such as a syntax error
     * or invalid number of bind arguments.
     * @throws OperationCanceledException if the operation was canceled.
     */
    public int executeForChangedRowCount(String sql, Object[] bindArgs, int connectionFlags,
            CancellationSignal cancellationSignal) {
        if (sql == null) {
            throw new IllegalArgumentException("sql must not be null.");
        }

        // 這裡雖然傳入了bindArgs,但並沒用到
        if (executeSpecial(sql, bindArgs, connectionFlags, cancellationSignal)) {
            return 0;
        }

        acquireConnection(sql, connectionFlags, cancellationSignal); // might throw
        try {
            // 真正用到sql和bindArgs的地方
            return mConnection.executeForChangedRowCount(sql, bindArgs,
                    cancellationSignal); // might throw
        } finally {
            releaseConnection(); // might throw
        }
    }複製程式碼

Ok,繼續深入,來到SQLiteConnection.java

    /**
     * Executes a statement that returns a count of the number of rows
     * that were changed.  Use for UPDATE or DELETE SQL statements.
     *
     * @param sql The SQL statement to execute.
     * @param bindArgs The arguments to bind, or null if none.
     * @param cancellationSignal A signal to cancel the operation in progress, or null if none.
     * @return The number of rows that were changed.
     *
     * @throws SQLiteException if an error occurs, such as a syntax error
     * or invalid number of bind arguments.
     * @throws OperationCanceledException if the operation was canceled.
     */
    public int executeForChangedRowCount(String sql, Object[] bindArgs,
            CancellationSignal cancellationSignal) {
        if (sql == null) {
            throw new IllegalArgumentException("sql must not be null.");
        }

        int changedRows = 0;
        final int cookie = mRecentOperations.beginOperation("executeForChangedRowCount",
                sql, bindArgs);
        try {
            // 獲取預先編譯過的SQL
            final PreparedStatement statement = acquirePreparedStatement(sql);
            try {
                throwIfStatementForbidden(statement);
                // 引數繫結
                bindArguments(statement, bindArgs);
                applyBlockGuardPolicy(statement);
                attachCancellationSignal(cancellationSignal);
                try {
                    // 交給SQLiteEngine執行
                    changedRows = nativeExecuteForChangedRowCount(
                            mConnectionPtr, statement.mStatementPtr);
                    return changedRows;
                } finally {
                    detachCancellationSignal(cancellationSignal);
                }
            } finally {
                releasePreparedStatement(statement);
            }
        } catch (RuntimeException ex) {
            mRecentOperations.failOperation(cookie, ex);
            throw ex;
        } finally {
            if (mRecentOperations.endOperationDeferLog(cookie)) {
                mRecentOperations.logOperation(cookie, "changedRows=" + changedRows);
            }
        }
    }複製程式碼

首先,會通過acquirePreparedStatement去獲取PreparedStatement例項,原始碼如下:

    private PreparedStatement acquirePreparedStatement(String sql) {
        PreparedStatement statement = mPreparedStatementCache.get(sql);
        boolean skipCache = false;
        if (statement != null) {
            if (!statement.mInUse) {
                return statement;
            }
            // The statement is already in the cache but is in use (this statement appears
            // to be not only re-entrant but recursive!).  So prepare a new copy of the
            // statement but do not cache it.
            skipCache = true;
        }

        final long statementPtr = nativePrepareStatement(mConnectionPtr, sql);
        try {
            final int numParameters = nativeGetParameterCount(mConnectionPtr, statementPtr);
            final int type = DatabaseUtils.getSqlStatementType(sql);
            final boolean readOnly = nativeIsReadOnly(mConnectionPtr, statementPtr);
            statement = obtainPreparedStatement(sql, statementPtr, numParameters, type, readOnly);
            if (!skipCache && isCacheable(type)) {
                mPreparedStatementCache.put(sql, statement);
                statement.mInCache = true;
            }
        } catch (RuntimeException ex) {
            // Finalize the statement if an exception occurred and we did not add
            // it to the cache.  If it is already in the cache, then leave it there.
            if (statement == null || !statement.mInCache) {
                nativeFinalizeStatement(mConnectionPtr, statementPtr);
            }
            throw ex;
        }
        statement.mInUse = true;
        return statement;
    }複製程式碼

可以看到,這裡有個mPreparedStatementCache用於快取之前生成過的PreparedStatement,如果之前有相同的SQL語句,則取出重用,避免重複編譯SQL。這個快取本質上是一個LruCache<String, PreparedStatement>key為sql語句。

也即是,如果我們使用whereClausewhereArgs的方式運算元據庫的話,同樣的whereClause,不同的whereArgs取值,將能利用到這個快取。但如果直接將限定語句拼接好,由於引數取值是可變的,一旦發生改變,就變成不同的語句,自然無法匹配上快取,白白浪費了已編譯過的PreparedStatement例項。

順便貼下繫結引數的程式碼:


    private void bindArguments(PreparedStatement statement, Object[] bindArgs) {
        final int count = bindArgs != null ? bindArgs.length : 0;
        if (count != statement.mNumParameters) {
            throw new SQLiteBindOrColumnIndexOutOfRangeException(
                    "Expected " + statement.mNumParameters + " bind arguments but "
                    + count + " were provided.");
        }
        if (count == 0) {
            return;
        }

        final long statementPtr = statement.mStatementPtr;
        for (int i = 0; i < count; i++) {
            final Object arg = bindArgs[i];
            switch (DatabaseUtils.getTypeOfObject(arg)) {
                case Cursor.FIELD_TYPE_NULL:
                    nativeBindNull(mConnectionPtr, statementPtr, i + 1);
                    break;
                case Cursor.FIELD_TYPE_INTEGER:
                    nativeBindLong(mConnectionPtr, statementPtr, i + 1,
                            ((Number)arg).longValue());
                    break;
                case Cursor.FIELD_TYPE_FLOAT:
                    nativeBindDouble(mConnectionPtr, statementPtr, i + 1,
                            ((Number)arg).doubleValue());
                    break;
                case Cursor.FIELD_TYPE_BLOB:
                    nativeBindBlob(mConnectionPtr, statementPtr, i + 1, (byte[])arg);
                    break;
                case Cursor.FIELD_TYPE_STRING:
                default:
                    if (arg instanceof Boolean) {
                        // Provide compatibility with legacy applications which may pass
                        // Boolean values in bind args.
                        nativeBindLong(mConnectionPtr, statementPtr, i + 1,
                                ((Boolean)arg).booleanValue() ? 1 : 0);
                    } else {
                        nativeBindString(mConnectionPtr, statementPtr, i + 1, arg.toString());
                    }
                    break;
            }
        }
    }複製程式碼

程式碼很簡單,就不多解釋了。

資料驗證

囉囉嗦嗦貼了這麼多原始碼,其實只是為了證明,whereClause搭配whereArgs是很有意義的。說是這麼說,但實際優化效能差多少呢?

通過demo驗證,執行一千次查詢的情況:
簡單語句_ID = ?

無引數化 有引數化
112 65

加大複雜度_ID >= ?

無引數化 有引數化
150 71

再複雜點_ID >= ? AND COLUMN_CATEGORY LIKE ?

無引數化 有引數化
190 87

結論:

  • 隨著限定語句複雜度的上升,編譯一次SQL語句的耗時也隨之增加;
  • 使用引數化能有效提升效能,使用引數化語句能提高約一倍的效能(場景越複雜,效果越明顯)

所以...為了效能考慮,寫程式碼的時候別再用拼接字串的方式直接生成限定語句了。

One more things...

但,最開始提及的那種不便的使用方式,難道就只能默默忍受了?答案顯然並不是,通過簡單的抽象、封裝,能夠實現如下的效果:

Statement statement =
        Statement.where(UPDATE_TIME).lessOrEqual(now)
        .and(EXPIRY_TIME).moreThan(now)
        .or(AGE).eq(23)
        .end();
statement.sql(); // 生成sql語句
statement.whereClause(); // 生成whereClause語句
statement.args(); // 對應的引數陣列複製程式碼

這是我嘗試造的一個輪子,用於通過語義化的方式,定義和生成whereClausewhereArgs。用起來就像是寫sql語句一樣自然,同時還能避免人工書寫sql語句導致的一些拼寫錯誤,生成的whereClause的引數順序也和whereArgs引數陣列嚴格對應。

寫得比較挫,就不發出來賣弄了。哈哈。如有錯誤的地方,還請各路大神指正!

相關文章