I don't understand why you do it. You should write simple:<br><br>three(TA, TB) -><br> QLC = qlc:q([B || {A} <- ets:table(TA), {_,B} <- ets:table(TB), A =:= B]),<br>
do("two", QLC).<br>
<br>With N = 10000:<br>$ erl -noshell -run question<br>Query Info for query "one": qlc:q([element(2, B) ||<br> A <-<br> ets:table(8204,<br> [{traverse,{select,[{{'$1'},[true],['$1']}]}}]),<br>
B <- ets:table(12301),<br> A =:= element(2, B)])<br>Query time: 28.2231s<br>Results: 10000<br>Query Info for query "two": begin<br> V1 =<br> qlc:q([P0 ||<br> P0 = {A} <- qlc:keysort(1, ets:table(8204), [])]),<br>
V2 =<br> qlc:q([[G1|B] ||<br> G1 <- V1,<br> B <- qlc:keysort(2, ets:table(12301), []),<br> element(1, G1) == element(2, B)],<br> [{join,merge}]),<br>
qlc:q([element(2, B) ||<br> [{A}|B] <- V2,<br> A =:= element(2, B)])<br>end<br>Query time: 0.036186s<br>Results: 10000<br>Query Info for query "three": begin<br> V1 =<br> qlc:q([P0 ||<br>
P0 = {_,B} <- ets:table(12301)]),<br> V2 =<br> qlc:q([[G1|G2] ||<br> G2 <- V1,<br> G1 <- ets:table(8204),<br> element(2, G1) =:= element(1, G2)],<br>
[{join,lookup}]),<br> qlc:q([B ||<br> [{A}|{_,B}] <- V2,<br> A =:= B])<br>end<br>Query time: 0.016635s<br>Results: 10000<br><br><div class="gmail_quote">On Fri, Apr 17, 2009 at 4:00 AM, Sam Bobroff <span dir="ltr"><<a href="mailto:samb@m5networks.com.au">samb@m5networks.com.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">-----BEGIN PGP SIGNED MESSAGE-----<br>
Hash: SHA1<br>
<br>
Hi Erlangers,<br>
<br>
I've discovered that when performing a QLC query, QLC is unable to use<br>
a fast join unless the values being returned by both generators are<br>
wrapped as tuples.<br>
<br>
Does anyone know if this is caused just because that pattern isn't<br>
recognised by QLC? Would it be worth adding this as a pattern?<br>
<br>
Here's an example (notice that "one" is done by traversal and is slow,<br>
while "two" is done by a merge join and is fast):<br>
<br>
- -module(question).<br>
- -export([start/0]).<br>
<br>
- -include_lib("stdlib/include/qlc.hrl").<br>
<br>
- -define(N, 1000).<br>
<br>
start() -><br>
TA = ets:new(ta, []),<br>
TB = ets:new(tb, []),<br>
[ ets:insert(TA, {X}) || X <- lists:seq(1, ?N) ],<br>
[ ets:insert(TB, {X, $a - 1 + (X rem 26)}) || X <- lists:seq(1, ?N) ],<br>
one(TA, TB),<br>
two(TA, TB),<br>
init:stop().<br>
<br>
one(TA, TB) -><br>
TMP = qlc:q([A || {A} <- ets:table(TA)]),<br>
QLC = qlc:q([element(2, B) || A <- TMP, B <- ets:table(TB), A =:= element(2, B)]),<br>
do("one", QLC).<br>
<br>
two(TA, TB) -><br>
TMP = qlc:q([A || A <- ets:table(TA)]),<br>
QLC = qlc:q([element(2, B) || {A} <- TMP, B <- ets:table(TB), A =:= element(2, B)]),<br>
do("two", QLC).<br>
<br>
do(Name, QLC) -><br>
io:fwrite("Query Info for query ~p: ~s\n", [Name, qlc:info(QLC)]),<br>
ST = now(),<br>
Result = qlc:e(QLC),<br>
ET = now(),<br>
io:fwrite("Query time: ~ps\nResults: ~p\n", [timer:now_diff(ET, ST) / 1000000, length(Result)]).<br>
<br>
Running this on my machine (running Erlang R12B):<br>
<br>
$ erlc question.erl && erl -noshell -run question<br>
Query Info for query "one": qlc:q([element(2, B) ||<br>
A <-<br>
ets:table(8204,<br>
[{traverse,{select,[{{'$1'},[true],['$1']}]}}]),<br>
B <- ets:table(12301),<br>
A =:= element(2, B)])<br>
Query time: 0.908338s<br>
Results: 1000<br>
Query Info for query "two": begin<br>
V1 =<br>
qlc:q([P0 ||<br>
P0 = {A} <- qlc:keysort(1, ets:table(8204), [])]),<br>
V2 =<br>
qlc:q([[G1|B] ||<br>
G1 <- V1,<br>
B <- qlc:keysort(2, ets:table(12301), []),<br>
element(1, G1) == element(2, B)],<br>
[{join,merge}]),<br>
qlc:q([element(2, B) ||<br>
[{A}|B] <- V2,<br>
A =:= element(2, B)])<br>
end<br>
Query time: 0.010174s<br>
Results: 1000<br>
<br>
Cheers,<br>
Sam.<br>
- --<br>
Sam Bobroff | <a href="mailto:sam@m5net.com">sam@m5net.com</a> | M5 Networks<br>
-----BEGIN PGP SIGNATURE-----<br>
Version: GnuPG v1.4.9 (GNU/Linux)<br>
<br>
iEYEARECAAYFAknn4sMACgkQm97/UHSa/AQJ1wCfa5OxpvF+RngKfFniuXQxWZ8i<br>
BxMAnifufDoqp0ZrRYdd4oHILmPyBCez<br>
=250i<br>
-----END PGP SIGNATURE-----<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br><br>Analyze your data in minutes. Share your insights instantly. Thrill your boss. Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com">www.gooddata.com</a><br>