[sqlalchemy] Returning an attribute from an object when using query_property()?

2010-08-02 Thread Zippy P
Hi all:

I have the following code:

class EventQuery(Query):
  def histogram(self):
''' return a histogram of source / count(source) given an eventquery''
...
...

class Event(Base):
  __tablename__ = 'events'
  query = Session.query_property(query_cls = EventQuery)
  name = Column(Unicode(128))
  source = Column(BigInteger)
...

Now, Event has 54 additional attributes, some of which are quite large. In
EventQuery.histogram(), all I really need is the Event.source attribute.
'events' has over 150 million rows, and it's likely that
EventQuery.histogram() will be called for all events.

So - is there any way to 'modify' the query (self) in histogram() so that it
only returns (Event.source, func.count(Event.source)), or, for the general
case, to modify the query to return an attribute of the object? I've tried
from_self, but that seems highly inefficient (at least 20x slower than
Session.query(...) ). I'd also really like to keep histogram() as a method
of EventQuery.

I can see reasons why this would be impossible, but perhaps there's a way to
do it.

Thanks,

S.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Returning an attribute from an object when using query_property()?

2010-08-02 Thread Michael Bayer

On Aug 2, 2010, at 12:41 PM, Zippy P wrote:

 Hi all:
 
 I have the following code:
 
 class EventQuery(Query):
   def histogram(self):
 ''' return a histogram of source / count(source) given an eventquery''
 ...
 ...
 
 class Event(Base):
   __tablename__ = 'events'
   query = Session.query_property(query_cls = EventQuery)
   name = Column(Unicode(128))
   source = Column(BigInteger)
 ...
 
 Now, Event has 54 additional attributes, some of which are quite large. In 
 EventQuery.histogram(), all I really need is the Event.source attribute. 
 'events' has over 150 million rows, and it's likely that 
 EventQuery.histogram() will be called for all events.
 
 So - is there any way to 'modify' the query (self) in histogram() so that it 
 only returns (Event.source, func.count(Event.source)), or, for the general 
 case, to modify the query to return an attribute of the object? I've tried 
 from_self, but that seems highly inefficient (at least 20x slower than 
 Session.query(...) ). I'd also really like to keep histogram() as a method of 
 EventQuery.
 
 I can see reasons why this would be impossible, but perhaps there's a way to 
 do it.

you can use values(Event.source, func.count(...)), or just put a method on 
Event called histogram that uses the standard object_session(self).query(...) 
approach, I'm not really sure what the using the old query_property accessor 
gets you here



 
 Thanks,
 
 S.
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.