Mnesia secondary indexes and selects

Leonard B leonard.boyce@REDACTED
Thu Jun 18 17:03:28 CEST 2020


While working on understanding secondary indexes in mnesia to add
support for them in another project I was a bit surprised to see that
secondary indexes are not used in 'select' operations unless the value
is bound in the head pattern.

Hopefully someone with deeper knowledge can either tell me I'm wrong,
or explain why that is the case.

For example:
```
1> mnesia:create_schema([node()]).
ok
2> mnesia:start().
ok
3> rd(test, {i, v}).
test
4> mnesia:create_table(test,
[{disc_copies,[node()]},{type,ordered_set},{record_name,
test},{attributes,record_info(fields,test)}, {index, [#test.v]}]).
{atomic,ok}
5>
5> mnesia:dirty_write(test, #test{i = 1, v = one}).
ok
6> mnesia:dirty_write(test, #test{i = 2, v = two}).
ok
7> mnesia:dirty_write(test, #test{i = 3, v = three}).
ok
8>
8> mnesia:dirty_select(test, [{#test{v = two, _ = '_'},[],['$_']}]).
remote_dirty_select USING index
[#test{i = 2,v = two}]
9> mnesia:dirty_select(test, [{#test{v = '$1', _ = '_'},[{'=:=', '$1',
two}],['$_']}]).
remote_dirty_select NOT using index
[#test{i = 2,v = two}]

```

Was there some historical reason for this?

Also curious if there has ever been any attempt to write a planner to
take advantage of secondary indexes.

Thanks,
Leonard


More information about the erlang-questions mailing list