<br><br><div class="gmail_quote">On Sat, Apr 2, 2011 at 9:19 PM, Michael Snoyman <span dir="ltr">&lt;<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div dir="ltr"><br><br><div class="gmail_quote"><div class="im">On Sun, Apr 3, 2011 at 6:40 AM, Greg Weber <span dir="ltr">&lt;<a href="mailto:greg@gregweber.info" target="_blank">greg@gregweber.info</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div>That is wonderful- application joins or database joins. Lets compare to Rails:</div><div><div><br></div><div><div>selectOneMany [AuthorIsPublicEq True] [AuthorNameAsc] [EntryIsPublicEqTrue] [EntryPublishedDesc] EntryAuthorEq</div>





</div><div><br></div></div>Author.where(:isPublic =&gt; true).order(&quot;name&quot;).includes(:entries) &amp; Entry.where(:isPublic =&gt; true).order(&quot;published DESC&quot;)<div><br></div></blockquote><div><br></div>



</div><div>How does Rails handle when there are two join keys? For example:</div><div><br></div><div>Person</div><div>  name String</div><div>Entry</div><div>  author PersonId</div><div>  editor PersonId</div><div>  title String</div>

<div class="im">

<div></div></div></div></div></blockquote><div><br></div><div>Basically the same- you have to declare associations. It auto-detects the class and foreign key from the association name in simple case- I left the code out before:</div>

<div><br></div><div><div>class Entry &lt; ActiveRecord::Base; end</div></div><div>class Author &lt; ActiveRecord::Base</div><div>  has_many :entries</div><div>end</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div dir="ltr"><div class="gmail_quote"><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div></div><div>Note that a Rails query is lazy and the &amp; is combining the queries. However, when there are no filtering criteria on the association, Rails prefers to perform 2 queries- one to retrieve the authors, and then one to retrieve the entries based on the author ids:</div>






<div><div><br></div><div>SELECT &quot;entries&quot;.* FROM &quot;entries&quot; WHERE (&quot;entries&quot;.author_id IN (51,1,78,56,64,84,63,60))</div></div><div><br></div><div>Originally rails always did do a SQL level join, but then decided to switch to preferring app-level, largely because it reduced the number of Ruby objects that needed to be allocated, resulting in much better performance for large data sets [1].</div>






<div><br></div></blockquote><div><br></div></div><div>Regarding lazy: we could definitely go that route of allowing laziness via unsafeInterleaveIO, but it&#39;s very contrary to the normal workings of Yesod/Persistent. If we want constant space, we should use enumerators I think.</div>



<div><br></div><div>As far as the performance concern: do you think we need to be worried about the large number of allocated objects? My guess is that Haskell won&#39;t have the same concerns here, but I could be wrong.</div>

</div></div></blockquote><div><br></div><div>I think these problems were specific to the way the results were being constructed in Rails. The real point is that Rails was able to default to smart application joins with no downside.</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div dir="ltr"><div class="gmail_quote"><div class="im">

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div></div><div>It appears that persistent.Join is instead performing an n + 1 query- one query per each author. We should avoid these kinds of queries, and then there will not be much point to an outer join in the db.</div>





<div><br></div></blockquote><div><br></div></div><div>Good point, we can easily fix that. However, can you clarify what you mean by outer join here? Why would there be need for an outer join?</div><div><br></div><div>* Correction: It&#39;s not so simple to add, at least not efficiently. We would need to have a function like this:</div>



<div><br></div><div><div>selectOneMany :: (PersistEntity one, PersistEntity many, PersistBackend m, Eq (Key one))</div><div>              =&gt; [Filter one]  -&gt; [Order one]</div><div>              -&gt; [Filter many] -&gt; [Order many]</div>



<div>              -&gt; ([Key one] -&gt; Filter many)</div><div>              -&gt; (many -&gt; Key one)</div><div>              -&gt; m [((Key one, one), [(Key many, many)])]</div><div>selectOneMany oneF oneO manyF manyO inFilt&#39; getKey = do</div>



<div>    x &lt;- selectList oneF oneO 0 0</div><div>    let inFilt = inFilt&#39; $ map fst x</div><div>    y &lt;- selectList (inFilt : manyF) manyO 0 0</div><div>    return $ map (go y) x</div><div>  where</div><div>    go manys one@(key, _) = (one, filter (\x -&gt; getKey (snd x) == key) manys)</div>



