Mongo DB indexes

#1

It would be also good to know how fast parse server is. For example if I have a million objects in same class and I want to do a query, how fast will parse server respond?

Who's using Parse in production?
#2

It really depends on many different things:

  • What you have stored in these objects;
  • What indexes you have created;
  • What query you are running and how many objects you are trying to retrieve;
  • What kind of cache you are using;
  • How is your setup for database and Parse Server.

I’d say that Parse Server allows you to build your application to be as fast as you want/need. But it really depends on you to create your app’s architecture in order to have the best performance.

#3

For example if my app has 10000 user and everyone follows everyone. That means I have 10000^2=100,000,000 “Follow” object.

This is my follow object. And official android guide handle this way.

ParseObject follow = new ParseObject(“Follow”);
follow.put(“follower” , ParseUser.getCurrentUser());
follow.put(“following” , otherUser);
follow.saveInBackground();

And this is how i check if i follow other user

ParseQuery followCheck = new ParseQuery(“Follow”);
followCheck.whereEqualTo(“follower”,ParseUser.getCurrentUser());
followCheck.whereEqualTo(“following”,otherUser);
followCheck.getFirstInBackground(new GetCallback() {
@Override
public void done(ParseObject object, ParseException e) {

}

});

What do you think about response time with enough cpu resource?

#4

If you create an index to the Follow table containing fields { follower: 1, following: 1 }, it should be few milliseconds. The performance for this case will be more likely related to your MongoDB setup and how many of these queries you are doing at the same time than to the Parse Server itself.

#5

But with that code I’m not getting how many followers i have. I already keep follower and following number in user. And increase decrese it with increment method.What I’m doing there is checking if I follow that person. Is there any way to do same job with using less resources. I really want to know.

#6

The way you are doing for knowing if someone follows someone else seems good to me. Just create the index I told you and you should be good.

#7

I couldn’t understand what index I should create. Can you give me an example code?

#8

Where are you running your database? Are you using MongoDB or Postgres?

#9

I’m using MongoDB. I deployed with deploy with heroku button. parse server hosted in heroku and MongoDB hosted in MLab. Currently I develop my mobile app. so its just a development database.

#10

I think MLab has a tool you can create the index visually. Otherwise, connect to your mongodb database and try this:

db.Follow.createIndex({ follower: 1, following: 1 })

See reference here:
https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/

You can also create the index using the Parse JS SDK:

#11

Sorry I’m trying to understand the logic behind the creating indexes. Also my english is not so well but so far I understand normally MongoDB writes data randomly. But If we create index like your example like this

db.Follow.createIndex({ follower: 1, following: 1 })

then MongoDB will write data ascending order which is good for query. Am I right?

I Understand you better know. In your previous message you said

“If you create an index to the Follow table containing fields { follower: 1, following: 1 }, it should be few milliseconds.”

I thought you were suggesting to keep following numbers. I’m sorry I’m really new to this Database thing.

#12

Yes. MongoDB will keep your records in this collection organized by this index (like in a book index) and your query will run much faster even with a huge number of objects.

#13

Should I use same index to every collection. I have a Post object like this

{
    "_id": "aOmSp00ncz",

    "commentnumber": 0,

    "_p_group": "Group$jgfhsgulAt",

    "description": "Ucjgjjg",

    "_p_user": "_User$AQysGkzw4O",

    "type": "text",

    "likenumber": 0,

    "_created_at": {
        "$date": "2019-05-06T18:54:58.611Z"
    },

    "_updated_at": {
        "$date": "2019-05-06T18:54:58.611Z"
    }
}

I use Parse Query to retrieve related Posts. Like this

ParseQuery<ParseObject> benblock= new ParseQuery<ParseObject>("Block");
benblock.whereEqualTo("owner",ParseUser.getCurrentUser());

ParseQuery<ParseObject> oblock = new ParseQuery<ParseObject>("Block");
oblock.whereEqualTo("who",ParseUser.getCurrentUser());

ParseQuery<Post> getposts = new ParseQuery<Post>(Post.class);
getposts.whereDoesNotMatchKeyInQuery("user","who",benblock);
getposts.whereDoesNotMatchKeyInQuery("user","owner",oblock);
getposts.whereExists("group");
getposts.include("group");
getposts.whereEqualTo("group",group[0]);
getposts.include("user");
getposts.whereEqualTo("user",ParseUser.getCurrentUser());
getposts.orderByDescending("createdAt");

will it be good to create index like this

db.Post.createIndex({ description: 1, type: 1, group: 1, user: 1, createdAt: 1 })

In which conditions I should use “1”. I saw another parameter that “-1”

#14

That’s the idea. But you don’t need to create indexes including all your fields. You just need to include the fields you are using in the query. For every different query you have, you should think about the indexes and see if you have an appropriate index for that query. -1 can be used to order it descending. For example, in your query above, you should include createdAt: -1, since you are using this field to order descending. But again, I think that mlab has a visual tool that even suggests the index you need to create based on the queries you are doing. You can find other tools in Internet about this and some of Parse hosting platforms also have their own automatic index optimization tools. If you want to learn more about indexes, I suggest you look for MongoDB Indexes material at internet. I think this discussion here is already completely out of scope for this thread.

1 Like
#15

Thank you for your all replies. I really learned a lot from you. Yes You are right. We need to end this discussion.

For other people who wanna learn indexes in MongoDB beside official MongoDB tutorials there is Mlab tutorials .

https://docs.mlab.com/indexing/#performing-efficient-or-queries

Have a nice day @davimacedo

1 Like
#16

This topic has been split from ‘Who’s using Parse in production?’ due to it being off topic.

1 Like
#17

Hi. I created indexes via console with this code:

db.Post.createIndex({ user: 1, createdAt: -1, group:1 ,type:1})

This is result:

{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1,
	"operationTime" : Timestamp(1557650736, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1557650736, 1),
		"signature" : {
			"hash" : BinData(0,"lkcl+/1fByBxibDrRyjOhPyywbg="),
			"keyId" : NumberLong("6662483596042829825")
		}
	}
}

And in my mLab console i see my index. But when i write new item I see my item at the end of the collection like before index. Is this normal? I thought with index mongodb write data organized. What is the expected behaviour? Are Indexes working good but mLab console show data differently or something else?

Edit: When i save object for example user in post I use “user” key name. But in mongo db it shown like “_p_user” Should I use “user” or “_p_user” when creating index?

also
“createdAt” become “_created_at”
“group” become “_p_group”

Edit2: I gues ı should _p_user key. And I think you cant see index in action and mongo db writes data normally but in background also changing something in index am I right?

But should I create index all in one or separately?

For example:
db.Post.createIndex({ user: 1, createdAt: -1, group:1 ,type:1})

or
db.Post.createIndex({ _p_user: 1})
db.Post.createIndex({_created_at: -1})
db.Post.createIndex({_p_group: 1})
db.Post.createIndex({type: 1})

which one should I use?

#18

I understand you have to main questions.

  1. Should I create the index using _p_user or user, for example?
    You should create with the same name you see in your mongo (i.e. _p_user) since you are creating directly in the database.

  2. Should mongo store the data differently after the index?
    No. You will see the data in the same way you used to do. But when you perform some query, behind the scenes, mongo will use the index that will turn out the best performance.

1 Like