Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

perf(dataset): fix N+1 database insert bottleneck in Notion sync (#39899)

Hassan Rady26b4798e880e
diff --git a/api/services/dataset_service.py b/api/services/dataset_service.py
index 2ac58e15911..0258ad7e304 100644
--- a/api/services/dataset_service.py
+++ b/api/services/dataset_service.py
@@ -2402,13 +2402,14 @@ class DocumentService:
truncated_page_name,
batch,
)
+ document.id = str(uuid.uuid4())
session.add(document)
- session.flush()
document_ids.append(document.id)
documents.append(document)
position += 1
else:
exist_document.pop(page.page_id)
+ session.flush()
# delete not selected documents
if len(exist_document) > 0:
clean_notion_document_task.delay(list(exist_document.values()), dataset.id)
diff --git a/api/tests/unit_tests/services/test_dataset_service_document.py b/api/tests/unit_tests/services/test_dataset_service_document.py
index 26a3ac08d5f..e8e231936dc 100644
--- a/api/tests/unit_tests/services/test_dataset_service_document.py
+++ b/api/tests/unit_tests/services/test_dataset_service_document.py
@@ -1022,6 +1022,7 @@ class TestDocumentServiceSaveDocumentWithDatasetId:
patch.object(DocumentService, "build_document", return_value=created_document) as build_document,
patch("services.dataset_service.clean_notion_document_task") as clean_task,
patch("services.dataset_service.DocumentIndexingTaskProxy") as document_proxy_cls,
+ patch("services.dataset_service.uuid.uuid4", return_value="doc-new"),
):
mock_redis.lock.return_value = _make_lock_context()
session.scalars.return_value.all.return_value = [existing_keep, existing_remove]