Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

perf: replace heavy aggregate query with lightweight dropdown query (#3389)

LearningCircuite3d280bb3d9f
diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.py
index 3766e63767..b0c711c25d 100644
--- a/src/local_deep_research/research_library/routes/library_routes.py
+++ b/src/local_deep_research/research_library/routes/library_routes.py
@@ -158,7 +158,7 @@ def library_page():
unique_domains = service.get_unique_domains()
# Get research list for filter dropdown
- research_list = service.get_research_list_with_stats()
+ research_list = service.get_research_list_for_dropdown()
# Get collections list for filter dropdown
collections = service.get_all_collections()
@@ -944,10 +944,10 @@ def download_bulk():
@library_bp.route("/api/research-list")
@login_required
def get_research_list():
- """Get list of research sessions with download stats."""
+ """Get list of research sessions for dropdowns."""
username = session["username"]
service = LibraryService(username)
- research_list = service.get_research_list_with_stats()
+ research_list = service.get_research_list_for_dropdown()
return jsonify({"research": research_list})
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index da90e03266..260753c72d 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -499,6 +499,26 @@ class LibraryService:
return collections
+ def get_research_list_for_dropdown(self) -> List[Dict]:
+ """Get minimal research session info for filter dropdowns.
+
+ Returns only id, title, and query — no joins or aggregates.
+ """
+ with get_user_db_session(self.username) as session:
+ results = (
+ session.query(
+ ResearchHistory.id,
+ ResearchHistory.title,
+ ResearchHistory.query,
+ )
+ .order_by(ResearchHistory.created_at.desc())
+ .all()
+ )
+ return [
+ {"id": r.id, "title": r.title, "query": r.query}
+ for r in results
+ ]
+
def get_research_list_with_stats(
self,
limit: int = 0,
diff --git a/tests/research_library/routes/test_library_routes.py b/tests/research_library/routes/test_library_routes.py
index df378d2e69..51f88ccac6 100644
--- a/tests/research_library/routes/test_library_routes.py
+++ b/tests/research_library/routes/test_library_routes.py
@@ -72,6 +72,7 @@ def _authenticated_client(app):
mock_library_service.get_documents.return_value = []
mock_library_service.get_unique_domains.return_value = []
mock_library_service.get_research_list_with_stats.return_value = []
+ mock_library_service.get_research_list_for_dropdown.return_value = []
mock_library_service.get_all_collections.return_value = []
mock_library_service.get_document.return_value = None
mock_library_service.toggle_favorite.return_value = None
diff --git a/tests/research_library/routes/test_library_routes_coverage.py b/tests/research_library/routes/test_library_routes_coverage.py
index 1bd83ee98a..1463d22b63 100644
--- a/tests/research_library/routes/test_library_routes_coverage.py
+++ b/tests/research_library/routes/test_library_routes_coverage.py
@@ -242,7 +242,7 @@ class TestLibraryPage:
}
lib_svc.get_documents.return_value = [{"id": "d1", "title": "Doc 1"}]
lib_svc.get_unique_domains.return_value = ["arxiv.org"]
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(app, library_service=lib_svc) as (client, mocks):
@@ -258,7 +258,7 @@ class TestLibraryPage:
lib_svc.get_library_stats.return_value = {"storage_path": ""}
lib_svc.get_documents.return_value = []
lib_svc.get_unique_domains.return_value = []
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(app, library_service=lib_svc) as (client, mocks):
@@ -278,7 +278,7 @@ class TestLibraryPage:
lib_svc.get_library_stats.return_value = {"storage_path": ""}
lib_svc.get_documents.return_value = []
lib_svc.get_unique_domains.return_value = []
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(
@@ -1058,7 +1058,7 @@ class TestGetResearchList:
def test_returns_research_list(self, app):
lib_svc = Mock()
- lib_svc.get_research_list_with_stats.return_value = [
+ lib_svc.get_research_list_for_dropdown.return_value = [
{"id": "r1", "query": "quantum computing"}
]
diff --git a/tests/research_library/routes/test_library_routes_deep_coverage.py b/tests/research_library/routes/test_library_routes_deep_coverage.py
index fd96b60d15..c2c5bb30eb 100644
--- a/tests/research_library/routes/test_library_routes_deep_coverage.py
+++ b/tests/research_library/routes/test_library_routes_deep_coverage.py
@@ -395,7 +395,7 @@ class TestGetDocumentsPagination:
lib_svc.get_library_stats.return_value = {"storage_path": "/tmp"}
lib_svc.get_documents.return_value = []
lib_svc.get_unique_domains.return_value = []
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(app, library_service=lib_svc) as (client, _):
client.get("/library/?collection=col-42")
diff --git a/tests/research_library/routes/test_library_routes_view_coverage.py b/tests/research_library/routes/test_library_routes_view_coverage.py
index a005b24a78..33a5cc9cd7 100644
--- a/tests/research_library/routes/test_library_routes_view_coverage.py
+++ b/tests/research_library/routes/test_library_routes_view_coverage.py
@@ -176,7 +176,7 @@ class TestLibraryPageWithFilters:
lib_svc.get_library_stats.return_value = {"storage_path": "/tmp"}
lib_svc.get_documents.return_value = []
lib_svc.get_unique_domains.return_value = ["arxiv.org"]
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(app, library_service=lib_svc) as (client, ctx):
@@ -195,7 +195,7 @@ class TestLibraryPageWithFilters:
lib_svc.get_library_stats.return_value = {"storage_path": "/tmp"}
lib_svc.get_documents.return_value = []
lib_svc.get_unique_domains.return_value = []
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(app, library_service=lib_svc) as (client, ctx):
@@ -214,7 +214,7 @@ class TestLibraryPageWithFilters:
lib_svc.get_library_stats.return_value = {"storage_path": "/tmp"}
lib_svc.get_documents.return_value = []
lib_svc.get_unique_domains.return_value = []
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(app, library_service=lib_svc) as (client, ctx):
@@ -236,7 +236,7 @@ class TestLibraryPageWithFilters:
lib_svc.get_library_stats.return_value = {"storage_path": ""}
lib_svc.get_documents.return_value = []
lib_svc.get_unique_domains.return_value = []
- lib_svc.get_research_list_with_stats.return_value = []
+ lib_svc.get_research_list_for_dropdown.return_value = []
lib_svc.get_all_collections.return_value = []
with _auth_client(app, library_service=lib_svc) as (client, _):
diff --git a/tests/research_library/services/test_library_service_collections_stats.py b/tests/research_library/services/test_library_service_collections_stats.py
index cb51e39c84..fe671352af 100644
--- a/tests/research_library/services/test_library_service_collections_stats.py
+++ b/tests/research_library/services/test_library_service_collections_stats.py
@@ -203,6 +203,65 @@ class TestGetUniqueDomainsEdgeCases:
assert set(result) == {"arxiv.org", "pubmed", "other"}
+# ============== get_research_list_for_dropdown ==============
+
+
+class TestGetResearchListForDropdown:
+ """Tests for get_research_list_for_dropdown."""
+
+ def test_returns_id_title_query_only(self, mocker):
+ """Returned dicts contain only id, title, and query."""
+ service = _make_service()
+ mock_session = MagicMock()
+
+ mock_row1 = Mock()
+ mock_row1.id = "r1"
+ mock_row1.title = "My Research"
+ mock_row1.query = "quantum computing"
+
+ mock_row2 = Mock()
+ mock_row2.id = "r2"
+ mock_row2.title = None
+ mock_row2.query = "neural networks"
+
+ mock_query = MagicMock()
+ mock_query.order_by.return_value = mock_query
+ mock_query.all.return_value = [mock_row1, mock_row2]
+ mock_session.query.return_value = mock_query
+ _mock_session_cm(mocker, mock_session)
+
+ result = service.get_research_list_for_dropdown()
+
+ assert len(result) == 2
+ assert result[0] == {
+ "id": "r1",
+ "title": "My Research",
+ "query": "quantum computing",
+ }
+ assert result[1] == {
+ "id": "r2",
+ "title": None,
+ "query": "neural networks",
+ }
+ # Ensure no extra keys
+ assert set(result[0].keys()) == {"id", "title", "query"}
+
+ def test_empty_returns_empty_list(self, mocker):
+ """No research sessions → empty list."""
+ service = _make_service()
+ mock_session = MagicMock()
+
+ mock_query = MagicMock()
+ mock_query.order_by.return_value = mock_query
+ mock_query.all.return_value = []
+ mock_session.query.return_value = mock_query
+ _mock_session_cm(mocker, mock_session)
+
+ result = service.get_research_list_for_dropdown()
+
+ assert result == []
+
+
# ============== get_research_list_with_stats ==============