Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Performance Improvement: Use JOIN when JSON fields being used, closes #1078

davidmezzetti8e569462f261
diff --git a/src/python/txtai/database/rdbms.py b/src/python/txtai/database/rdbms.py
index 60b180d1..3deadf8f 100644
--- a/src/python/txtai/database/rdbms.py
+++ b/src/python/txtai/database/rdbms.py
@@ -186,8 +186,11 @@ class RDBMS(Database):
if indexids:
select = f"{self.resolve('indexid')}, {self.resolve('score')}"
+ # Use JOIN when documents table is used to utilize indexes, default to LEFT JOIN
+ join = "JOIN" if any(x and self.jsonprefix() in x for x in [where, groupby, orderby]) else "LEFT JOIN"
+
# Build query text
- query = Statement.TABLE_CLAUSE % select
+ query = Statement.TABLE_CLAUSE % (select, join)
if where is not None:
query += f" WHERE {where}"
if groupby is not None:
diff --git a/src/python/txtai/database/schema/statement.py b/src/python/txtai/database/schema/statement.py
index 4221c0e8..3652766b 100644
--- a/src/python/txtai/database/schema/statement.py
+++ b/src/python/txtai/database/schema/statement.py
@@ -91,7 +91,7 @@ class Statement:
# Partial sql clauses
TABLE_CLAUSE = (
"SELECT %s FROM sections s "
- + "LEFT JOIN documents d ON s.id = d.id "
+ + "%s documents d ON s.id = d.id "
+ "LEFT JOIN objects o ON s.id = o.id "
+ "LEFT JOIN scores sc ON s.indexid = sc.indexid"
)