---
title: "Google App Engine里面的select count(*)"
author: "iamxiaole"
date: "2009-10-07T09:47:51Z"
tags: []
slug: "google-app-engine-select-count"
draft: false
lastmod: "2009-10-07T09:48:16Z"
id: 482410
url: "https://iamxiaole.iteye.com/blog/482410"
---

因为GAE规定了返回的条数不能超过1000条，所以只能分两种做法：

当记录的条数小于1000的时候：

```java
PersistenceManager pm = getPersistenceManager(); 
Query query = pm.newQuery(Chapter.class); 
query.setResult("count(this)"); 
int count = 0; 
try { 
  count = (Integer) query.execute(); 
} finally { 
  pm.close(); 
} 
```


当记录的条数大于1000的时候：
最有效的方法是新建一个表，一个counter字段来记录总记录数目。（原话：the most efficient solution would be incrementing a 
counter with every write and just reading this value when it's needed）