How to apply LIMIT / OFFSET on a large table in sqlalchemy, while getting an intermediate result [please read the details below]?

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

When performing these operations, I need an intermediate result (obtained after filtering / sorting) to find out the number of records matching the given filter (send this information to the interface).

Now I use filterand order_byfor sqlalchemy to obtain an intermediate result, and then applying the cutting list. How can I achieve the same using sqlalchemy filter, order_byand slicetogether with getting the number of records after filtering / sorting as a subresult?

Now I use the following sqlalchemy query:

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()
+4
source share
1 answer
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()
+3
source

All Articles