解决了我在这个问题中提出的问题后,我尝试使用索引来优化 FTS 的性能。我在我的数据库上发出了命令:
CREATE INDEX my_table_idx ON my_table USING gin(to_tsvector('italian', very_important_field), to_tsvector('italian', also_important_field), to_tsvector('italian', not_so_important_field), to_tsvector('italian', not_important_field), to_tsvector('italian', tags));
Run Code Online (Sandbox Code Playgroud)
然后我编辑了模型的 Meta 类,如下所示:
class MyEntry(models.Model):
very_important_field = models.TextField(blank=True, null=True)
also_important_field = models.TextField(blank=True, null=True)
not_so_important_field = models.TextField(blank=True, null=True)
not_important_field = models.TextField(blank=True, null=True)
tags = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'my_table'
indexes = [
GinIndex(
fields=['very_important_field', 'also_important_field', 'not_so_important_field', 'not_important_field', 'tags'],
name='my_table_idx'
)
]
Run Code Online (Sandbox Code Playgroud)
但似乎一切都没有改变。查找所花费的时间与以前完全相同。
这是查找脚本:
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
# other unrelated stuff here
vector = …Run Code Online (Sandbox Code Playgroud) python django postgresql full-text-search full-text-indexing