Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be using LINQ queries on RavenDB?

Tags:

c#

linq

ravendb

I'm learning RavenDB and I am rather confused. As far as I understand, one should create indexes in order to have really efficient queries. However, it is possible to simply make LINQ queries, such as

using(IDocumentSession session = _store.OpenSession())
{
    MyDocument doc = session.Query<MyDocument>()
                            .Where(d => d.Property == value)
                            .Single();
}

This type of query works perfectly fine. I have, however, never created an index for it (and never reference an index when making the query, of course).

Should I be using this kind of query when working with RavenDB? If not, why is it even available in the API?

like image 696
David Nordvall Avatar asked Dec 06 '11 09:12

David Nordvall


3 Answers

There's two things you are asking, here.

  1. Can we use Indexes .. which are suppose to be more efficient than dynamic queries?
  2. If we use indexes .. then should we use Linq and chaining?

Indexes

As Matt Warren correctly said, you're not using any indexes in your sample query. Right now, with your sample query, RavenDb is smart enough to create a temp (dynamic) index. If that dynamic index is used enough, it get auto-promoted to a static / perminent index.

So .. should you use indexes? If you can, then yeah!

here's your statement again, this time with an Index defined.

using(IDocumentSession session = _store.OpenSession())
{
    MyDocument doc = session.Query<MyDocument>("ByProperty")
                            .Where(d => d.Property == value)
                            .Single();
}

In this case an index called MyDocument_ByProperty was created somewhere. I'm not going to explain the details of indexes .. go and read all about them here.

Linq and chaining

(Not sure if that is the correct terminology ... )

If you create a linq statement (which I did above) with OR without an index .. a query is still generated .. which then is translated into an HTTP RESTful request to the RavenDB Server. If you have an index .. then the query is smart enough to ask to use that. None? Then the server will create a dynamic index .. which means it will also have to go through the motions of indexing first, then retrieving your results.

TL;DR;

Yes use indexes. Yes use Linq chaining.

like image 58
Pure.Krome Avatar answered Oct 21 '22 20:10

Pure.Krome


RavenDb comes with a native support for .Net and Linq.

The Linq provider, under the hood, does normal REST calls to the ravendb server, but for you it's easier to code on it since you can use IQueryable<T> with strongly typed classes.

So yes, you can and you should use linq/lambda to work with RavenDB in a .Net envorinment.

like image 33
Matteo Mosca Avatar answered Oct 21 '22 21:10

Matteo Mosca


Something to be aware of that caught me out is that if you include a linq statement such as .Where(d => d.SomeProperty == null) then you might expect that if the document does not have the property then you would return a match. However this is not the case. If the document does not have the property then its value is not considered to be null (or any other value).

like image 1
naskew Avatar answered Oct 21 '22 21:10

naskew