From 7f5d687882762b11f4c25a1422ea01194c01986a Mon Sep 17 00:00:00 2001 From: xiao-77 Date: Tue, 4 Mar 2025 20:10:40 +0800 Subject: [PATCH] Enh(insert): use mini cache in stmt to improve insert speed. --- include/libs/nodes/querynodes.h | 1 + source/libs/nodes/src/nodesUtilFuncs.c | 1 + source/libs/parser/src/parInsertSql.c | 21 ++++++++++++++++++--- tests/army/insert/auto_create_bench.py | 16 ++++++++-------- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index 3c15ffa6b4..7191b4ef4a 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -571,6 +571,7 @@ typedef struct SVnodeModifyOpStmt { SHashObj* pVgroupsHashObj; // SHashObj SHashObj* pTableBlockHashObj; // SHashObj SHashObj* pSubTableHashObj; // SHashObj + SHashObj* pSuperTableHashObj; // SHashObj SHashObj* pTableNameHashObj; // set of table names for refreshing meta, sync mode SHashObj* pDbFNameHashObj; // set of db names for refreshing meta, sync mode SHashObj* pTableCxtHashObj; // temp SHashObj for single request diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index ee7eff273d..8d47a90d19 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1325,6 +1325,7 @@ void nodesDestroyNode(SNode* pNode) { taosArrayDestroy(pStmt->pTableTag); taosHashCleanup(pStmt->pVgroupsHashObj); taosHashCleanup(pStmt->pSubTableHashObj); + taosHashCleanup(pStmt->pSuperTableHashObj); taosHashCleanup(pStmt->pTableNameHashObj); taosHashCleanup(pStmt->pDbFNameHashObj); taosHashCleanup(pStmt->pTableCxtHashObj); diff --git a/source/libs/parser/src/parInsertSql.c b/source/libs/parser/src/parInsertSql.c index e322dd1dc7..456ab93e20 100644 --- a/source/libs/parser/src/parInsertSql.c +++ b/source/libs/parser/src/parInsertSql.c @@ -1325,8 +1325,22 @@ static int32_t getUsingTableSchema(SInsertParseContext* pCxt, SVnodeModifyOpStmt return TSDB_CODE_SUCCESS; } if (!pCxt->missCache) { - bool bUsingTable = true; - code = getTableMeta(pCxt, &pStmt->usingTableName, &pStableMeta, &pCxt->missCache, bUsingTable); + char tbFName[TSDB_TABLE_FNAME_LEN]; + code = tNameExtractFullName(&pStmt->usingTableName, tbFName); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + STableMeta** ppStableMeta = taosHashGet(pStmt->pSuperTableHashObj, tbFName, strlen(tbFName)); + if (NULL != ppStableMeta) { + pStableMeta = *ppStableMeta; + } + if (NULL == pStableMeta) { + bool bUsingTable = true; + code = getTableMeta(pCxt, &pStmt->usingTableName, &pStableMeta, &pCxt->missCache, bUsingTable); + if (TSDB_CODE_SUCCESS == code) { + code = taosHashPut(pStmt->pSuperTableHashObj, tbFName, strlen(tbFName), &pStableMeta, POINTER_BYTES); + } + } } if (pCxt->isStmtBind) { goto _no_ctb_cache; @@ -1349,7 +1363,6 @@ _no_ctb_cache: code = cloneTableMeta(pStableMeta, &pStmt->pTableMeta); } } - taosMemoryFree(pStableMeta); taosMemoryFree(pCtableMeta); if (TSDB_CODE_SUCCESS == code && !pCxt->missCache) { code = getTargetTableVgroup(pCxt->pComCxt, pStmt, true, &pCxt->missCache); @@ -2847,6 +2860,7 @@ static int32_t createVnodeModifOpStmt(SInsertParseContext* pCxt, bool reentry, S } } pStmt->pSubTableHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK); + pStmt->pSuperTableHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK); pStmt->pTableNameHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK); pStmt->pDbFNameHashObj = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK); if ((!reentry && (NULL == pStmt->pVgroupsHashObj || NULL == pStmt->pTableBlockHashObj)) || @@ -2856,6 +2870,7 @@ static int32_t createVnodeModifOpStmt(SInsertParseContext* pCxt, bool reentry, S } taosHashSetFreeFp(pStmt->pSubTableHashObj, destroySubTableHashElem); + taosHashSetFreeFp(pStmt->pSuperTableHashObj, destroySubTableHashElem); *pOutput = (SNode*)pStmt; return TSDB_CODE_SUCCESS; diff --git a/tests/army/insert/auto_create_bench.py b/tests/army/insert/auto_create_bench.py index 0996aac87a..d8ac9dde24 100644 --- a/tests/army/insert/auto_create_bench.py +++ b/tests/army/insert/auto_create_bench.py @@ -100,15 +100,15 @@ if __name__ == "__main__": precreate_tables() # 测试场景1:自动建表插入 auto_create_time = test_auto_create_tables() - # 清理环境并重新初始化 - prepare_database() - # 预创建所有子表 - precreate_tables() - # 测试场景2:直接插入 - direct_insert_time = test_direct_insert() + # # 清理环境并重新初始化 + # prepare_database() + # # 预创建所有子表 + # precreate_tables() + # # 测试场景2:直接插入 + # direct_insert_time = test_direct_insert() # 打印最终结果 print("\n测试结果对比:") print(f"自动建表插入耗时: {auto_create_time:.2f} 秒") - print(f"直接插入耗时: {direct_insert_time:.2f} 秒") - print(f"性能差异: {auto_create_time/direct_insert_time:.1f} 倍") \ No newline at end of file + # print(f"直接插入耗时: {direct_insert_time:.2f} 秒") + # print(f"性能差异: {auto_create_time/direct_insert_time:.1f} 倍") \ No newline at end of file