</div><div><br></div><div>I&#39;m not convinced that there will be any performance enhancement here. Most likely, when dealing with small datasets, this will pay off due to the savings in the number of bytes transmitted with the server. But for large datasets, the O(m * n) complexity (number of ones times number of manys) will hurt us. I&#39;d prefer to optimize for larger datasets. Plus, I think the current API is much nicer.</div>



<div><br></div><div>If you can think of a better approach than this, let me know. But remember that there&#39;s no way to know the sort order of the keys of the one table.</div></div></div></blockquote><div><br></div><div>

So this is an overly simple solution. The first selectList should probably be a function that returns a list of ids to reduce 2m from the map to m (unless that can be fused away). But more importantly it should be returning a Set of keys to make for one lookup for each n in the second query for a total of O(n) + O(m). The ideal here might be a set that has immediate access to the list of keys. An intriguing idea would be for SelectList to return an ordered Map that can still be treated as a list.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div dir="ltr"><div class="gmail_quote"><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div></div><div>Looking at the behavior of Rails for joins, I don&#39;t like how it decides between types of joins. The sql produced by Rails is not in fact identical to the persistent one: it will do an outer join with the entry filtering in a WHERE clause, not as part of the JOIN conditions.</div>





<div>If we are to support joins it needs to be very apparent which type of join is performed.</div><div><br></div></blockquote><div><br></div></div><div>As far as I know, every database on the planet these days treat these two as identical for performance reasons:</div>



<div><br></div><div>    SELECT * from a, b where a.foo = b.bar</div><div>    SELECT * from a INNER JOIN b ON a.foo = b .bar</div><div><br></div><div>I went the INNER JOIN route because I&#39;ve always had a preference for it. But *outer* join will be a very different beast. If you look in the runtests.hs file, it specifically relies on the fact that we&#39;re doing an inner join. An outer join would mean that *all* authors appear in the output set, while an inner join will only include authors with entries.</div>



<div><br></div><div>I agree that we should make this clear in the docs.</div></div></div></blockquote><div><br></div><div>It would be best for it to also be clear from the function names or arguments..</div><div><br></div>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div dir="ltr"><div class="gmail_quote"><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div></div><div><br></div><div>

selectOneMany doesn&#39;t have an offset and limit. If we added it we end up with queries like this:</div>

<div>

<div><div><br></div><div>selectOneMany [] [] [] [] EntryAuthorEq 0 0</div><div><br></div></div></div></blockquote><div><br></div></div><div>I purposely avoided offset and limit for now, since I&#39;m not exactly certain how it should be applied. Should it offset/limit the number of ones? The number of manys? The total number of manys for all ones, or the number of manys per one?</div>

</div></div></blockquote><div> </div><div>The number of ones. Breaking up the manys is difficult for the framework and the user.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div dir="ltr"><div class="gmail_quote"><div class="im">

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div></div><div><div>This function with 5+ required arguments is somewhat awkward/difficult to use and to read. Rails is composable in a readable way because it copied haskellDB. I would like to get away from the empty optional arguments.</div>



</div></div></div></blockquote><div><br></div></div><div>OK, how about this:</div><div><br></div><div>data SelectOneManyArgs one many = SelectOneManyArgs { oneFilter :: [Filter] } ...</div><div><br></div><div>defaultOneManyArgs :: (Key one -&gt; Filter many) -&gt; SelectOneManyArgs one many</div>



<div><br></div><div>But I&#39;d like to have shorter names somehow.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div>

</div><div>I am all for adding these changes for now, I just hope we can move to a more composable API in the future.</div>

</div><div>I thought the API that Aur came up with was a better effort in that direction, although there are definitely practical issues with it.<br><div><br></div></div></div></blockquote><div><br></div></div><div>I definitely think we should continue exploring the design space to see if we can come up with better solutions. But I have a sneaking suspicion that if we want to have fully customizable queries that allow arbitrary joining and selecting individual fields, we&#39;re going to end up with some kind of SQL syntax inside of Template Haskell. I&#39;m all for making something like that... but it&#39;s not Persistent.</div>



<div><br></div><div>I think Persistent&#39;s goal should *not* be to handle every possible query you can ever imagine. It should handle the common cases efficiently, with type-safety and a simple API. It should also allow people to easily drop down to something more low-level- possibly sacrificing type safety- when the need arises. And over time, as we get more user experience feedback, we can push the boundary farther of what Persistent handles out-of-the-box.</div>



<div><br></div><div>If we get to the point where 95% of queries people perform can be handled with an out-of-the-box function, and for the 5% people need to write some SQL (or MongoDB backend code, or Redis...), I think we&#39;ll have hit our target.</div>

