It is important to note that the order of the fields in the index is important.
An index is, in a sense, a search tree. If you index (B.event,B.state) then the tree will group together all records with the save "event" field, then order them by the "state" field.
If you were then to query that index for "b.state = x", the index would be of little use; The index is ordered by the "event" first.
In your example:
- filter A by it's "event" field
- join A.event to B.event
- join B.state to C.id
- Join B.hur = D.id
- Order by B.event, B.timestamp
It's important to note that the optimise will look at the statistics of your tables, and indexes, then may re-arrange the order of the joins. The result will be the same, but the order may give different performance, and the optimisers job is to try to find the best performance.
In your case I would expect B.event's order to be extremely important. Simply because that's the order of the resulting output, AND it's the field you filter by.
Next you join B.state to C.id. So having and index on C.id is good, it makes the join faster. But equally, having the B table data in a nice order may also make the join faster.
But, having an index on B.event and a separate index on B.state may yield little. The B.state index becomes next to pointless because we're using the B.event index. If you combine the two into one index (b.event then b.state) the execution plan may find a way to use the b.state part of the index.
Finally, if you put all the fields in the index, the index does get bigger, but the query may never actually need to look at the table. The information is in the index. The time taken to go from an index to the table to find the 'missing' fields is similar to that of a join. So for read performance, adding extra fields to the index can be of significant beenfit.
I'm wittering on now, but the summary is this:
- Usually, separate index on separate fields don't get used together
- For composite indexes, the order you specify the fields makes a difference
- Adding 'extra' fields to the index makes it bigger, but also can make queries faster
- The order of the execution plan matters more than the order of your query
- But the indexes you have can determine the order of the execution plan
This kind of work has no categorical answers. It is so dependent on your data it's closer to an art.
One option is to over load the tables with indexes, look at the resulting execution plan, and delete the indexes that are not necessary.
But even there a caveat applies. Because the execution plan is data (and table statistics) dependent, it is very important to have real world data in the tables. While the tables have 10' or 100s of rows, one execution plan may be fastest. But when you get millions of rows the execution plan can change, and so benefit from different indexes.