Part4 - JDO/JPA执行Keys-Only的查询

Welcome to Episode 4 of JDO/JPA Snippets That Work. Today we're going to
look at keys-only queries.

Keys Only Queries

If you use the low-level datastore api you may have noticed that the
com.google.appengine.api.datastore.Query class has a setKeysOnly() method.
If you call this method before you execute the query the datastore will
return com.google.appengine.api.datastore.Entity instances that have their
keys filled in but none of their properties. This can reduce consumption of
your Datastore Data Received from API quota, especially if you've got some
large entities, but, more importantly, it can also reduce consumption of
your Datastore CPU Time quota. How? Well, if the fulfillment of your query
requires an index or a merge join your query actually executes in two
stages: First it scans the index to find the keys of the entities that
match and then it issues additional scans to retrieve the entities uniquely
identified by the matching keys. If your query is keys-only we can skip
that second step entirely. That means faster queries!
Now, JPA and JDO don't know anything about keys-only queries, but they do
give you the flexibility to either return your entire object or some subset
of the fields on your object. If you construct this subset to only contain
the primary key of your object, the App Engine implementation of JPA and JDO
will use a keys-only query. Let's look at some examples:

JPA:
@Entity 
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Key id;
private Date dateOfPublication;
// getters and setters
}


Now let's implement a method that returns the Keys of all Books published
betweeen 2 years (we'll assume someone else is creating and closing an
EntityManager named 'em' for us):

public List<Key> keysOfBooksPublishedBetween(EntityManager em, Date from, 
Date to) {
em.getTransaction().begin();
try {
Query q = em.createQuery("select id from " + Book.class.getName()
+ " where dateOfPublication >= :from AND dateOfPublication <=
:to");
q.setParameter("from", from);
q.setParameter("to", to);
return (List<Key>) q.getResultList();
} finally {
em.getTransaction().rollback();
}
}


JDO:
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = 
"true")
public class Book {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;
private Date dateOfPublication;
// getters and setters
}


Now let's implement a method that returns the Keys of all Books published
betweeen 2 years (we'll assume someone else is creating and closing a
PersistenceManager named 'pm' for us):

public List<Key> keysOfBooksPublishedBetween(PersistenceManager pm, Date 
from, Date to) {
pm.currentTransaction().begin();
try {
Query q = pm.newQuery("select id from " + Book.class.getName()
+ " where dateOfPublication >= :from && dateOfPublication <=
:to");
return (List<Key>) q.execute(from, to);
} finally {
pm.currentTransaction().rollback();
}
}


--------------------------------
Notice how we are only selecting the 'id' field from our Book class. This
is crucial. If you select any other fields your query will end up fetching
entire entities and the optimization will be lost.
Max

转载自:[url]http://groups.google.com/group/google-appengine-java/browse_thread/thread/1d699d0db8641dbd#[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值