2

When I try to use a query in Google App Engine's datastore like this:

user = User.all().filter('name =',userName).get()

I have this error: DeadlineExceededError.

EDIT: This is my function:

def feeds(request):
    dict = get_user_details()
    search_key = None
    if request.GET.has_key('search_key'):
        search_key = request.GET['search_key']
        dict['search_key']=search_key
    feeds = list()
    if search_key is not None and search_key!="":
        feeds_list = Feed.all().order('-CreatedDate')
        search_string=string.upper(search_key)
        for feed in feeds_list:
            feed_name=string.upper(feed.FeedUrl)
            if search_string in feed_name:
                feeds.append(feed)
            dict['search_key']=search_key
    else:
        feeds = Feed.all().order('-CreatedDate')
    if request.GET.has_key('page'):
        page = request.GET['page']
    try:
        page = int(page) - 1
    except:
        page = 0

    paginator = Paginator(feeds,10)
    if page>=paginator._get_num_pages():
        page = paginator._get_num_pages() - 1

    dict["page_obj"]     = paginator.page(page+1)
    return render_to_response('feed_list.html', dict, context_instance=RequestContext(request))

Searching on Feeds, it takes too much time, raising a DeadlineExceededError.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
linhtruong
  • 49
  • 1
  • 3

1 Answers1

1

This won't scale:

    feeds_list = Feed.all().order('-CreatedDate')
    search_string=string.upper(search_key)
    for feed in feeds_list:
        feed_name=string.upper(feed.FeedUrl)
        if search_string in feed_name:
            feeds.append(feed)

You're pulling every single Feed object back from the datastore and then locally filtering the list based on a search term. If you only want to show 10 records per page, do a fetch(10) on your query object so you're not trying to pull back every entity in one page. Use query cursors for pagination. If you want to filter a large set of entities, you need to pre-define a datastore index that will satisfy the query; fetching everything and filtering locally is prohibitively inefficient.

Drew Sears
  • 12,812
  • 1
  • 32
  • 41
  • He's basically performing a dreaded "table scan". I don't think a `fetch(10)` could solve the problem since he has to read all the entities to find the correct match. I would go with something like this http://code.google.com/p/gae-text-search/ getting rid of the pagination feature. – systempuntoout Apr 26 '11 at 21:19