> On 3 Nov 2014, at 13:08, Ladislav Lenart <lenart...@volny.cz> wrote: > > Hello. > > >> On 31.10.2014 18:33, Simon King wrote: >> At a guess, I would say that the Python code "Foo.bar != bar" means >> that you are looking for Foo objects that aren't related to bar. >> This includes Foos that are related to a *different* Bar instance, >> as well as Foos that aren't related to *any* bar instance. In sql >> terms, foo.bar_id != bar.id OR foo.bar_id is NULL. >> >> Whereas in the other instance, you've asked for something more >> specific, so that's what SA has given you. > > Thank you for the explanation. Seems logical. I have just never looked at it > this way. The generated SQL has surprised me because in my model: > * Foo.bar cannot be NULL by definition and > * bar instance (the query parameter used in the filter()) already has an id. > > Also, the SQL SELECT with an OR condition might confuse the DB query planner > to > choose a suboptimal plan. > > I have learned new lesson: do not use SA object references when the ultimate > goal is to create a SQL SELECT. > > > Thank you once again to both of you, > > Ladislav Lenart > > PS: Sorry for the delay. I was offline during the weekend. >
If your foreign key column is indexed, I'd be surprised if there was a difference in query plan between the two, particularly if the column is declared NOT NULL in the database. Simon -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.