sheepduke

sheepduke

Mnesia 100x slower to query compared with SQLite?

Hi folks,

I just started to explore the Mnesia database. I read many docs, posts and resources provided by Elixir forum, but still could not found a solution:

My Mnesia is 100x times slower to query (scan) compared with Ecto/SQLite3.

Initialization

The following code shows how Mnesia was initialized:

defmodule Data.Test do
  use Memento.Table,
    attributes: [
      :uuid,
      :name
    ],
    index: [:name]
end

Memento.stop()
Memento.Schema.create([node()])
Memento.start()

Memento.Table.create!(Data.Test, disc_copies: nodes)

Memento.transaction!(fn ->
  Enum.each(1..100_000, fn i ->
    Memento.Query.write(%Data.Test{
      uuid: Ecto.UUID.generate(),
      name: "Test #{i}"
    })
  end)
end)

Then I created an exactly same table in SQLite3, without index.

Query

The following code shows the query process:

:timer.tc(fn ->
  Memento.transaction!(fn ->
    Memento.Query.all(Data.Test)
    |> Stream.filter(&String.contains?(&1.name, "12345"))
    |> Enum.to_list()
  end)
end)

Test.Repo.query("select * from test where name like '%12345%'")

The result was about 1600 ms and 16 ms respectively.

I replaced the Stream in Mnesia query with :mnesia.foldl (without Memento) and the result was almost identical (1400 ms maybe, still ~100x times).

Question

My understanding is that SQL like '%xxx%' leads to a full table scan, which is same as Mnesia.

Since Mnesia stores the data in memory, why is the table scan so slow? Am I miss anything?

Any clue is sincerely appreciated!

Marked As Solved

Rustixir

Rustixir

Hi , I working with mnesia for distributed read-heavy data application for three last year.

Mnesia is a distributed, concurrency DBMS but Sqlite is a embedded database.

for real-project if you need high concurrency access to Database with Sqlite become bottleneck .

  1. Mnesia intenal use ETS for disc_copies and ram_copies
    and that have many parameter for tune maximize performance

  2. if you want for a single node/machine and maximize performance use ETS

  3. if you want use mnesia use itself module → :mnesia

  4. for increase performance for read/query use :mnesia.activity with <:async_dirty > parameter

  5. if you want use mnesia with maximize performance for read/query use
    :mnesia.activity with < :ets > parameter

Also Liked

cmkarlsson

cmkarlsson

mnesia and ets are key/value database. It is best used when you have a specific value to retrieve.

That said, if you can structure your object in a way that :mnesia.select can be used then it is much faster. Mnesia select relies on match specifications (Erlang -- Match Specifications in Erlang) which are a bit gnarly to get your head around but very useful, not only in mnesia but also in ets and if you are doing tracing.

Match specifications do not support matching on parts of binary strings so your specific example would not work as is, but they are a faster way to get information out of mnesia and ets compared to foldl or iterating the table.

I see that Memento also support select (a simplified version of erlang’s matchspecs) and select_raw with the full power of select. Perhaps it is worth investigating.

Exadra37

Exadra37

The queries are not comparable at all.

In SQLITe everything its done by the database engine, but in the Mnesia query you are splitting the work between Mnesia and Elixir.

You can read the Memento Docs to see how you can properly do your SQLITe query only with Memento or you can read the Mnesia docs:

bit4bit

bit4bit

hi, try mnesia directly maybe get a better answer

japplegame

japplegame

Mnesia is more of a key-value storage than a full-featured database.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement