The recipe book described a
class TreeProxy(object): ...
which implements unordered tree traversal. It is the most efficient way to
store records in a tree and retrieve them efficiently in a single query.
Anyway, if this is the bottle neck, you should definitively cache it
somehow.
On Saturday, 7 April 2012 10:42:03 UTC-5, Detectedstealth wrote:
>
> Well I know the major bottle neck in the site is the binary tree. I am
> still trying to figure out how to do this the best and most efficient. If
> this was a stand alone app in c/c++ this would all be in memory which
> wouldn't be such a problem. However how this is implemented it needs to
> query through the database to find nodes. As you can imagine as we get more
> and more members this will take much longer to run.
>
> See bellow code, binary_tree is called from a action and distributor is a
> database object IE: db(db.distributors.id == id).select().first()
>
> def binary_tree(self, original_dist, distributor, levels, counter=0,
> month=None):
>
>
> children = []
> left_count = right_count = counter
>
> target_month = month
>
> if distributor.left_id and left_count < levels:
>
> left_count += 1
> #print "left - A", distributor.left_id
>
> children.append(self.binary_tree(original_dist,
> distributor.left_id, levels, left_count, target_month))
>
>
> if distributor.right_id and right_count < levels:
>
> right_count += 1
> #print "right - B", distributor.right_id
>
> children.append(self.binary_tree(original_dist,
> distributor.right_id, levels, right_count, target_month))
>
>
> if distributor.sponsor_id == original_dist:
>
> sponsored_by_original = True
> else:
>
> sponsored_by_original = False
>
> tree = {
>
> 'id': ("B" if distributor.side else "A") + "-" +
> str(distributor.id),
>
> 'name': "<img src='/static/images/" + \
>
> ("top_tree_icon.png" if distributor.id == original_dist else
> ("sponsored_tree_icon.png" if sponsored_by_original else
> "user_tree_icon.png")) + \
>
> "' id='img-" + ("B" if distributor.side else "A") + "-" +
> str(distributor.id) + "' /><span class='idlabel'>" + \
>
> str("0"*(7-len(str(distributor.id))))+str(distributor.id) + "-" +
> ("B" if distributor.side else "A") + \
>
> "</span><div class='userinfo' id='id-" + ("B" if distributor.side
> else "A") + "-" + \
>
> str(distributor.id) + "'><ul><li class='userinfotop'><h4>" +
> distributor.display_name + "</h4><span>" + \
>
> self.T("A CV:") + "<b>" +
> (str(distributor.left_id.binary_cv_total) if distributor.left_id else str(0))
> + \
>
> "</b></span><span>" + self.T("B CV:") + "<b>" +
> (str(distributor.right_id.binary_cv_total) if distributor.right_id else
> str(0)) + \
>
> "</b></span></li><li class='userinfobottom'><p>" +
> self.T("Sponsored by me ") + (self.T("YES") if sponsored_by_original else
> self.T("NO")) + \
>
> "</p><p>" + (self.T("Has down lines") if distributor.right_id or
> distributor.left_id else self.T("Has no down lines")) + "</p><p>" + \
>
> (self.T("Disqualified ") if not distributor.quali_bonus else
> self.T("Qualified ")) + self.T("to get bonus") + "</p></li></ul></div>",
>
> 'children': children
> }
>
> return tree
>
> *# This is the biggest bottle neck of our entire application. Need a much
> better way to handle tree traversing.*
>
> def traversingTree(self, distributor, list):
>
> if distributor:
> left_node = distributor.left_id
>
> right_node = distributor.right_id
> if left_node:
>
> list.append(left_node)
> self.traversingTree(left_node,list)
>
>
> if right_node:
> list.append(right_node)
>
> self.traversingTree(right_node,list)
>
>
> def bottom_right_b(self, distributor):
>
> right_id = None
> cur_dist = distributor
> while cur_dist.right_id:
>
> right_id = cur_dist.right_id
> cur_dist = cur_dist.right_id
>
> print "Right Bottom", right_id
> return right_id
>
>
> def bottom_left_a(self, distributor):
>
> left_id = None
> cur_dist = distributor
> while cur_dist.left_id:
>
> left_id = cur_dist.left_id
> cur_dist = cur_dist.left_id
>
> print "Left Bottom", left_id
> return left_id
>
>
>
> On Sat, Apr 7, 2012 at 8:16 AM, Massimo Di Pierro <
> [email protected]> wrote:
>
>> Right....what? [changing color to red] Note to self. Never do math before
>> fully waking up.
>>
>> Correction: 2 reqs/second/server. (1M/24/60/60/6 servers). Still ok from
>> web2py prospective.
>> If all requests hit the database once: 12 reqs/second. It really depends
>> of what those requests do.
>>
>> Massimo
>>
>> On Saturday, 7 April 2012 09:33:38 UTC-5, Anthony wrote:
>>>
>>> 1M reqs/day is 7 reqs/minute.
>>>>
>>>
>>> Now we know how Massimo gets so much done -- his days are 2400 hours
>>> long. ;-)
>>>
>>
>
>
> --
> --
> Regards,
> Bruce Wade
> http://ca.linkedin.com/in/brucelwade
> http://www.wadecybertech.com
> http://www.fittraineronline.com - Fitness Personal Trainers Online
> http://www.warplydesigned.com
>
>