Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:47:53+00:00 2026-05-14T23:47:53+00:00

OK this is a 2 part question, I’ve seen and searched for several methods

  • 0

OK this is a 2 part question, I’ve seen and searched for several methods to get a list of unique values for a class and haven’t been practically happy with any method so far.
So anyone have a simple example code of getting unique values for instance for this code. Here is my super slow example.

class LinkRating2(db.Model):
    user = db.StringProperty()
    link = db.StringProperty()
    rating2 = db.FloatProperty()

def uniqueLinkGet(tabl):
    start = time.time()
    dic = {}
    query = tabl.all()
    for obj in query:
        dic[obj.link]=1
    end = time.time()
    print end-start
    return dic

My second question is calling for instance an iterator instead of fetch slower? Is there a faster method to do this code below? Especially if the number of elements called be larger than 1000?

query = LinkRating2.all()
link1 = 'some random string'
a = query.filter('link = ', link1)
adic ={}
for itema in a:
    adic[itema.user]=itema.rating2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:47:54+00:00Added an answer on May 14, 2026 at 11:47 pm

    1) One trick to make this query fast is to denormalize your data. Specifically, create another model which simply stores a link as the key. Then you can get a list of unique links by simply reading everything in that table. Assuming that you have many LinkRating2 entities for each link, then this will save you a lot of time. Example:

    class Link(db.Model):
        pass  # the only data in this model will be stored in its key
    
    # Whenever a link is added, you can try to add it to the datastore.  If it already
    # exists, then this is functionally a no-op - it will just overwrite the old copy of
    # the same link.  Using link as the key_name ensures there will be no duplicates.
    Link(key_name=link).put()
    
    # Get all the unique links by simply retrieving all of its entities and extracting
    # the link field.  You'll need to use cursors if you have >1,000 entities.
    unique_links = [x.key().name() for Link.all().fetch(1000)]
    

    Another idea: If you need to do this query frequently, then keep a copy of the results in memcache so you don’t have to read all of this data from the datastore all the time. A single memcache entry can only store 1MB of data, so you may have to split your links data into chunks to store it in memcache.

    2) It is faster to use fetch() instead of using the iterator. The iterator causes entities to be fetched in “small batches” – each “small batch” results in a round-trip to the datastore to get more data. If you use fetch(), then you’ll get all the data at once with just one round-trip to the datastore. In short, use fetch() if you know you are going to need lots of results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is a part algorithm-logic question (how to do it), part implementation question (how
This is a two part question. A dumb technical query and a broader query
I think this is a multi-part question, so bear with me. Currently all of
Note : The code in this question is part of deSleeper if you want
I ask this question in anticipation as part of a project. I have experience
This is part observation, part question. First the observation: While everyone talks of modular
I apologize. This is part rant, part question. For the rant: Dear MS developers
This is a two part question: Part 1 First, dealing with calculating the entropy
Yesterday I ran into an Issue while developing a Web Part (This question is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.