NEO4J UNWIND CREATE无休止运行

3pvhb19x  于 2023-10-18  发布在  其他
关注(0)|答案(2)|浏览(96)

我在执行以下代码时遇到了问题:

public static void create(final Session session, final String nodeName, final Map<String, List<Object>> attributes) {
        final String create = getCreate(nodeName, attributes);
        Logger.getLogger("Neo4jCommands").log(Level.FINE, "Creating node " + nodeName);
        try {
            session.executeWriteWithoutResult(tx -> {
                var query = new Query(create);
                tx.run(query);
            }, TransactionConfig.builder().withTimeout(Duration.of(30, ChronoUnit.SECONDS)).build());
        } catch (final Exception e) {
            if (create.length() > 3000) {
                Logger.getLogger("Neo4jCommands").log(Level.SEVERE, "Failed create: " + create.substring(0, 3000));
                ;
            } else {
                Logger.getLogger("Neo4jCommands").log(Level.SEVERE, "Failed create: " + create);
            }
            throw new RuntimeException(e);
        } finally {
            session.close();
        }
    }

下面的密码查询将无休止地运行:

UNWIND [6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698, 6432013617038820698] AS A
UNWIND [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] AS B
UNWIND ['1000000', '1000000', '1000000', '1000000', '1000000', '1000000', '1000000', '1000000', '1000000', '1000000', '1000000', '1000000', '1000000'] AS C
UNWIND ['Attribute101', 'Attribute102', 'Attribute103', 'Attribute104', 'Attribute105', 'Attribute106', 'Attribute107', 'Attribute108', 'Attribute109', 'Attribute110', 'Attribute111', 'Attribute112', 'Attribute113'] AS D
UNWIND [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4] AS E
UNWIND ['1000161', '1000162Text', '1000163', '1000164Text', '1000165', '1000166Text', '1000167', '1000168Text', '1000169', '1000170Text', '1000171', '1000172Text', '1000173'] AS F
UNWIND [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] AS G
CREATE (:CT11000000 {VA: A, VB: B, VC: C, VD: D, VE: E, VF: F, VG: G})

感谢您的帮助!

shstlldc

shstlldc1#

这里有7个嵌套的循环,它们占了13^7次迭代,即超过6200万次迭代。要么查询永远不会完成,因为它耗尽了内存,要么它只是需要(很多)时间。
如果你只需要创建13个CT11000000节点,你可以定义一个UNWIND,并使用一个包含13个map的列表:

UNWIND [
    {A: 6432013617038820698, B: 1, C: '1000000', D: 'Attribute101', E: 4, F: '1000161', G: 1}
    // [...] + 12 other maps
] AS attributes
CREATE (:CT11000000 {VA: attributes.A, VB: attributes.B, VC: attributes.C, VD: attributes.D, VE: attributes.E, VF: attributes.F, VG: attributes.G})

边注:请看看密码风格指南,标签和属性的情况下是相当不寻常的在这里

z9smfwbn

z9smfwbn2#

一个更简单的替代@fbiville的查询:

UNWIND [
    {va: 6432013617038820698, vb: 1, vc: '1000000', vc: 'Attribute101', vc: 4, vc: '1000161', vc: 1}
    // [...] + 12 other maps
] AS attributes
CREATE (n:CT11000000)
SET n = attributes

相关问题