<div dir="ltr"><div>It depends on how your filter function works:<br><br>Create a table:<br><br>2> ets:new(foo, [named_table, {keypos, 1}]).<br>foo<br><br>Fill the table so we can measure lookup differences in time:<br><br>3> ets:insert(foo, [{K, K+1} || K <- lists:seq(1, 1000000)]).<br>true<br><br>Create a point-query match spec:<br><br>4> MS = ets:fun2ms(fun({3, V}) -> V end).<br>[{{3,'$1'},[],['$1']}]<br><br>Run a query with this matchspec:<br><br>5> timer:tc(fun() -> ets:select(foo, MS) end).<br>{25,[4]}<br><br>Create another match-spec where you don't match directly on the key:<br><br>6> MS2 = ets:fun2ms(fun({K, V}) when K == 3 -> V end).<br>[{{'$1','$2'},[{'==','$1',3}],['$2']}]<br><br>Now time this one:<br><br>7> timer:tc(fun() -> ets:select(foo, MS2) end).<br>{177663,[4]}<br><br></div>As you can see, the MS2 variant requests elements of the form {$1, $2} and then proceeds to filter them via $1 == 3, which is much more expensive than the first one which requests data of the form {3, $2} and then needs no filtering.<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 26, 2015 at 1:40 PM, Max Lapshin <span dir="ltr"><<a href="mailto:max.lapshin@gmail.com" target="_blank">max.lapshin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra">Correct me if I'm wrong, but ets:select is always a full table scan.</div><div class="gmail_extra"><br></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">J.</div>
</div>