Indexes in Access - when to use - modelling question (1 Viewer)

jaryszek

Registered User.
Local time
Today, 14:34
Joined
Aug 25, 2016
Messages
756
Hi,

where do you using indexes in your tables and in what situations?
When you are searching data, what does it mean?

And this can have impact also on update quries performance,
what are your best approaches?

Best,
Jacek
 

Ranman256

Well-known member
Local time
Today, 17:34
Joined
Apr 9, 2015
Messages
4,339
yes, it makes queries faster.
add one if the table has a unique identifier that you will search on.

a KEY field does this too.
like states table:
NY
TX
etc...
they are unique so when you key the field, it prevents duplicates and gets indexed.

but with people, not so easy, there no field to index except a generic non repeating #, like the autonum.
you may index LAST NAME (with repeats) to speed the search for them
index SOC SEC # but you cannot have nulls.
so its a judgement call.

I have fields I do not index, and the speed is fine, but I don't use half a million record neither.
 

jaryszek

Registered User.
Local time
Today, 14:34
Joined
Aug 25, 2016
Messages
756
thank you.

the search speed you mean select query?

Best,
Jacek
 

isladogs

MVP / VIP
Local time
Today, 22:34
Joined
Jan 14, 2017
Messages
18,209
Just to add to what I wrote in your other thread and to clarify a point in Ranman's reply.

Indexes make searches much faster. In very large tables the difference can be dramatic. Fields that are regularly searched should be indexed.
Having said that there is little point indexing fields where there are only two values such as Boolean fields or fields such as gender (if restricted to M/F) unless one of the values is much more prevalent than the other e.g Boolean field with 90%=false
Also the maximum number of indexes per table=32. Unlikely to be an issue but worth bearing in mind.

However indexes make update queries much slower as the index also needs to be updated.
Indexes also increase file size. I have one very large database of 1.6GB.
That is almost entirely made up of a single reference table of 2.6 million records and 44 fields. It has 10 indexes to improve search performance but doing that added over 200MB to the file size. The data is never updated in that table so I don't need t worry about that issue

So as Ranman stated, indexing is a judgement call
 
Last edited:

sonic8

AWF VIP
Local time
Today, 23:34
Joined
Oct 27, 2015
Messages
998
And this can have impact also on update quries performance,
Yes, correct, indexes slow down update and insert queries. However, always keep in mind that in most applications for each update/insert operation there are probably a dozen or more read operations on the data. So, adding a couple of milliseconds on top of the time for the write operation will be repaid multiple times by the time save on reads, which can be significant even with a single operation.
I did an extensive video on indexes in Access on YouTube. This will probably answer a lot of details questions about the topic.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 17:34
Joined
Oct 17, 2012
Messages
3,276
Indexes make searches much faster. In very large tables the difference can be dramatic.

As corroboration of Colin's point, I regularly work with VERY large tables. I'm talking nearly 2 billion records and over a TB in table size. Certain searches would take 20 to 30 minutes before indexing, and 2-3 seconds after indexing. That's an extreme example, but it shows the worth of indexes.

Basically, indexes trade disk space and a minor hit to action queries for massively improved performance in searches, joins, and select queries.
 

isladogs

MVP / VIP
Local time
Today, 22:34
Joined
Jan 14, 2017
Messages
18,209
Good link - I had forgotten that thread.

I've taken the liberty of making it a sticky thread so its easier to find in the future - you could say I've indexed the thread (sort of...) :D
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:34
Joined
Feb 19, 2013
Messages
16,600
An index feature that is often overlooked because it does not appear in the field parameters is ignore nulls. What this does is not populate an index where the value is null. If you don't ignore nulls then the index will be populated with a null and the pointer.

So for example you have a table with start and end date fields. Every record will have a start date, but only current records (for example) will not have the end date populated - null

The way the indexing works if looking for current records (i.e. end date is null) is to return records which are not in the end date index which should be faster than returning records in the index with null values. If looking for an end date value, the indexing does not have to wade through multiple nulls.

With regards booleans, if you want to index, use a byte field rather than a boolean field and populate either the true or the false value depending on which you will have fewer of. Make sure the default is null. OK so a bit of work to convert to use e.g. nz(bytefield,0) but improved performance on large datasets
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:34
Joined
Feb 28, 2001
Messages
27,128
A lot of discussion has appeared in this thread including a good link to CJ's article on indexing. You have a good set of responses about reasons to index or not index. I'll go "technical" on you for the purpose of providing an alternate viewpoint because you asked for guidelines on how to decide to index or not.

We do not know EXACTLY how Access builds its indexes because Microsoft has not given us any structural glimpses "inside the box." Therefore, any answer must be made in a more general context based on other similar products that have "lifted the hood" so we can see inside.

An index is conceptually a list associated with a specific table. You can have more than one index on a table and one index can include more than one field. This index is just a list of the values of the indexed field(s) and a pointer to the record having the given value(s). Where the field is marked "No Dups" then the index is a unique index and COULD be the primary key of the table, though it is not required to be. (You could have more than one candidate key.)

When you have a unique index, you can supply a value and Access will look into that index to find that value, then follow the pointer to find the right record. The speed differential comes from the idea that an index only contains two fields (unless it is a compound index). It is faster to read the two-field index than it would be to read a 50-field table.

Based on performance, we BELIEVE that a binary tree method (or something very much like that) is used to find a given value. If that is true, then the speed of the lookup will be based on the logarithm (base 2) of the number of entries in the table, rounded up.

If you have a table with 262,143 entries, the estimated number of comparisons (probes) to this table would be 18, because Log2(262,143) is just under 18. If you had no index, you would have to read (on the average) about 1/2 of the table, which would be 131K probes. Therefore, when trying to find something in the table, you would probe according to whether you had or did not have an index. And for a table that size, the difference between 18 probes and 131K probes would stand out like a sore thumb.

Another part of your question is when do you decide that you need an index? To which my best answer is, since an index is associated intimately with a table, you must know how you intend to USE the table in order to answer that question. When your table is going to be used for a LOT of lookups but it is relatively static (unchanging), you have a perfect case for an index. When the table is updated more often than used for lookups, it becomes a big question as to whether you want to bother. Because every time you update the table you have to update the index, and that can often be tricky.

The exact trade-off point is very hard to nail down. You can say that for a lookup table, an index is an easy call. For an event log table, where it is ALL update and rarely involves lookups, no index is needed. The middle ground is hard to find because it gets involved with key size and uniqueness. Shorter keys are cheaper and so favor indexing. Unique keys also favor indexing.

An earlier comment was to consider the cardinality (Colin didn't use this term). If the field is NOT unique (i.e. Dups Allowed), there is a number to consider that helps make the decision. The cardinality is the expected number (or ratio) of records returned from the table for a given value of the search key field.

Colin points out that indexing a Y/N field when you have thousands of records is a waste of space and resources. The cardinality of a Y/N field is 50% of the table (unless there is a bias in the data source). The perfect cardinality for a field to be indexed is 1 (not %), indicating that one key value returns one table value. When you are looking at cardinality values of 2-5, that isn't so bad. When the cardinality is in the thousands, you get little benefit from having an index.

When do you use indexes? When you do any of the following: Use a .FindFirst / .FindNext in a recordset. Create a JOIN between two tables using key fields. Create a query with an ORDER BY on an indexed field. Do a DLookup involving an indexed field. There are more but that should get you the idea of how often they are used.
 

Users who are viewing this thread

Top Bottom