i'm running into a bit of speed problems with my little project i'm working on, and I'm confused about indexing and sorting.
First off, my site is in flask (python), and i'm using pymongo to interact with the db. My db has been growing, and now i'm starting to run into performance issues.
My collection is 991.2 KB and when I pull it from into db, i've just been sorting it in the view function. I realize now I should probably be sorting it at the DB level, and probably creating an index, but i have a few really dumb questions about what I should be doing.
Is it even worth creating a new sorted index? The DB is a wiki, so people add stuff to it not infrequently, often multiple times per day. Not sure how that affects having it indexed.
How exactly do I create this index if i want to implement it? I know it should be something like this:
collection.create_index([('field_i_want_to_index', pymongo.TEXT)], name='search_index', default_language='english')
- via stackoverflow
but i'm not sure when and how frequently I should run that line of code? Just once I assume?
Any advice would be helpful. I'm a bit out of my depth here even though I've been working with mongoDB for like 2 years now.
I have just been returning the standard index and sorting it in flask. It works fine, but as my database grows, it seems like an unnecessary step.
Here are two queries that are on my index page, I added the bottom one today:
@cache.memoize(timeout=CACHE_TIMEOUT)
def all_collection(collection_name):
return list(db()[collection_name].find({}))
@cache.memoize(timeout=CACHE_TIMEOUT)
def all_courses_alphabetical():
return list( db()['course'].find({}, sort=[('name', 1)]) )