homedark

Making our MongoDB Code Run Faster

May 09, 2011

Yesterday I talked about the work I was doing in improving the performance around mogade's ranking functionality. Basically, we denormalized our data and leaned on Redis for its built-in sorted set data structure.

When you're working closely with code, trying to improve specific aspects of it, all types of smaller things jump out at you trying to get your attention. Today I wanted to run a some benchmarks and see if any of improvements I briefly considered yesterday would actually be worth implementing.

As a recap, we're dealing with scores/leaderboards. Our structure looks something like:

|leaderboard_id	|	name	|	unique			|	created_at			|	points	|
|	1			|	leto	|	device-1-leto	|	2011/04/11 10:32	|	100000	|
|	1			|	paul	|	device-1-paul	|	2011/04/12 09:22	|	200000	|
|	2			|	duke	|	device-3-duke	|	2011/04/12 18:34	|	200000	|
|	1			|	jess	|	device-2-jess	|	2011/04/13 21:44	|	300000	|

We only track a player's best score.

Idea 1 - Reducing Index Memory

In order to efficiently get leaderboard pages, there's a composite index on leaderboard_id, created_at and points. If we want today's leaderboard, we end up doing the MongoDB equivalent of where leaderboard_id = 1 and created_at >= '2011/05/09 00:00' order by points desc (assuming today is May 9th). So our index is pretty solid.

For 500,000 scores, this index takes ~26MB. My idea? Rather than storing the absolute date the score was entered, store the time as an offset in days from a certain point (say Jan 1st 2011). For example, our first document's date would change from 2011/04/11 10:32 to 100. I know, I know...people hate this type of date messing up..but I wanted to try it out.

The reason for this madness? I wanted to know what impact indexing a 32bit field (an int) rather than a 64bit (a date) field would have on memory. You'd think that at a mere 26MB, I shouldn't care...but I can't help it. As I probably could have guessed, given that this index is made up of 3 fields, and cutting the storage requirement of one field by 1/2, resulted in an approximate memory savings of 1/6th. Not a compelling improvement.

Idea 2 - Improve Paging Performance

Although my main interest with the above change was to reduce memory footprint, I couldn't help but wonder what impact it might have on fetching leaderboards. Rather than finding all scores where the date >= XYZ we're able to use a straight up equality operator (because all scores for today will have the same date value).

Using the original approach, grabbing a couple thousand random pages of scores took about 25 seconds. Using the new approach took 5 seconds. Wow, hold on a minute, a 5 times improvement? This is something I'm interested in.

Two things changed here though, first we're using an equality rather than a greater than or equal. Second, we're compare an int versus a date. I suspected that the performance gain was coming from the equality check. I rolled back my date change and tried again. The same 5 time speed improvement.

This means if we store our date without hours we can still get our 5 times performance improvement without making our data all fugly and weird.

Idea 3 - Rename fields

In MongoDB every field name is stored in every document. This leads to developers who start to try to cheat some characters out of field names. Using the full names our 500K documents took around 80MB. Renaming the fields to things like lid, n, u, cat and p reduced this to 62MB. Not bad.

In v2 we are [stupidly] using our own thin mapping layer between our models and the MongoDB ruby driver. The code takes care of this aliasing for us, so there's no reason not to use it. Hopefully though, this is something MongoDB will support natively one day. Also, our data is pretty small, so the ratio of field name length to data is significant. You might not get the same results.

Idea 4 - Removing an Index

There's a 3rd index (I say 3rd, because I'm also counting the default (and unremovable) index on _id) in our scores table. It's on the leaderboard_id and unique fields. This is the index used when finding or updating a specific player's score for a leaderboard. Since unique is a string (a sha1 hash), this index is as big as the other two indexes combined, and over 1/2 the size of our data. If I'm really interested in reducing memory usage, this seems like an ideal candidate

If you didn't read yesterday's post, you'll need to understand that we actually have 3 scores collections. One for the daily, weekly and overall scores. Also, yesterday we introduced a high_scores collection which tracks player's best score. This is duplicate data from what's available in our 3 scores collections, but we can get to it with a single read, rather than 3.

