Beginners Guide: Improving Search Indexes for Better Performance
Join the DZone community and get the full member experience.Join For Free
An index is in simple terms an optimized data structure , that allows searching our original structure in a more efficient way.
As a common example, imagine any book you have. You have the main structure which is the book content. But you also normally have two extra structures, the Table of Contents and the Index at the end.
The Table of Contents is an structure that help us find chapters and sections fast by the title, so we say that the book is indexed by chapter and section title.
The Index help us find pages by particular words. So the book is also indexed by words.
Using any of these indexes will allow us to find what we need faster as without them we would need to scan the whole book until we find what we want.
Indexes help us find data fast, but as you can see from the analogy, they also add some extra space (memory) cost and writing cost as they need to maintain an extra up to date structure. Paying attention we can also see that creating a new entry in the Index needs to be ordered so it cannot be a simple insertion at the end of the index, but it needs to be in the right position.
The idea is the same for a DB or for a searching engine. We have our main data structure (which is the tables and columns of our model in case of DB and our text content in case of search engines). And if we create an index we are creating another structure that will allow fast searching on the main structure.
We’ll now make a little example in Java of how this would work for a full text search that index by words of a book. We start we a Book and a Page class
So let’s make an test example for a book with 10000 pages (I know it is a big fake book) and then try to find a word there:
We create a finder
No we create a IndexFinder based on a Hash Map.
We can see that the cost is in the construction of the index now. but the search is a O(1) operation.
Creating our testing code
And executing we get:
As we can see the indexed version executes the search in less than 1 milisecond compared to the 270 miliseconds the first one takes.
This is a very simple hashmap based index implementation, but that is the idea. Of course it is missing almost all of the functionality, but the idea is just to see that an alternative structure is used to search into the main data in a faster way.
Normally Database implementations use a B-Tree to implement their indexes instead of a Hashmap as used here. B Tree definition can be found on wikipedia here http://en.wikipedia.org/wiki/B-tree”>B-Tree . The main point is that they allow searches, inseritons, deletions in logarithimc time. And they allow efficient searches by ranges, direct, sorted, etc.
I'll try to follow this post with a little more realistic index example based on B Tree.
Published at DZone with permission of Carlo Scarioni, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.