Ensure index and performance

246 views
Skip to first unread message

Joshua Partogi

unread,
Aug 8, 2010, 12:21:10 AM8/8/10
to mongod...@googlegroups.com
Hi all,

Can somebody explain how does index works in mongo? From what I read
we are to call ensureIndex method in our code instead of creating an
index one time like in RDBMS. CMIIW.

My question is:
1. Do we call ensureIndex every time we make a request to the database?
2. Would calling this on every request slows down the performance of
the web request?

Thanks heaps for your kind assistance.

Kind regards,
Joshua.

--
http://twitter.com/scrum8

Scott Hernandez

unread,
Aug 8, 2010, 12:27:38 AM8/8/10
to mongod...@googlegroups.com
You are welcome to do it either in your code or in the javascript shell. Since there is no schema there is no way to define it as part of your schema. You can create an index before you store (insert) data though, if you like.

I ensure my indexes when my application starts (each time); basically whenever I change my code.

Calling ensureIndex for an existing index will do nothing, so there is no harm in doing it periodically.

I would not suggest doing it before each query.

HTH

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Andreas Jung

unread,
Aug 8, 2010, 4:30:12 AM8/8/10
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Joshua Partogi wrote:
> Hi all,
>
> Can somebody explain how does index works in mongo? From what I read
> we are to call ensureIndex method in our code instead of creating an
> index one time like in RDBMS.

Sorry, this is nonsense. Indexes basically work the same way as in a
RDBMS: you create them once and that's it. As with an index in a RDBMS:
you can create and drop and re-create them at runtime. The only slightly
difference is perhaps that indexes in MongoDB can be created in background.

- -aj

CMIIW.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkxeaxQACgkQCJIWIbr9KYxmbACgunc44L7m+NRFCJa66oKoOSP5
wKwAoOAadRcNk7QsiOe30PbAMhJmOBTv
=9iVe
-----END PGP SIGNATURE-----

lists.vcf

Joshua Partogi

unread,
Aug 9, 2010, 12:08:15 AM8/9/10
to mongodb-user
Thanks for sharing your experience Scott. I don't know how indexes
work in mongo so your explanation is really worth it.

On Aug 8, 2:27 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> You are welcome to do it either in your code or in the javascript shell.
> Since there is no schema there is no way to define it as part of your
> schema. You can create an index before you store (insert) data though, if
> you like.
>
> I ensure my indexes when my application starts (each time); basically
> whenever I change my code.
>
> Calling ensureIndex for an existing index will do nothing, so there is no
> harm in doing it periodically.
>
> I would not suggest doing it before each query.
>
> HTH
> On Sat, Aug 7, 2010 at 9:21 PM, Joshua Partogi <joshua.part...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Hi all,
>
> > Can somebody explain how does index works in mongo? From what I read
> > we are to call ensureIndex method in our code instead of creating an
> > index one time like in RDBMS. CMIIW.
>
> > My question is:
> > 1. Do we call ensureIndex every time we make a request to the database?
> > 2. Would calling this on every request slows down the performance of
> > the web request?
>
> > Thanks heaps for your kind assistance.
>
> > Kind regards,
> > Joshua.
>
> > --
> >http://twitter.com/scrum8
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages