The major advantage of indexing is that it provides fast access to tables of data. For example, a table of employee records might be represented as shown below in order to gain fast access to the records by employee name:
% employee(LastName,FirstNames,Department,Salary,DateOfBirth) employee('Smith', ['John'], sales, 20000, 1-1-59). employee('Jones', ['Mary'], engineering, 30000, 5-28-56). ...
If fast access to the data via department is also desired, the data can be organized little differently. The employee records can be indexed by some unique identifier, such as employee number, and additional tables can be created to facilitate access to this table, as shown in the example below. For example,
% employee(Id,LastName,FirstNames,Department,Salary,DateOfBirth) employee(1000000, 'Smith', ['John'], sales, 20000, 1-1-59). employee(1000020, 'Jones', ['Mary'], engineering, 30000, 5-28-56). ... % employee_name(LastName,EmpId) employee_name('Smith', 1000000). employee_name('Jones', 1000020). ... % department_member(Department,EmpId) department_member(sales, 1000000). department_member(engineering, 1000020). ...
Indexing would now allow fast access to the records of every employee named Smith, and these could then be backtracked through looking for John Smith. For example:
| ?- employee_name('Smith', Id), employee(Id, 'Smith', ['John'], Dept, Sal, DoB).
Similarly, all the members of the engineering department born since 1965 could be efficiently found like this:
| ?- department_member(engineering, Id), employee(Id, LN, FN, engineering, _, M-D-Y), Y > 65.