Can one have more than one secondary index on a mnesia table?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can one have more than one secondary index on a mnesia table?

Lloyd R. Prentice-2
Hello,

Initialized table:

init_table() ->
    mnesia:create_table(?TABLE,
        [ {disc_copies, [node()] },
          {attributes, record_info(fields, ?TABLE)},
          {index, [member_id, penname]}
        ]).

Checked info:

$ wg_schema(wg_table).
....
{index,3}            -> 376866
{index,5}            -> 380964

This query works:

get_author_by_member_id(MemberID) ->
    Query =
        fun() ->
            mnesia:index_read(?TABLE, MemberID, 3)
        end,
    {atomic, Result} = mnesia:transaction(Query),
    Result.

But this doesn't:

get_author_by_penname(Penname) ->
    Query =
        fun() ->
            mnesia:index_read({?TABLE, Penname, 5})
        end,
    {atomic, Result} = mnesia:transaction(Query),
    {length(Result), Result}.

Am I doing something wrong? Or is there something I should be doing altogether?

Thanks to all,

LRP




*********************************************
My books:

THE GOSPEL OF ASHES
http://thegospelofashes.com

Strength is not enough. Do they have the courage
and the cunning? Can they survive long enough to
save the lives of millions?  

FREEIN' PANCHO
http://freeinpancho.com

A community of misfits help a troubled boy find his way

AYA TAKEO
http://ayatakeo.com

Star-crossed love, war and power in an alternative
universe

Available through Amazon or by request from your
favorite bookstore


**********************************************

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Can one have more than one secondary index on a mnesia table?

Dan Gudmundsson-3
Remove the tuple in the second call:

mnesia:index_read(?TABLE, MemberID, 3) 
vs
mnesia:index_read({?TABLE, Penname, 5})

/Dan

On Sun, Oct 22, 2017 at 9:15 PM <[hidden email]> wrote:
Hello,

Initialized table:

init_table() ->
    mnesia:create_table(?TABLE,
        [ {disc_copies, [node()] },
          {attributes, record_info(fields, ?TABLE)},
          {index, [member_id, penname]}
        ]).

Checked info:

$ wg_schema(wg_table).
....
{index,3}            -> 376866
{index,5}            -> 380964

This query works:

get_author_by_member_id(MemberID) ->
    Query =
        fun() ->
            mnesia:index_read(?TABLE, MemberID, 3)
        end,
    {atomic, Result} = mnesia:transaction(Query),
    Result.

But this doesn't:

get_author_by_penname(Penname) ->
    Query =
        fun() ->
            mnesia:index_read({?TABLE, Penname, 5})
        end,
    {atomic, Result} = mnesia:transaction(Query),
    {length(Result), Result}.

Am I doing something wrong? Or is there something I should be doing altogether?

Thanks to all,

LRP




*********************************************
My books:

THE GOSPEL OF ASHES
http://thegospelofashes.com

Strength is not enough. Do they have the courage
and the cunning? Can they survive long enough to
save the lives of millions?

FREEIN' PANCHO
http://freeinpancho.com

A community of misfits help a troubled boy find his way

AYA TAKEO
http://ayatakeo.com

Star-crossed love, war and power in an alternative
universe

Available through Amazon or by request from your
favorite bookstore


**********************************************

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Can one have more than one secondary index on a mnesia table?

Martin Karlsson-3
In reply to this post by Lloyd R. Prentice-2
>Am I doing something wrong? Or is there something I should be doing altogether?

Your second index_read is not correct:

>mnesia:index_read({?TABLE, Penname, 5})

Should be:
mnesia:index_read/3 i.e

mnesia:index_read(?TABLE, Penname, 5).

I also tend to use the names rather than index position for the queries.

mnesia:index_read(?TABLE, Penname, #yourrecord.penname).

Cheers,
Martin
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions