A blog about one guys quest to learn Python, Django and Google App Engine.
Feed: http://appengineguy.com/atom.xml

onsdag 16 juli 2008

How to do AVG and SUM in Google App Engine Data Store

People who are used to relational databases, which is pretty much every gosh-darned web developer out there, will run into pretty much the same obstacles with the app engine datastore - one of them is How the heck do I do SUM or AVG?. Yeah, due to how the Data Store works - you cannot do any kind of aggregate query. Instead, you have to re-calculate the totals at write time and keep them in a Counter instead. Like this:

models.py

And you use it like this...

tests.py

Performance note: It is a slightly bad idea to do counters like I have done above, but in my defence, I have done it for simplicity. In practice, it's important that you shard your counters.

As always, comments and questions are always welcome!

9 kommentarer:

Alex sa...

I don't understand why you need a custom Callable class -- wouldn't the built-in staticmethod (used as a decorator, @staticmethod) do just as well?

Mattias Johansson sa...

Well, what do you know! Looks lite it! I'll update the code later today using @staticmethod.

For some reason, I didn't find how to do static methods in python when googling - maybe I was on drugs at that particular moment in time.

davet sa...

Should line 42:
votes.save()
be
total.save()

Unknown sa...

jaosro@netti.fi

Unknown sa...

dsadsaadsdsa.wtfwtfwtf.asddsaa@gmail.com

Arastotle sønn Plato sønn Socrates sønn sa...

May I ask how you are formatting your code so nicely? with alternating lines and line numbers; what are you using?

Calvin Spealman sa...

As pointed elsewhere, this global counter is itself a bottleneck. Instead, assume multiple counters by any name as shards and for the sum (or the sum that you then divide for an average) query for all of them and add up the total. This lets you grab multiple locks at the same time to update the same counter, because you update just one of many aggregate counters.

Avinash sa...

Get More adds Earn Money

Anonym sa...

免費線上漫畫免費線上漫畫免費線上漫畫免費線上漫畫免費線上漫畫免費線上漫畫免費線上漫畫免費線上漫畫免費線上漫畫免費線上漫畫psp遊戲下載psp遊戲下載psp遊戲下載psp遊戲下載psp遊戲下載psp遊戲下載psp遊戲下載psp遊戲下載psp遊戲下載psp遊戲下載情色文學區情色文學區情色文學區情色文學區情色文學區情色文學區情色文學區情色文學區情色文學區情色文學區海賊王h漫畫海賊王h漫畫海賊王h漫畫海賊王h漫畫海賊王h漫畫海賊王h漫畫海賊王h漫畫海賊王h漫畫海賊王h漫畫海賊王h漫畫99770在線漫畫99770在線漫畫99770在線漫畫99770在線漫畫99770在線漫畫99770在線漫畫99770在線漫畫99770在線漫畫99770在線漫畫99770在線漫畫台灣色情論壇台灣色情論壇台灣色情論壇台灣色情論壇台灣色情論壇台灣色情論壇台灣色情論壇台灣色情論壇台灣色情論壇台灣色情論壇金瓶梅H漫畫網金瓶梅H漫畫網金瓶梅H漫畫網金瓶梅H漫畫網金瓶梅H漫畫網aa片免費看微風論壇080哈啦聊天室6k聊天室成人聊天室上班族捷克論壇大眾論壇plus論壇080視訊聊天室520視訊聊天室尋夢園上班族聊天室成人聊天室上班族 a片a片影片免費情色影片免費a片觀看小弟第貼影片區免費av影片免費h影片試看 H漫 - 卡通美女短片小魔女貼影片免費影片觀賞無碼a片網美女pc交友相簿美女交友-哈啦聊天室中文a片線上試看免費電影下載區免費試看a短片免費卡通aa片觀看女優影片無碼直播免費性感a片試看日本AV女優影音娛樂網日本av女優無碼 dvd辣妹視訊 - 免費聊天室美女交友視訊聊天室 080免費視訊聊天室尋夢園聊天室080苗栗人聊天室a片下載日本免費視訊美女免費視訊聊天中文搜性網後宮電影院 - 免費a片a片下載情色A片下載gogo2sex免費成人影片xvediox 免費a片影片 go2av免費影片伊莉討論區 sex520免費影片gogobox下載論壇ggyy8在線漫畫