So I was reading up on indexes and their implementation, and I stumbled upon this website that has a brief explanation of b-tree indexes:
http://20bits.com/articles/interview-questions-database-indexes/
The b-tree index makes perfect sense for indexes that are only on a single column, but let’s say I create an index with multiple columns, how then does the b-tree work? What is the value of each node in the b-tree?
For example, if I have this table:
table customer:
id number
name varchar
phone_number varchar
city varchar
and I create an index on: (id, name, city)
and then run the following query:
SELECT id, name
FROM customer
WHERE city = 'My City';
how does this query utilize the multiple column index, or does it not utilize it unless the index is created as (city, id, name) or (city, name, id) instead?
Imagine that the key is represented by a Python tuple (col1, col2, col3) … the indexing operation involves comparing
tuple_awithtuple_b… if you have don’t know which value of col1 and col2 that you are interested in, but only col3, then it would have to read the whole index (“full index scan”), which is not as efficient.If you have an index on (col1, col2, col3), then you can expect that any RDBMS will use the index (in a direct manner) when the WHERE clause contains reference to (1) all 3 columns (2) both col1 and col2 (3) only col1.
Otherwise (e.g. only col3 in the WHERE clause), either the RDBMS will not use that index at all (e.g. SQLite), or will do a full index scan (e.g. Oracle) [if no other index is better].
In your specific example, presuming that id is a unique identifier of a customer, it is pointless to have it appear in an index (other than the index that your DBMS should set up for a primary key or column noted as UNIQUE).