图数据库与关系数据库 - 算法复杂性

时间:2018-01-26 06:37:40

标签: mysql algorithm neo4j graph-databases

我理解图表数据库与关系数据库相比,可以更好地表达关系。因此,我们可以更好地可视化图形数据,并编写不太复杂的查询。但是,图表数据库在性能方面也更好吗?请给出一个示例查询,与MySQL相比,在Neo4j中执行相同查询时可以提供更快的结果。

让我试着更清楚地解释我的问题。 以下是本书"图形数据库权威指南"形成Neo4j。该查询用于列出可根据其同行客户购买的产品推荐给客户的产品。

Cypher查询:

MATCH (u:Customer {customer_id:'customer-one'})-
[:BOUGHT]->(p:Product)<- [:BOUGHT]-
(peer:Customer)-[:BOUGHT]->(reco:Product)

WHERE not (u)-[:BOUGHT]->(reco)

RETURN reco as Recommendation, count(*) as Frequency
ORDER BY Frequency DESC LIMIT 5;

相应的SQL查询(非常庞大):

SELECT product.product_name as Recommendation, count(1) as Frequency
FROM product, customer_product_mapping, (SELECT cpm3.product_id,
cpm3.customer_id

FROM Customer_product_mapping cpm, Customer_product_mapping cpm2,

Customer_product_mapping cpm3

WHERE cpm.customer_id = ‘customer-one’

and cpm.product_id = cpm2.product_id

and cpm2.customer_id != ‘customer-one’

and cpm3.customer_id = cpm2.customer_id

and cpm3.product_id not in (select distinct product_id

FROM Customer_product_mapping cpm

WHERE cpm.customer_id = ‘customer-one’)

) recommended_products
WHERE customer_product_mapping.product_id = product.product_id
and customer_product_mapping.product_id in recommended_products.product_id
and customer_product_mapping.customer_id = recommended_products.customer_id
GROUP BY product.product_name
ORDER BY Frequency desc

Cypher查询较小的事实并不意味着内部查询以更有效的方式执行(即,它比SQL查询更快)。

如果我给出一个示例,其中使用图形数据库实际上减少了遍历量,而不是在MySQL中使用它。如果根据节点数量,关系船舶等给出答案,那就更好了。

由于

1 个答案:

答案 0 :(得分:0)

我的感觉是你的查询大致相当于此 - 虽然我仍然怀疑有更多的表实例而不是严格必要的...

SELECT p.product_name Recommendation
     , count(0) Frequency
  FROM product p
  JOIN customer_product_mapping m
    ON m.product_id = p.product_id
  JOIN Customer_product_mapping a
    ON a.product_id = m.product_id 
   AND a.customer_id = m.customer_id
  JOIN Customer_product_mapping cpm2
    ON cpm2.product_id = a.product_id 
   AND cpm2.customer_id != a.customer_id 
  JOIN Customer_product_mapping cpm3
    ON cpm3.customer_id = cpm2.customer_id
  LEFT
  JOIN Customer_product_mapping x
    ON x.product_id = cpm3.product_id
   AND x.customer_id = 'customer-one'
 WHERE cpm.customer_id = 'customer-one'
   AND x.product_d IS NULL
 GROUP 
    BY p.product_name
 ORDER 
    BY Frequency desc