开发者

Google App Engine JDO Query using Alternate logic for 'NOT IN'

I'm developing a Googl开发者_开发问答e App Engine Java app where users can search business objects from database based on search criteria. The search results (a list of records) should not include any of the records (certain number of records, say 100) from their past searches. I'm storing the past results in the User Profile for this reason. Any suggestions on efficiently implementing this logic (without using multiple collection iterations). I'm using JDO and there are restrictions in using 'NOT IN' condition in the queries.


Here's a solution, assuming your goal is to get 200 keys that are not in the history already. I will attempt to estimate the number of operations used as a proxy for "efficiency", since this is how we will be charged in the new pricing model

  1. Fetch the User object and "history keys" (1 read operation)
  2. Do a keys only query and fetch 300 records. (300 small operations)
  3. In your code, subtract any of the history keys from the 300 records. (0 operations)
  4. If you end up with less than 200 records after step 3, fetch another 100.(repeat if necessary) (100 small operations).
  5. Once you have 200 keys not seen before, you can fetch the full business object entities if you need them, or display the keys to the user. (200 read operations if you fetch the entire objects)

If the datastore supported a native "NOT IN" operator, then we could shave off 100 small operations from step 2, and skip step 4. The largest cost here will be fetching the actual 200 entities, which would have to happen with or without the NOT IN operator. Ultimately, this method is not that inefficient compared to what a native NOT IN operator would do.

Further optimizations:

  • If you don't need to display 200 keys all at once, then you can use cursors to only get N results at a time.

  • I am simply guessing when I suggest that you get 300 keys at first. You may need to get more or less. You can also probably get less than 100 on the second attempt.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