Indexes are key for maintaining large data sets. Let me give you an overly simplified example:
dog, cat, apple, frog, elephant, + 1,000,000 more items…. (Store this in an array of DIM Words(1,000,005) AS STRING)
Now, let’s say the above is your data set, and you need it sorted alphabetically. Instead of moving the data itself, you simply create an index and use it.
3, 2, 1, 5, 4, + 1,000,000 more items…. (Store these values in an array of DIM Index(1,000,005) AS LONG)
Now, which is the first word in the list alphabetically? It’s not Words(1) — that’s “dog”.
But what about Words(Index(1))?
Index(1) holds a value of 3, making that Words(3) — “apple”. The first element of the index points to the first word alphabetically in the list!
And Words(Index(2)) is “cat”, and Words(Index(3)) is “dog”, and so on!
Your data never moves. It stays in its original order at all times. You’re just sorting an Index to the data, rather than rearranging or reordering the data itself — and that can be an extremely powerful database tool!
Think of a list of data with first name, last name, address, state, zip code, phone number….
Now, you could sort and move and reorder that data EVERY time you needed it listed in a certain order…. Your boss likes last names first. Telemarketing likes sorted by phone numbers. The mail men want it sorted by address…. An advertiser wants it sorted by zip code….
Now, since you’re maintaining a Fortune 500 database with 397,216,312 customers, are you going to want to take the time to reshuffle ALL that data every time you need it sorted by a different criteria??
NOT IF YOU WANT TO KEEP YOUR JOB!!
For companies like that, maintaining the database — without altering it and possibly corrupting it — is the Holy Grail! You *don’t* erase a 300 million customer database. You *don’t* overwrite it. You *worship* it as your God and *ONLY* add new customers to the end of it! The customer database can ONLY grow — NOTHING else!!
So then how do you sort it for all those different people and use cases?
By indexes!
DIM FirstName(1,000,000,000) AS LONG — Index sorted by first name.
DIM LastName(1,000,000,000) AS LONG — Index sorted by last name.
You want to know who the first customer is, sorted by first name? Thats CustomerInfo(FirstName(1)).
Want to know who the first customer is, sorted by last name? CustomerInfo(LastName(1)).
You reference the data by its position in the index, not by its position in the database itself.