Find the maximum / minimum value of a column in Mnesia in constant time

How can I find the maximum or minimum value in the indexed column in the Mnesia table in constant time (or as much as possible)?

+4
source share
2 answers

I would do this outside the Mnesia database. Keep explicit-min and explicit-max, having a process that finds out about these values ​​whenever there is an insert in the table. This gives you a very fast constant search on values.

If you can do this with an O (lg n) time, you can make the table ordered_set. From there, first/1 and last/1 should give you what you want, given that the key contains what you order. But it also slows down other queries in general to O (log n).

The third trick is an approximation. From time to time you look at the table and note the values ​​of max and min. Then it materializes into what you want, but the value may not correspond to the date if a lot of time has passed since the last scan.

+4
source
Good question, but I don’t think it is possible. A quick look at the mnesia and qlc documentation did not give me any hints on this.

It seems to me that the secondary key facility in mnesia is incomplete and therefore very limited in function. Not to mention the terrible moments of starting mnesia when loading indexed tables.

I think the most reliable solution in your case would be to do explicit indexing. For instance. Creating and maintaining synchronization along with the table, sorted by primary keys, which are actually the values ​​that you would like to index.

+1
source

Source: https://habr.com/ru/post/1432298/


All Articles