Я использую JSON в моей модели БД SQLAlchemy:
from sqlalchemy.dialects.postgresql import JSON
class Customer(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
custcontext_json = db.Column(JSON, default=lambda: {})
Теперь у меня слишком медленный запрос:
customers = Customer.query.filter(
Customer.clientid == clientid,
or_(
func.lower(Customer.custcontext_json['cinfo', 'userName'].astext.cast(Unicode)).contains(searchterm.lower()),
func.lower(Customer.custcontext_json['cinfo', 'home', 'sign'].astext.cast(Unicode)).contains(searchterm.lower())
),
or_(
Customer.custcontext_json['cinfo', 'home', 'status'].astext == 'pre',
Customer.custcontext_json['cinfo', 'home', 'status'].astext == 'during',
Customer.custcontext_json['cinfo', 'lastChange'].astext.cast(DateTime) > pendulum.now('UTC').subtract(days=14)
)
).all()
Возможно ли индексировать таблица для этого запроса. Или хотя бы его части?