2 ms·
Seems like a reasonable question. Here's my answer: Let's say you have a database table with two columns. One is an ID, and the other is a random integer. Y
by cbanek 9y ago
Seems like a reasonable question. Here's my answer:
Let's say you have a database table with two columns. One is an ID, and the other is a random integer. You want to be able to go through and find a particular integer by value, such as SELECT id,randomint from t WHERE randomint=4 (4 gotten by xkcd random dice roll, of course).
Now, if you had just a list of these integers, and they weren't sorted you'd have to scan through them all one at a time. This is called a table scan, and it can be slow.
Imagine if you had another list, that had the same values from the table, but they were sorted in the order of the values, rather than table order. Now you'd be able to do all the sorted array tricks, such as binary search, starting from the beginning, starting from the end, etc. This means you can find it faster. The index can also be smaller than the table, because you only have to store the parts you are indexing. But of course, there are performance tradeoffs, because you are increasing the size on disk, and it takes time to modify the index when modifying the table. (This is why some DB upgrades drop indexes then add them back after doing a lot of activity)