yangdx
commited on
Commit
·
711819b
1
Parent(s):
d71c66a
Optimize query speed by using directed edge Cypher statement
Browse files
lightrag/kg/postgres_impl.py
CHANGED
@@ -1201,7 +1201,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1201 |
tgt_label = target_node_id.strip('"')
|
1202 |
|
1203 |
query = """SELECT * FROM cypher('%s', $$
|
1204 |
-
MATCH (a:base {entity_id: "%s"})-[r]
|
1205 |
RETURN COUNT(r) > 0 AS edge_exists
|
1206 |
$$) AS (edge_exists bool)""" % (
|
1207 |
self.graph_name,
|
@@ -1233,7 +1233,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1233 |
label = node_id.strip('"')
|
1234 |
|
1235 |
query = """SELECT * FROM cypher('%s', $$
|
1236 |
-
MATCH (n:base {entity_id: "%s"})-[]
|
1237 |
RETURN count(x) AS total_edge_count
|
1238 |
$$) AS (total_edge_count integer)""" % (self.graph_name, label)
|
1239 |
record = (await self._query(query))[0]
|
@@ -1262,7 +1262,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1262 |
tgt_label = target_node_id.strip('"')
|
1263 |
|
1264 |
query = """SELECT * FROM cypher('%s', $$
|
1265 |
-
MATCH (a:base {entity_id: "%s"})-[r]
|
1266 |
RETURN properties(r) as edge_properties
|
1267 |
LIMIT 1
|
1268 |
$$) AS (edge_properties agtype)""" % (
|
@@ -1285,7 +1285,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1285 |
|
1286 |
query = """SELECT * FROM cypher('%s', $$
|
1287 |
MATCH (n:base {entity_id: "%s"})
|
1288 |
-
OPTIONAL MATCH (n)-[]
|
1289 |
RETURN n, connected
|
1290 |
$$) AS (n agtype, connected agtype)""" % (
|
1291 |
self.graph_name,
|
@@ -1604,7 +1604,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1604 |
query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1605 |
WITH [{src_array}] AS sources, [{tgt_array}] AS targets
|
1606 |
UNWIND range(0, size(sources)-1) AS i
|
1607 |
-
MATCH (a:base {{entity_id: sources[i]}})-[r:DIRECTED]
|
1608 |
RETURN sources[i] AS source, targets[i] AS target, properties(r) AS edge_properties
|
1609 |
$$) AS (source text, target text, edge_properties agtype)"""
|
1610 |
|
@@ -1643,7 +1643,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1643 |
query = """SELECT * FROM cypher('%s', $$
|
1644 |
UNWIND [%s] AS node_id
|
1645 |
MATCH (n:base {entity_id: node_id})
|
1646 |
-
OPTIONAL MATCH (n)-[]
|
1647 |
RETURN node_id, connected.entity_id AS connected_id
|
1648 |
$$) AS (node_id text, connected_id text)""" % (
|
1649 |
self.graph_name,
|
@@ -1711,7 +1711,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1711 |
strip_label = node_label.strip('"')
|
1712 |
count_query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1713 |
MATCH (n:base {{entity_id: "{strip_label}"}})
|
1714 |
-
OPTIONAL MATCH p = (n)-[*..{max_depth}]
|
1715 |
RETURN count(distinct m) AS total_nodes
|
1716 |
$$) AS (total_nodes bigint)"""
|
1717 |
|
@@ -1723,7 +1723,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1723 |
if node_label == "*":
|
1724 |
query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1725 |
MATCH (n:base)
|
1726 |
-
OPTIONAL MATCH (n)-[r]
|
1727 |
RETURN collect(distinct n) AS n, collect(distinct r) AS r
|
1728 |
LIMIT {max_nodes}
|
1729 |
$$) AS (n agtype, r agtype)"""
|
@@ -1731,7 +1731,7 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1731 |
strip_label = node_label.strip('"')
|
1732 |
query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1733 |
MATCH (n:base {{entity_id: "{strip_label}"}})
|
1734 |
-
OPTIONAL MATCH p = (n)-[*..{max_depth}]
|
1735 |
RETURN nodes(p) AS n, relationships(p) AS r
|
1736 |
LIMIT {max_nodes}
|
1737 |
$$) AS (n agtype, r agtype)"""
|
|
|
1201 |
tgt_label = target_node_id.strip('"')
|
1202 |
|
1203 |
query = """SELECT * FROM cypher('%s', $$
|
1204 |
+
MATCH (a:base {entity_id: "%s"})-[r]->(b:base {entity_id: "%s"})
|
1205 |
RETURN COUNT(r) > 0 AS edge_exists
|
1206 |
$$) AS (edge_exists bool)""" % (
|
1207 |
self.graph_name,
|
|
|
1233 |
label = node_id.strip('"')
|
1234 |
|
1235 |
query = """SELECT * FROM cypher('%s', $$
|
1236 |
+
MATCH (n:base {entity_id: "%s"})-[]->(x)
|
1237 |
RETURN count(x) AS total_edge_count
|
1238 |
$$) AS (total_edge_count integer)""" % (self.graph_name, label)
|
1239 |
record = (await self._query(query))[0]
|
|
|
1262 |
tgt_label = target_node_id.strip('"')
|
1263 |
|
1264 |
query = """SELECT * FROM cypher('%s', $$
|
1265 |
+
MATCH (a:base {entity_id: "%s"})-[r]->(b:base {entity_id: "%s"})
|
1266 |
RETURN properties(r) as edge_properties
|
1267 |
LIMIT 1
|
1268 |
$$) AS (edge_properties agtype)""" % (
|
|
|
1285 |
|
1286 |
query = """SELECT * FROM cypher('%s', $$
|
1287 |
MATCH (n:base {entity_id: "%s"})
|
1288 |
+
OPTIONAL MATCH (n)-[]->(connected:base)
|
1289 |
RETURN n, connected
|
1290 |
$$) AS (n agtype, connected agtype)""" % (
|
1291 |
self.graph_name,
|
|
|
1604 |
query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1605 |
WITH [{src_array}] AS sources, [{tgt_array}] AS targets
|
1606 |
UNWIND range(0, size(sources)-1) AS i
|
1607 |
+
MATCH (a:base {{entity_id: sources[i]}})-[r:DIRECTED]->(b:base {{entity_id: targets[i]}})
|
1608 |
RETURN sources[i] AS source, targets[i] AS target, properties(r) AS edge_properties
|
1609 |
$$) AS (source text, target text, edge_properties agtype)"""
|
1610 |
|
|
|
1643 |
query = """SELECT * FROM cypher('%s', $$
|
1644 |
UNWIND [%s] AS node_id
|
1645 |
MATCH (n:base {entity_id: node_id})
|
1646 |
+
OPTIONAL MATCH (n:base)-[]->(connected:base)
|
1647 |
RETURN node_id, connected.entity_id AS connected_id
|
1648 |
$$) AS (node_id text, connected_id text)""" % (
|
1649 |
self.graph_name,
|
|
|
1711 |
strip_label = node_label.strip('"')
|
1712 |
count_query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1713 |
MATCH (n:base {{entity_id: "{strip_label}"}})
|
1714 |
+
OPTIONAL MATCH p = (n:base)-[*..{max_depth}]->(m:base)
|
1715 |
RETURN count(distinct m) AS total_nodes
|
1716 |
$$) AS (total_nodes bigint)"""
|
1717 |
|
|
|
1723 |
if node_label == "*":
|
1724 |
query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1725 |
MATCH (n:base)
|
1726 |
+
OPTIONAL MATCH (n:base)-[r]->(target:base)
|
1727 |
RETURN collect(distinct n) AS n, collect(distinct r) AS r
|
1728 |
LIMIT {max_nodes}
|
1729 |
$$) AS (n agtype, r agtype)"""
|
|
|
1731 |
strip_label = node_label.strip('"')
|
1732 |
query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1733 |
MATCH (n:base {{entity_id: "{strip_label}"}})
|
1734 |
+
OPTIONAL MATCH p = (n:base)-[*..{max_depth}]->(m:base)
|
1735 |
RETURN nodes(p) AS n, relationships(p) AS r
|
1736 |
LIMIT {max_nodes}
|
1737 |
$$) AS (n agtype, r agtype)"""
|