[web-devel] Join support in persistent

Michael Snoyman michael at snoyman.com
Sun Apr 3 08:10:52 CEST 2011

On Sun, Apr 3, 2011 at 8:45 AM, Greg Weber <greg at gregweber.info> wrote:


>>> 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.
>> 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?
>> * Correction: It's not so simple to add, at least not efficiently. We
>> would need to have a function like this:
>> selectOneMany :: (PersistEntity one, PersistEntity many, PersistBackend m,
>> Eq (Key one))
>>               => [Filter one]  -> [Order one]
>>               -> [Filter many] -> [Order many]
>>               -> ([Key one] -> Filter many)
>>               -> (many -> Key one)
>>               -> m [((Key one, one), [(Key many, many)])]
>> selectOneMany oneF oneO manyF manyO inFilt' getKey = do
>>     x <- selectList oneF oneO 0 0
>>     let inFilt = inFilt' $ map fst x
>>     y <- selectList (inFilt : manyF) manyO 0 0
>>     return $ map (go y) x
>>   where
>>     go manys one@(key, _) = (one, filter (\x -> getKey (snd x) == key)
>> manys)
>> I'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'd prefer to optimize for larger datasets. Plus, I think the
>> current API is much nicer.
>> If you can think of a better approach than this, let me know. But remember
>> that there's no way to know the sort order of the keys of the one table.
>  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.
>> I think you're solving a different problem. Are you talking about the fact
that the EntryAuthorIn constructor takes a list instead of a Set? That's not
where the slowdown comes from. Actually, for the current backends, a set
would needlessly slow things down, since the In constructor simply converts
things to SQL and lets the database do the work.

I'm not sure what you're suggesting here to be honest, can you clarify?

>>> Looking at the behavior of Rails for joins, I don'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.
>>> If we are to support joins it needs to be very apparent which type of
>>> join is performed.
>> As far as I know, every database on the planet these days treat these two
>> as identical for performance reasons:
>>     SELECT * from a, b where a.foo = b.bar
>>     SELECT * from a INNER JOIN b ON a.foo = b .bar
>> I went the INNER JOIN route because I'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'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.
>> I agree that we should make this clear in the docs.
> It would be best for it to also be clear from the function names or
> arguments..
> I actually thought it *was* clear that it would be an inner join and not an
outer join. But how would you change the names? I don't want to end up with
selectJoiningOneToManyRelationshipUsingInnerJoin ;)

>>> selectOneMany doesn't have an offset and limit. If we added it we end up
>>> with queries like this:
>>> selectOneMany [] [] [] [] EntryAuthorEq 0 0
>> I purposely avoided offset and limit for now, since I'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?
> The number of ones. Breaking up the manys is difficult for the framework
> and the user.
Fair enough.

>>> 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.
>> OK, how about this:
>> data SelectOneManyArgs one many = SelectOneManyArgs { oneFilter ::
>> [Filter] } ...
>> defaultOneManyArgs :: (Key one -> Filter many) -> SelectOneManyArgs one
>> many
>> But I'd like to have shorter names somehow.
>>> I am all for adding these changes for now, I just hope we can move to a
>>> more composable API in the future.
>>> I thought the API that Aur came up with was a better effort in that
>>> direction, although there are definitely practical issues with it.
>> 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're going to end up with some kind of SQL
>> syntax inside of Template Haskell. I'm all for making something like that...
>> but it's not Persistent.
>> I think Persistent'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.
>> 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'll have hit our
>> target.
> 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.
Can I get some feedback on what's missing for this? In the
Database.Persist.GenericSql.Raw module[1], there are two functions (withStmt
and execute) that let you run any SQL command against the DB you want. I've
used this myself when I needed to do something that Persistent didn't allow
(a full text search in my case).

I know that the functions are neither pretty nor well documented, but what's
missing that is preventing people from dropping down to SQL now? If it's
just a documentation issue, I'll address it.



> Greg
>> Michael
>>> [1]
>>> http://akitaonrails.com/2008/05/25/rolling-with-rails-2-1-the-first-full-tutorial-part-2
>>> Greg Weber
>>> On Sat, Apr 2, 2011 at 2:50 PM, Michael Snoyman <michael at snoyman.com>wrote:
>>>> Hey all,
>>>> After a long discussion with Aur Saraf, I think we came up with a good
>>>> approach to join support in Persistent. Let's review the goals:
>>>> * Allow for non-relational backends, such as Redis (simple key-value
>>>> stores)
>>>> * Allow SQL backends to take advantage of the database's JOIN abilities.
>>>> * Not force SQL backends to use JOIN if they'd rather avoid it.
>>>> * Keep a simple, straight-forward, type-safe API like we have
>>>> everywhere else in Persistent.
>>>> * Cover the most common (say, 95%) of use cases out-of-the-box.
>>>> So our idea (well, if you don't like it, don't blame Aur...) is to
>>>> provide a separate module (Database.Persist.Join) which provides
>>>> special functions for the most common join operations. To start with,
>>>> I want to handle a two-table one-to-many relationship. For
>>>> demonstration purposes, let's consider a blog entry application, with
>>>> entities Author and Entry. Each Entry has precisely one Author, and
>>>> each Author can have many entries. In Persistent, it looks like:
>>>> Author
>>>>    name String Asc
>>>>    isPublic Bool Eq
>>>> Entry
>>>>    author AuthorId Eq
>>>>    title String
>>>>    published UTCTime Desc
>>>>    isPublic Bool Eq
>>>> In order to get a list of all entries along with their authors, you
>>>> can use the newly added[1] selectOneMany function:
>>>>    selectOneMany [AuthorIsPublicEq True] [AuthorNameAsc]
>>>> [EntryIsPublicEqTrue] [EntryPublishedDesc] EntryAuthorEq
>>>> This will return a value of type:
>>>>    type AuthorPair = (AuthorId, Author)
>>>>    type EntryPair = (EntryId, Entry)
>>>>    [(AuthorPair, [EntryPair])]
>>>> In addition to Database.Persist.Join, there is also a parallel module
>>>> named Database.Persist.Join.Sql, which has an alternative version of
>>>> selectOneMany that is powered by a SQL JOIN. It has almost identical
>>>> semantics: the only catch comes in when you don't fully specify
>>>> ordering. But then again, if you don't specify ordering in the first
>>>> place the order of the results is undefined, so it really *is*
>>>> identical semantics, just slightly different behavior.
>>>> Anyway, it's almost 1 in the morning, so I hope I haven't rambled too
>>>> much. The basic idea is this: Persistent 0.5 will provide a nice,
>>>> high-level approach to relations. I'll be adding more functions to
>>>> these modules as necessary, and I'd appreciate input on what people
>>>> would like to see there.
>>>> Michael
>>>> [1]
>>>> https://github.com/snoyberg/persistent/commit/d2b52a6a7b7a6af6234315492f24f821a0ea7ce4#diff-2
>>>> _______________________________________________
>>>> web-devel mailing list
>>>> web-devel at haskell.org
>>>> http://www.haskell.org/mailman/listinfo/web-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/web-devel/attachments/20110403/a0706eae/attachment-0001.htm>

More information about the web-devel mailing list