Django管理员搜索查询没有打到Postgres索引 [英] Django Admin search query not hitting Postgres index

查看:125
本文介绍了Django管理员搜索查询没有打到Postgres索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Postgres(所有localhost)中使用GIN索引进行全文搜索,当我编写自己的查询并在psql中运行时,我获得了很好的响应时间,而 EXPLAIN ANALYZE 报告索引命中(woohoo),但是通过Django管理员搜索框通过相同的搜索字词进行查询时,该索引不会被扫描,并且查询将永远超出完成。



我的索引是通过完全可怕的pg_trgm Postgres扩展

  CREATE INDEX name_gin ON实体使用gin(name gin_trgm_ops); 

此查询命中索引,并采取炽热的 84ms 搜索900k全文记录:

  SELECT COUNT(*)
FROM实体
WHERE名称LIKE UPPER(' %dubteeeff%')
AND name LIKE UPPER('%django%');

由Django Admin界面创建的完全相同的查询需要 938ms

  SELECT COUNT(*)
FROM entity
WHERE UPPER(entity :: text)LIKE UPPER('%dubteeeff%')
和UPPER(entity。name:: text)LIKE UPPER('%django%');`

唯一的区别似乎是列的引用方式 - Django和Postgres相当新 - 有一些Postgres配置设置或Django管理QuerySet - 某些东西,或者我可以覆盖或修改的RawQuery来加速这个?我不希望我的管理员页面最好是缓慢,最糟糕的是,拖动网站的其余部分。



提前感谢。

解决方案

看起来像您需要在实体(upper(name))上添加索引而不是 entity(name),用于Django正在生成的不区分大小写的搜索。


I'm doing a full-text search with a GIN index in Postgres (all localhost), and when I write my own query and run it in psql, I am getting great response times and EXPLAIN ANALYZE reports an index hit (woohoo), however when querying via the Django Admin search box with the same search terms, the index is not scanned and the query takes upwards of forever to complete.

My index is created thusly via the fully-awesome pg_trgm Postgres extension:

CREATE INDEX name_gin ON entity USING gin (name gin_trgm_ops);

This query hits the index and takes a blazing 84ms to search through 900k full-text records:

SELECT COUNT(*) 
FROM entity 
WHERE name LIKE UPPER('%dubteeeff%') 
 AND name LIKE UPPER('%django%');

The exact same query, as created by the Django Admin interface, takes 938ms:

SELECT COUNT(*) 
FROM entity 
WHERE UPPER("entity"."name"::text) LIKE UPPER('%dubteeeff%') 
  and UPPER("entity"."name"::text) LIKE UPPER('%django%');`

The only difference appears to be the way the columns are referenced--I'm fairly new with Django and Postgres--is there some Postgres config setting or Django admin QuerySet-something, or a RawQuery that I could override or modify to speed this up? I don't want my Admin page to at best be slow and at worst, drag the rest of the site down with it.

Thanks in advance.

解决方案

Look like you need to add an index on entity(upper(name)) rather than entity(name) for the case-insensitive search that Django is generating.

这篇关于Django管理员搜索查询没有打到Postgres索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