</div></div></blockquote><div><br></div><div>I agree, I am not trying to say that we need to elegantly handle every possible query. I am just pushing that for those that we are currently handling to be elegant. Persistent integration with directly writing SQL should probably be a high priority.</div>

<div><br></div><div>Greg</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div dir="ltr"><div class="gmail_quote">

<div><br></div><font color="#888888"><div>Michael</div></font><div><div></div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div>

</div><div>[1] <a href="http://akitaonrails.com/2008/05/25/rolling-with-rails-2-1-the-first-full-tutorial-part-2" target="_blank">http://akitaonrails.com/2008/05/25/rolling-with-rails-2-1-the-first-full-tutorial-part-2</a></div>






<div><br></div><div>Greg Weber</div><div><br><div class="gmail_quote"><div><div></div><div>On Sat, Apr 2, 2011 at 2:50 PM, Michael Snoyman <span dir="ltr">&lt;<a href="mailto:michael@snoyman.com" target="_blank">michael@snoyman.com</a>&gt;</span> wrote:<br>







</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div>Hey all,<br>
<br>
After a long discussion with Aur Saraf, I think we came up with a good<br>
approach to join support in Persistent. Let&#39;s review the goals:<br>
<br>
* Allow for non-relational backends, such as Redis (simple key-value stores)<br>
* Allow SQL backends to take advantage of the database&#39;s JOIN abilities.<br>
* Not force SQL backends to use JOIN if they&#39;d rather avoid it.<br>
* Keep a simple, straight-forward, type-safe API like we have<br>
everywhere else in Persistent.<br>
* Cover the most common (say, 95%) of use cases out-of-the-box.<br>
<br>
So our idea (well, if you don&#39;t like it, don&#39;t blame Aur...) is to<br>
provide a separate module (Database.Persist.Join) which provides<br>
special functions for the most common join operations. To start with,<br>
I want to handle a two-table one-to-many relationship. For<br>
demonstration purposes, let&#39;s consider a blog entry application, with<br>
entities Author and Entry. Each Entry has precisely one Author, and<br>
each Author can have many entries. In Persistent, it looks like:<br>
<br>
Author<br>
    name String Asc<br>
    isPublic Bool Eq<br>
Entry<br>
    author AuthorId Eq<br>
    title String<br>
    published UTCTime Desc<br>
    isPublic Bool Eq<br>
<br>
In order to get a list of all entries along with their authors, you<br>
can use the newly added[1] selectOneMany function:<br>
<br>
    selectOneMany [AuthorIsPublicEq True] [AuthorNameAsc]<br>
[EntryIsPublicEqTrue] [EntryPublishedDesc] EntryAuthorEq<br>
<br>
This will return a value of type:<br>
<br>
    type AuthorPair = (AuthorId, Author)<br>
    type EntryPair = (EntryId, Entry)<br>
    [(AuthorPair, [EntryPair])]<br>
<br>
In addition to Database.Persist.Join, there is also a parallel module<br>
named Database.Persist.Join.Sql, which has an alternative version of<br>
selectOneMany that is powered by a SQL JOIN. It has almost identical<br>
semantics: the only catch comes in when you don&#39;t fully specify<br>
ordering. But then again, if you don&#39;t specify ordering in the first<br>
place the order of the results is undefined, so it really *is*<br>
identical semantics, just slightly different behavior.<br>
<br>
Anyway, it&#39;s almost 1 in the morning, so I hope I haven&#39;t rambled too<br>
much. The basic idea is this: Persistent 0.5 will provide a nice,<br>
high-level approach to relations. I&#39;ll be adding more functions to<br>
these modules as necessary, and I&#39;d appreciate input on what people<br>
would like to see there.<br>
<br>
Michael<br>
<br>
[1] <a href="https://github.com/snoyberg/persistent/commit/d2b52a6a7b7a6af6234315492f24f821a0ea7ce4#diff-2" target="_blank">https://github.com/snoyberg/persistent/commit/d2b52a6a7b7a6af6234315492f24f821a0ea7ce4#diff-2</a><br>








<br></div></div>
_______________________________________________<br>
web-devel mailing list<br>
<a href="mailto:web-devel@haskell.org" target="_blank">web-devel@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/web-devel" target="_blank">http://www.haskell.org/mailman/listinfo/web-devel</a><br>
</blockquote></div><br></div></div></div>
</blockquote></div></div></div><br></div>
</blockquote></div><br>