【问题标题】:Cassandra PreparedStatement vs normal insertCassandra PreparedStatement 与普通插入
【发布时间】:2016-01-15 23:19:22
【问题描述】:

我在我的项目中使用 Cassandra,并且在写入过程中遇到了超时问题,这与该人在这篇帖子中收到的相同 Cassandra cluster with bad insert performance and insert stability(目前我只使用一个节点进行测试,Java 驱动程序,最后释放卡桑德拉)。应用程序必须每天(在夜间)为每个用户插入大量数据。我有一个休息控制器,它接受文件,然后在它们并行到达时处理它们以在 Cassandra 中插入值。我必须为每个用户插入 100 万个条目,其中一个条目最多有 8 个值(时间不是那么重要,也可能需要 10 分钟)。根据Cassandra cluster with bad insert performance and insert stability 中提供的答案,我决定将 executeAsync()、Semaphore 和 PreparedStatement 添加到我的应用程序中,而之前我没有使用它们。

现在的问题是,使用可变键空间(每个用户一个)并且必须更新数据库中的列表,我无法在初始化阶段初始化我的 PreparedStatements,但我必须对每个文件至少执行一次已处理(一个文件包含 10+k 个条目),用户每天必须上传多达 100 个文件。因此,我收到以下警告:

Re-preparing already prepared query INSERT INTO c2bdd9f7073dce28ed973238ac85b6e5d6162fce.sensorMonitoringLog (timestamp, sensorId, isLogging) VALUES (?, ?, ?). Please note that preparing the same query more than once is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once.

我的问题是:像这样使用 PreparedStatement 是一个好习惯,还是使用带有 executeAsync() 的普通插入更好?

谢谢

【问题讨论】:

    标签: cassandra prepared-statement cql


    【解决方案1】:

    如果您在写入期间遇到超时问题,最好使用 PreparedStatement 但不要使用异步插入。超时是防止 Cassandra 工作超负荷的一种方法。使用异步,你会同时给它更多的工作,OOM 的风险会增加。

    要正确使用 PreparedStatement,您必须通过键空间创建一个且只有一个 Session 对象。然后每个会话必须准备一次自己的语句。

    此外,请注意它们是 PreparedStatement 和异步的线程安全风险。准备语句必须同步。但我再次建议您在这种情况下不要使用 ExecuteAsynch。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多