The official vignettes are the best introduction to this topic:
A data.table can be "keyed" by a sequence of columns, telling interested functions that the data is sorted by those columns. To get or set the key, use the functions documented at
Similarly, functions can take advantage of a data.table's "indices." Each index -- and a table can have more than one -- stores information about the order of the data with respect a sequence of columns. Like a key, an index can speed up certain tasks. To get or set indices, use the functions documented at
Indices may also be set automatically (currently only for a single column at a time). See
?datatable.optimize for details on how this works and how to disable it if necessary.
Missing values are allowed in a key column.
Keys and indices are stored as attributes and may, by accident, not correspond to the actual order of data in the table. Many functions check the validity of the key or index before using it, but it's worth keeping in mind.
Keys and indices are removed after updates where it's not obvious that sort order is preserved. For example, starting from
DT = data.table(a=c(1,2,4), key="a"), if we update like
DT[2, a := 3], the key is broken.