使用 neo4j 查询创建 100 万个关系的一些方法
Some way to create 1 million relationships with a neo4j query
通过这个查询,我从我的 csv 文件中导入了 75000 个节点。 (类别)
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///prodcategory.csv" AS row
CREATE (:Category {id: row.idProdCategory, name: row.name, idRestaurant: row.idRestaurant});
通过这个查询,我还从我的 csv 文件(产品)中导入了 100 万个节点
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///products.csv" AS row
CREATE (:Product {id: row.idProduct, idProductCategory: row.idProductCategory,name: row.name,idRestaurant:row.idRestaurant ,description: row.description, price: row.price, shipping_price: row.shippingPrice});
我正在使用此查询创建 id -> category 和 idProductCategory -> products 之间的关系。
MATCH (category:Category {id: category.id})
MATCH (Product:Product {idProductCategory: Product.idProductCategory})
WHERE Product.idProductCategory=category.id
MERGE (category)-[:OF_CATEGORY]->(Product);
这个查询只创建了2999个关系,我不相信我应该创建100万个关系,请如果有方法或配置能够创建超过100万个关系请帮助我,我将不胜感激.
尝试将您的查询更改为...您在查询中使用了太多过滤器
MATCH (category:Category),(Product:Product)
WHERE Product.idProductCategory=category.id
MERGE (category)-[:OF_CATEGORY]->(Product)
您也可以只更改第二个导入查询,这样就不需要单独的链接查询。
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///products.csv" AS row
CREATE (p:Product {id: row.idProduct, name: row.name,idRestaurant:row.idRestaurant ,description: row.description, price: row.price, shipping_price: row.shippingPrice})
MATCH (c:Category{id:row.idProductCategory}
MERGE (p)-[:OF_CATEGORY]->(c)
确保您在 Product.idProductCategory
上有索引。
我假设类别 ID 在类别中是唯一的。
CREATE CONSTRAINT ON (category:Category) ASSERT category.id IS UNIQUE;
我假设有多个产品具有相同的类别 ID。
CREATE INDEX ON :Product(idProductCategory);
然后您可以简单地匹配每个类别,然后为每个类别找到合适的产品并创建关系。
// match all of your categories
MATCH (category:Category)
// then with each category find all the products
WITH category
MATCH (Product:Product {idProductCategory: category.id })
// and then create the
MERGE (category)-[:OF_CATEGORY]->(Product);
如果您 运行 遇到内存限制,您可以使用 APOC 定期提交来包装您的查询...
call apoc.periodic.commit("
MATCH (category:Category)
WITH category
MATCH (Product:Product {idProductCategory: category.id })
MERGE (category)-[:OF_CATEGORY]->(Product)
",{limit:10000})
通过这个查询,我从我的 csv 文件中导入了 75000 个节点。 (类别)
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///prodcategory.csv" AS row
CREATE (:Category {id: row.idProdCategory, name: row.name, idRestaurant: row.idRestaurant});
通过这个查询,我还从我的 csv 文件(产品)中导入了 100 万个节点
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///products.csv" AS row
CREATE (:Product {id: row.idProduct, idProductCategory: row.idProductCategory,name: row.name,idRestaurant:row.idRestaurant ,description: row.description, price: row.price, shipping_price: row.shippingPrice});
我正在使用此查询创建 id -> category 和 idProductCategory -> products 之间的关系。
MATCH (category:Category {id: category.id})
MATCH (Product:Product {idProductCategory: Product.idProductCategory})
WHERE Product.idProductCategory=category.id
MERGE (category)-[:OF_CATEGORY]->(Product);
这个查询只创建了2999个关系,我不相信我应该创建100万个关系,请如果有方法或配置能够创建超过100万个关系请帮助我,我将不胜感激.
尝试将您的查询更改为...您在查询中使用了太多过滤器
MATCH (category:Category),(Product:Product)
WHERE Product.idProductCategory=category.id
MERGE (category)-[:OF_CATEGORY]->(Product)
您也可以只更改第二个导入查询,这样就不需要单独的链接查询。
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///products.csv" AS row
CREATE (p:Product {id: row.idProduct, name: row.name,idRestaurant:row.idRestaurant ,description: row.description, price: row.price, shipping_price: row.shippingPrice})
MATCH (c:Category{id:row.idProductCategory}
MERGE (p)-[:OF_CATEGORY]->(c)
确保您在 Product.idProductCategory
上有索引。
我假设类别 ID 在类别中是唯一的。
CREATE CONSTRAINT ON (category:Category) ASSERT category.id IS UNIQUE;
我假设有多个产品具有相同的类别 ID。
CREATE INDEX ON :Product(idProductCategory);
然后您可以简单地匹配每个类别,然后为每个类别找到合适的产品并创建关系。
// match all of your categories
MATCH (category:Category)
// then with each category find all the products
WITH category
MATCH (Product:Product {idProductCategory: category.id })
// and then create the
MERGE (category)-[:OF_CATEGORY]->(Product);
如果您 运行 遇到内存限制,您可以使用 APOC 定期提交来包装您的查询...
call apoc.periodic.commit("
MATCH (category:Category)
WITH category
MATCH (Product:Product {idProductCategory: category.id })
MERGE (category)-[:OF_CATEGORY]->(Product)
",{limit:10000})