banner



What Happens To The Original Data In Database Indexing

The championship of this post is pretty foreign, I admit. Usually, when we think about modeling, we think about our data. If it is a relational database, this generally means the structure of your tables and the relations between them. When using a document database, this ways the shape of your documents. Only in both cases, indexes are at that place merely to speed things up. Oh, a detail important query may need an index, and that may impact how you lay out the data, merely these are relatively rare cases. In relational databases and nearly non-relational ones, indexes do not play whatever major role in information modeling decisions.image

This isn't the instance for RavenDB. In RavenDB, an alphabetize doesn't be only to organize the information in a way that get in easier for the database to search for information technology. An index is actually able to modify and transform the information on the current document or full related data from related documents. A map/reduce alphabetize is even able to aggregate information from multiple documents as part of the indexing process. I'll bear on on the final one in more depth afterwards in this series. Commencement, let'due south tackle the more obvious parts because I want to show off some of the new features. I'g going to use JS for most of the indexes definitions in these posts, but you tin practise the same using Linq/C# as well, obviously.

When brainstorming for this post, I got so many skilful ideas about the kind of non-obvious things that you can practise with RavenDB'due south indexes that a single post has transformed into a series, and I got two pages of notes to go through. Almost all of those ideas are basically some form of computation during indexing simply applied in novel manners to requite you a lot of flexibility and power.

RavenDB prefers to accept more work to do during indexing (which is batched and happen on the groundwork) than during query time. This means that nosotros can push a lot more than piece of work into the background and just let RavenDB handle it for us. Let's start from what is probably the virtually basic example of computation during query, the Club'south Total. Consider the following document:

image

As yous tin can see, we take the Gild document and the listing of the line items in this gild. What nosotros don't accept here is the total social club value.

Now, actually computing how much you are going to pay for an social club is circuitous. You have to take into account taxation, promotions, discounts, aircraft costs, etc. That isn't something that you can do trivially, but it does serve to brand an excellent simple example and like requirements be in many fields.

We can manifestly add a Totalfield to the social club, simply and so nosotros have to make sure that we update it whenever we update the society. This is possible, but if we have multiple clients consuming the data, this can be fairly difficult to do. Instead, we tin place the logic to compute the property in the index itself. Here is how it would look like:

image

The same index in JavaScript is almost identical:

          map("Orders", society => {     return {         Employee: order.Employee,         Company: social club.Visitor,         Total: order.Lines.reduce(50 => (l.Quantity * 50.PricePerUnit) * (1 - l.Discount) )     }; });        

In this case, they are very similar, but as the complexity grows, I find it is usually easier to express logic equally a JavaScript alphabetize rather than utilize a single (complex) Linq expression.

Such an index give the states a computed field, Total, that has the total value of an order. We can query on this field, sort it, and even project information technology dorsum (if this field is stored). It allows u.s.a. to e'er have the most up to engagement value and have the database accept care of computing it.

This basic technique can be applied in many different ways and affect the way we shape and model our data. Currently, I have at least 3 more than posts planned for this serial, and I would love to hear your feedback. Both on the kind of stuff you would like me to talk about and the kind of indexes yous are using RavenDB and how information technology impacted your data modeling.

What Happens To The Original Data In Database Indexing,

Source: https://dzone.com/articles/data-modeling-with-indexes-introduction

Posted by: westwelition.blogspot.com

0 Response to "What Happens To The Original Data In Database Indexing"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel