The performance of my JSONB full text search is awfully slow using PostgreSQL and SQLAlchemy. How can I speed it up?
class Book(Base):
__tablename__ = "book"
id = Column(Integer, primary_key=True)
jsondata = Column(JSONB)
__table_args__ = (Index('index_jsondesc',
text("(jsondata->'description') jsonb_path_ops"),
postgresql_using="gin"),)
class BookSearch:
def __init__(self):
pass
def search(keyword):
self.query = self.query.filter(Book.jsondata['description'].cast(Unicode).match(keyword))
booksearch = BookSearch()
booksearch.search("Python")
Given selective enough queries, speeding up full text search queries means having the proper index in place. jsonb_path_ops
do not benefit full text search:
The non-default GIN operator class
jsonb_path_ops
supports indexing the@>
operator only.
Instead you need (for example) a functional index for explicit to_tsvector()
:
class Book(Base):
__tablename__ = "book"
id = Column(Integer, primary_key=True)
jsondata = Column(JSONB)
__table_args__ = (
Index('index_jsondesc',
func.to_tsvector('english', jsondata['description'].astext),
postgresql_using="gin"),
)
Note that you must choose the configuration to use when defining the index. Your query must then match the configuration used in the index:
def search(keyword):
tsvector = func.to_tsvector('english', Book.jsondata['description'].astext)
self.query = self.query.filter(tsvector.match(keyword))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.