sql - Index for multiple columns in ActiveRecord -


There are two ways to declare an index for multiple columns in the Active record:

 add_index: classification, [: Species: family: trivial_names] 
 add_index: taxonomy, species add_index: taxonomy, family add_index: taxonomy,: trivial_names 

what is the first approach and second? If so, when should I first use and second when?

You are comparing a composite index to a set of independent index they are just different.

Think about it: A complex index lets you quickly see the first field in the nested set of the area, after which the second field looks fast the first field already by Within the selected records only, after the fast field view of the third field - then, only within the records selected by the previous two indexes

We take an example. If your database engine does not take more than 20 steps to detect a unique value within 1,000,000 records (if memory works), then if is using any index, it is true that Whether or not you are using a composite or independent index - but only the first area (in your example "species" though I thought that you want you family, species, and then the common name Want).

Now, suppose that there are 100,000 Milan records for this first field value. If you have only one index, any search within these records will take 100,000 steps: The reason for each record retrieved by the first index is that the second index will not be used (mostly in the database - this is a bit of simplification. ) And a brutal force match should be used.

If you have the overall index then your search is very fast because the first area of ​​the value in your second field search will be set before in this case You will not need more than 17 steps to get your first match value on field 2 in Field 100 of 100,000 on field 1 (100,000 log base 2). Therefore: Stages needed to find 3 unique records on a nested field, using a unique index of 1,000,000 records, a unique record of the first 100,000, and the second 10,000 = 20 + 17 + 14 = Receives 51 steps.

Required steps under these conditions only with independent index = 20 + 100,000 + 10,000 = 110,020 steps.

The big difference, eh?

Now, the not hybrid index is everywhere first, they are expensive on inserts and updates. Second, they are only brought to bear if you actually search in nested data (For another example, I use the time when dragging data to log in for a date for a customer). Also, if you are working with relatively small data sets, then they are not worth it.

Finally, check your database documentation. These days the databases have become very sophisticated in the ability to adjust the indices and the Database 101 scenario I mentioned above may not be for some (although I always develop it like this is just because what I know I am).


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -