Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to apply LIMIT/OFFSET on large table in sqlalchemy while getting intermediate result as well [please read the details below]?

I want to do 3 operations on a table which contains large number of records: (1)filter (based on a search query) (2)order_by (on a single column in asc/desc) and (3)slice (for given offset and limit values).

While carrying out these operations I need the intermediate result (obtained after filtering/sorting), to find out the number of records which satisfied the given filter query (to send this information to the front-end).

Right now I am using FILTER and ORDER_BY of sqlalchemy to get the intermediate result and then applying slicing on the list. How can I achieve the same using sqlalchemy's FILTER, ORDER_BY and SLICE together along with getting the number of records after filtering/sorting as a sub-result?

The sqlalchemy query I am using right now is as follows:

result = session.\
         query(Customer).\
         filter(Customer.name.like('%' + filter_query + '%') | Customer.email.like('%' + filter_query + '%') | Customer.partner.like('%' + filter_query + '%')).\
         order_by(asc(getattr(Customer, sorting_column_name))).\
         all()
like image 885
exAres Avatar asked Jul 28 '14 09:07

exAres


1 Answers

result = session.\
     query(Customer).\
     filter(Customer.name.like('%' + filter_query + '%') | Customer.email.like('%' + filter_query + '%') | Customer.partner.like('%' + filter_query + '%')).\
     order_by(asc(getattr(Customer, sorting_column_name))).\
     slice(offset,limit).\
     all()
like image 130
cyberra Avatar answered Oct 09 '22 06:10

cyberra