【问题标题】:How to reduce the number of Write Operations for a simple Entity?如何减少简单实体的写入操作次数?
【发布时间】:2013-12-14 06:31:18
【问题描述】:

我有以下Entity

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DCOL", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("Alias")
public class Alias
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;
    @Basic
    private UUID from;
    @Basic
    private UUID to;
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
    private Date createdOn;

    public Alias() { /* intentionally blank */ }

    public Alias(@Nonnull final UUID from, @Nonnull final UUID to)
    {
        this.key = KeyFactory.createKey(Alias.class.getSimpleName(), from.toString());
        this.from = from;
        this.to = to;
        this.createdOn = new Date();
    }
}

这是持久化它的代码:

final EntityManager em = EMF.TRANSACTIONS_OPTIONAL.createEntityManager();
try
{
    final Alias a = new Alias(from, to);
    em.persist(a);
}
finally
{
    em.close();
}

目前需要6Datastore 写入操作才能持久化此Entity

我通过用@Extension 标记createdOn 将数字从10 减少到6,以将其从自动索引中排除。这是写操作的40% 减少!

有什么办法可以减少这么简单的事情的写入次数?

直接使用低级的Datastore API 会有什么改进吗?

【问题讨论】:

    标签: google-app-engine jpa google-cloud-datastore jpa-2.0


    【解决方案1】:

    正如您所提到的,您的实体需要 6 次写入操作,划分如下:

    • 1 为实体本身写
    • 1 写入内置EntitiesByKind 索引
    • 2 次写入 from 属性(1 次写入内置索引 EntitiesByProperty,另一次写入内置索引 EntitiesByPropertyDesc)。
    • 2 次写入 to 属性(1 次写入内置索引 EntitiesByProperty,另一次写入内置索引 EntitiesByPropertyDesc)。

    此时减少写入次数的唯一机会是将任何fromto 属性也标记为未编制索引(从而将每个属性的写入次数减少2)。

    您可以在此处查看更多信息:https://developers.google.com/appengine/docs/java/datastore/entities#Java_Understanding_write_costs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2014-08-22
      • 2017-02-14
      相关资源
      最近更新 更多