I have a table:
id:int
revision:int
text:ntext
In general I will need to retrieve the latest revision of text for a particular id, or (considerably less frequently) add a new row containing a new revision for a particular id. Bearing this in mind, it would be a good idea to put indexes on the id and revision columns. I don’t have a problem with implementing this, but I’m wondering if this is a situation where it would be sensible to use a composite (multi-field) index/key composed of both id and revision, or if there is any other strategy that would be appropriate for my use case?
I don’t think the performance difference between a composite index and two separate indexes would be noticeable, but, as usual, I suggest trying both and profiling if the absolute best performance is needed.
You are likely to always be querying on both fields, with a definite
idand an unknownrevisionoccasionally (when needing to find the maxrevisionfor anid). If your composite index is(id,revision)then this use case is supported by the index. Querying onidalone with no care forrevisionalso works.If it is ever likely that you will be querying on
revisiononly without regard toidthen you will need two separate indexes.You will also want to analyze the impact that either index has on
insertperformance. The composite index will cluster on both fields, whereas the two separate indexes will cluster only onid.EDIT: typos.