My idea is to store the _id of the daily, weekly and overall scores within the high_scores collection (in addition to the points which we are already storing). Since we are already reading and writing from high_scores when saving a new score, it isn't too much work to bring back an few more bytes. Rather than updating by leaderboard_id and unique, we can update directly against _id.

This doesn't only mean we get to drop our index, we actually get to remove the unique field since that's all it is being used for. Within high_scores we'll still have an index on leaderboard_id and unique, but we'll have this in a single collection rather than 3 collection. This should reduce that indexes memory footprint by 2/3s (plus 2/3s of whatever space is taken up by the unique field, which itself won't be small potatoes).

Conclusion

I think the most surprising outcome from this was the raw performance gain I saw when moving from >= to ==. Obviously I need to brush up on my B-Tree knowledge (the structure MongoDB uses for indexes). I'm also excited about the memory savings we'll get by removing the 3rd index (our production DB has more than 500K records). This is one of those things that, in retrospect, should have been obvious a long time ago.

For what it's worth, I enjoy doing premature optimization. You can think it's evil, but I find it fun. If I'm coding on my own time, and not having fun, something's not right. Beyond that though, I really think that there are legitimate and important reasons for premature optimization.

First, it is a great way to learn some fundamental stuff that you probably wouldn't consider looking at otherwise. I don't care if you've never had slow SQL, I think every developer should understand a bit about B-Trees, should know how to read query plans, and make sense of the various join approaches a database might take. As more and more gets abstracted away, it becomes increasing important to go out of our way to understand how, once fundamental things like memory allocation, work. Optimizing code is a good, practical way to get a hands on feel for this stuff.

Secondly, performance is a feature. A really important feature. Often times, you simply don't know how slow, or fast, something is until you start playing with it. Was my date to int change stupid? Sure. Could I have used my little brain to figure out that I'd get a 1/6 saving? Eh..perhaps. Should I have stopped once I realized I was talking about 26MB? Probably. But now I know. Plus, in exploring that memory improvement, I found a 5x performance improvement which I will be rolling out. And I won't only be rolling it out, I'm going to try to get a better understanding as to why it's really 5x faster, and apply that knowledge, wherever appropriate, moving forward.

"But the grooms, or 'bettos,' as the Japanese call them, are not the only ones who indulge in tattooing. You will see many of the 'sendos,' or boat-coolies, thus marked, but in a less degree than the bettos. Perhaps it is because the grooms are obliged to run so much, and consequently wish to lay aside all garments. As they must wear something, they have their skins decorated in this way, and thus have a suit of clothing always about them. Joe Johnston's shattered army was at Jackson, about forty-five miles to northward; beleaguered Vicksburg was in the Northwest, a trifle farther away; Natchez lay southwest, still more distant; and nearly twice as far in the south was our heartbroken New Orleans. We had paused to recuperate our animals, and there was a rumor that we were to get new clothing. Anyhow we had rags with honor, and a right to make as much noise as we chose. "Nonsense, my dear fellow. Now let me open your eyes. Behold the great force of a man who is gifted with second sight. Where did you get those notes? Was it not on the same evening as the murder?" "Oh, nothing," came the reply. "Only I was so silly as to place the wrong end of my cigarette in my mouth and burnt my lips. What's tuberose?" Reviewing these mechanical conditions, we may at once see sufficient reasons for the platen movement of planing machines; and that it would be objectionable, if not impossible, to add a traversing or cutting action to tools already supported through the medium of eight joints. To traverse for cutting would require a moving gib joint in place of the bolted one, between the standards and main frame, leading to a complication of joints and movements quite impracticable. And disadvantaged aye begins the strife. We have already seen how this fundamental division is applied to the universe as a whole. But our philosopher is not content with classifying the phenomena as he finds360 them; he attempts to demonstrate the necessity of their dual existence; and in so doing is guilty of something very like a vicious circle. For, after proving from the terrestrial movements that there must be an eternal movement to keep them going, he now assumes the revolving aether, and argues that there must be a motionless solid centre for it to revolve round, although a geometrical axis would have served the purpose equally well. By a still more palpable fallacy, he proceeds to show that a body whose tendency is towards the centre, must, in the nature of things, be opposed by another body whose tendency is towards the circumference. In order to fill up the interval created by this opposition, two intermediate bodies are required, and thus we get the four elementsearth, water, air, and fire. These, again, are resolved into the antithetical couples, dry and wet, hot and cold, the possible combinations of which, by twos, give us the four elements once more. Earth is dry and cold, water cold and wet, air wet and hot, fire hot and dry; each adjacent pair having a quality in common, and each element being characterized by the excess of a particular quality; earth is especially dry, water cold, air wet, and fire hot. The common centre of each antithesis is what Aristotle calls the First Matter, the mere abstract unformed possibility of existence. This matter always combines two qualities, and has the power of oscillating from one quality to another, but it cannot, as a rule, simultaneously exchange both for their opposites. Earth may pass into water, exchanging dry for wet, but not so readily into air, which would necessitate a double exchange at the same moment. 192 He helped her out. "I have drifted in a way," he went on to explain. "I left home when I was a mere boy, and the spirit of savagery and unrest laid hold of me. I can't break away. And I'm not even sure that I want to. You, I dare say, can't understand." Yet he felt so sure, for some reason, that she could that he[Pg 71] merely nodded his head when she said briefly, "I can." "Then, too," he went on, "there is something in the Indian character that strikes a responsive chord in me. I come of lawless stock myself. I was born in Sidney." Then he stopped short. What business was it of hers where he had been born? He had never seen fit to speak of it before. Nevertheless he intended that she should understand now. So he made it quite plain. "Sidney was a convict settlement, you know," he said deliberately, "and marriages were promiscuous. My grandfather was an officer who was best away from England. My grandmother poisoned her first husband. That is on my mother's side. On my father's side it was about as mixed." He leaned back, crossing his booted legs and running his fingers into his cartridge belt. His manner asked with a certain defiance, what she was going to do about it, or to think. "Naw; git out. Don't bother me with no questions, I tell you," impatiently said a man in citizen's clothes, who with arms outspread was signalling the switching engines. "'Tain't my business to give information to people. Got all I kin do to furnish brains for them bull-headed engineers. Go to that Quartermaster you see over there in uniform. The Government pays him for knowin' things. It don't me." As little Pete dropped to the ground, his nervous finger touched the trigger and his gun went off up in the air. The others took this as a cue, and banged away as rapidly as they could get their muskets off. "I am no master," Cadnan said wearily. "I am a slave." "The people know," Dodd said. "It's out. It's all out. About the slavery. Is that what you mean?" Cadnan peered at him, half-fearfully. "You are a master." One did not give orders to masters, or argue with them. "Why not? There's naun shameful in it. Munds's brother did it for twenty years. And think of the difference it'll m?ake to usthirty pound or so a year, instead of the dead loss of Harry's keep and the wages of an extra man beside. I tell you, mother, I wur fair sick about the farm till I thought of this." "Ben, I swear I'm your true wife." Reuben started, and Pete awoke noisily. Harry was frightened and dropped his string, crying because he could not find it. The knock came again, and this time Pete crossed the room yawning, and opened the door. www.clacc.com.cn
www.ifandco.com.cn
bjqiangsheng.com.cn
www.thjt002.com.cn
www.rbxeb.com.cn
www.ftfr130.com.cn
fyws554.com.cn
jumpsoft.com.cn
yuwenjie.com.cn
www.cnspring.net.cn
中韩欧美一级一中文字暮 _黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 _毛片机地男男性行为免费视频播放九九欧美一级毛欧美片 _啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中韩欧美一级一中文字暮 中文字幕第十九页 中央游月中文字幕 中文字幕 第9页 中文字幕午夜福利 黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 中文字幕欧美日韩 中村知惠中文字幕 啪啪啪在线视频 中文字幕亚洲综合 中文字幕先锋资源 中文字幕 青青草 中文字幕资源网站 中韩欧美一级一中文字暮 中文字幕卡通动漫 中文字幕理论电影 免费视频播放 中文字幕资源在线 毛片免费观看 黄色一级电影片 中国美女一级看片 中文字幕先锋影音 黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 欧美在线不卡 中文字幕巨乳有码 啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中文字幕第86页 中文字幕91在线 黄色一级录像片 中文字幕久荜在线 黄色一级欧美片 毛片视频在线 中文字幕 第7页 中文字幕每日更新 毛片免费试看 中文字幕巨乱亚洲 中韩欧美一级一中文字暮 啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中韩欧美一级一中文字暮 毛片机地男男性行为免费视频播放九九欧美一级毛欧美片 黄色一级录像带 中文在线视频观看 中文字幕Av电影 啪啪啪男女视频 中文字幕 新妈妈 中国一级特黄大片 中文字幕久久视频 中文字幕黄色视频 中国黄色一级大片 中文字幕手机看片 中文字幕伦理在线 欧美一级毛 中韩欧美一级一中文字暮 免费视频播 啪啪免费视频网站 中文字幕男人天堂 黄色一级伦理片 中文字幕亚洲在线 中文字幕视频不卡 毛片免费网址 中国一级黄色大片 中文字幕亚洲情字 啪啪视频在线播放 啪拍看片久色综合免费福利视频玖玖60岁欧美老妇一级毛 中韩欧美一级一中文字暮 啪啪免费在线视频 亚洲AV无码天堂 中文字幕丝袜美腿 毛片免费视频 毛片在线电影 毛片在线不卡 啪啪啪在线播放 中文字幕伦理电影 中文字幕中文字幕 中文字幕一级在线 毛片免费基地 中文字幕综合影院 中文字幕手机在线 中文字幕视频在线 毛片机地男男性行为免费视频播放九九欧美一级毛欧美片 中国一级特大黄片 中文字幕乱码视频 中国成人在线视频 啪啪啪视频欧美 中国一级黄色电影 啪啪啪性爱动态图 啪啪啪欧美视频 黄色一级片播放 中文字幕乱码免费 黄片BB亚洲AV无码天堂www亚洲国产韩国欧美在线不卡一级 黄色一级毛毛片 中文娱乐在线视频 黄色一级黄色片 中文字幕乱伦电影 黄色一级片aa 中文字幕第十七页 男男性行为免费视频播放 黄色一级片观看 毛片免费电影
кŷһһĺ _ƬBBAVwww޹ŷ߲һ _ëƬΪƵžžŷһëŷƬ _žĿƬɫۺѸƵ60ŷϸһë кŷһһĺ Ļʮҳ Ļ Ļ 9ҳ Ļҹ ƬBBAVwww޹ŷ߲һ Ļŷպ д֪Ļ žžžƵ Ļۺ ĻȷԴ Ļ ĻԴվ кŷһһĺ Ļͨ Ļ۵Ӱ Ƶ ĻԴ ëƬѹۿ ɫһӰƬ йŮһƬ ĻȷӰ ƬBBAVwww޹ŷ߲һ ŷ߲ Ļ žĿƬɫۺѸƵ60ŷϸһë Ļ86ҳ Ļ91 ɫһ¼Ƭ Ļ ɫһŷƬ ëƬƵ Ļ 7ҳ Ļÿո ëƬԿ Ļ кŷһһĺ žĿƬɫۺѸƵ60ŷϸһë кŷһһĺ ëƬΪƵžžŷһëŷƬ ɫһ¼ Ƶۿ ĻAvӰ žžžŮƵ Ļ йһػƴƬ ĻþƵ ĻɫƵ йɫһƬ ĻֻƬ Ļ ŷһë кŷһһĺ Ƶ žžƵվ Ļ ɫһƬ Ļ ĻƵ ëƬַ йһɫƬ Ļ žžƵ߲ žĿƬɫۺѸƵ60ŷϸһë кŷһһĺ žžƵ AV Ļ˿ ëƬƵ ëƬߵӰ ëƬ߲ žžž߲ ĻӰ ĻĻ Ļһ ëƬѻ ĻۺӰԺ Ļֻ ĻƵ ëƬΪƵžžŷһëŷƬ йһشƬ ĻƵ йƵ žžžƵŷ йһɫӰ žžž԰̬ͼ žžžŷƵ ɫһƬ Ļ ƬBBAVwww޹ŷ߲һ ɫһëëƬ Ƶ ɫһɫƬ Ļ׵Ӱ ɫһƬaa Ļʮҳ ΪƵ ɫһƬۿ ëƬѵӰ
ENTER NUMBET 0018