Daniil Boiko
Daniil Boiko

Reputation: 13

SQLAlchemy fast search

I'm using flask together with SQLAlchemy, SQLAlchemy-Searchable and PostgreSQL (3m rows in a table). count() function is extremely slow, so I want to use count_estimate() (https://wiki.postgresql.org/wiki/Count_estimate)

q = Article.query.search(query,sort=True)
answers = q.limit(5).all()

How can I connect SQLAlchemy's query with raw sql? Thanks.

Upvotes: 1

Views: 332

Answers (1)

mad_
mad_

Reputation: 8273

You can use func.count() function to do so.

select([func.count()]).select_from(table)

Otherwise, for running raw sql inside the query part. You can use text() function.

Upvotes: 1

Related Questions