From a1c1b8609827097b9bfaeb9e34bc81e74b919498 Mon Sep 17 00:00:00 2001 From: lianhuix Date: Mon, 26 Sep 2022 20:17:51 +0800 Subject: [PATCH] optimization for get sync data Signed-off-by: lianhuix --- .../src/sqlite/sqlite_query_helper.cpp | 64 ++++++++++++++++++- .../storage/src/sqlite/sqlite_query_helper.h | 1 + 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp index b1f04273611..a3c312d48e7 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp @@ -962,6 +962,64 @@ int SqliteQueryHelper::GetRelationalSyncDataQuerySql(std::string &sql, bool hasS return errCode; } +namespace { +std::string GetRelationalSyncDataQueryHeader(const std::vector &fieldNames) +{ + std::string sql = "SELECT b.data_key," + "b.device," + "b.ori_device," + "b.timestamp as " + DBConstant::TIMESTAMP_ALIAS + "," + "b.wtimestamp," + "b.flag," + "b.hash_key,"; + if (fieldNames.empty()) { // For query check. If column count changed, can be discovered. + sql += "a.*"; + } else { + for (const auto &fieldName : fieldNames) { // For query data. + sql += "a." + fieldName + ","; + } + sql.pop_back(); + } + return sql; +} + +std::string GetRelationalLogTableQuerySql(const std::string &tableName) +{ + return "SELECT * FROM " + DBConstant::RELATIONAL_PREFIX + tableName + "_log WHERE " + + "(timestamp >= ? AND timestamp < ?) AND (flag & 3 = 2)"; +} +} + +int SqliteQueryHelper::GetRelationalSyncDataQuerySqlWithSubQuery(const std::vector &fieldNames, + std::string &sql) +{ + if (!isValid_) { + return -E_INVALID_QUERY_FORMAT; + } + + if (hasPrefixKey_) { + LOGE("For relational DB query, prefix key is not supported."); + return -E_NOT_SUPPORT; + } + sql = GetRelationalSyncDataQueryHeader(fieldNames); + sql += " FROM ("; + sql += "SELECT rowid,* FROM " + tableName_ + " AS a WHERE (1 = 1) "; + { + querySql_.clear(); // clear local query sql format + int errCode = ToQuerySyncSql(true, true); + if (errCode != E_OK) { + LOGE("To query sql fail! errCode[%d]", errCode); + return errCode; + } + sql += querySql_; + } + sql += ") AS a INNER JOIN ("; + sql += GetRelationalLogTableQuerySql(tableName_); + sql += ") AS b ON (a.rowid = b.data_key)"; + sql += " ORDER BY naturalbase_rdb_aux_timestamp;"; + return E_OK; +} + int SqliteQueryHelper::GetRelationalMissQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime, const std::vector &fieldNames, sqlite3_stmt *&statement) { @@ -986,13 +1044,15 @@ int SqliteQueryHelper::GetRelationalQueryStatement(sqlite3 *dbHandle, uint64_t b const std::vector &fieldNames, sqlite3_stmt *&statement) { bool hasSubQuery = false; + std::string sql; + int errCode = E_OK; if (hasLimit_ || hasOrderBy_) { hasSubQuery = true; // Need sub query. + errCode = GetRelationalSyncDataQuerySqlWithSubQuery(fieldNames, sql); } else { isNeedOrderbyKey_ = false; // Need order by timestamp. + errCode = GetRelationalSyncDataQuerySql(sql, hasSubQuery, fieldNames); } - std::string sql; - int errCode = GetRelationalSyncDataQuerySql(sql, hasSubQuery, fieldNames); if (errCode != E_OK) { LOGE("[Query] Get SQL fail!"); return -E_INVALID_QUERY_FORMAT; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h index 0b00bbbdd91..fc73a069fa6 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h @@ -97,6 +97,7 @@ public: int GetRelationalMissQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime, const std::vector &fieldNames, sqlite3_stmt *&statement); int GetRelationalSyncDataQuerySql(std::string &sql, bool hasSubQuery, const std::vector &fieldNames); + int GetRelationalSyncDataQuerySqlWithSubQuery(const std::vector &fieldNames, std::string &sql); int GetRelationalQueryStatement(sqlite3 *dbHandle, uint64_t beginTime, uint64_t endTime, const std::vector &fieldNames, sqlite3_stmt *&statement); -- Gitee