使用 Postgres 9.5,我有一张表addresses。
CREATE TABLE addresses (
id integer PRIMARY KEY,
address text
);
Run Code Online (Sandbox Code Playgroud)
在那个表中,我有 750 万行。例子:
1, "1600 Pennsylvania Avenue NW, Washington, DC, 20500"
我在我的应用程序中使用这个表进行自动建议搜索,所以我需要使用这种类型的查询:
SELECT * FROM addresses WHERE address LIKE '123 Main St%';
Run Code Online (Sandbox Code Playgroud)
我创建了这个索引:
CREATE INDEX address_idx ON addresses (address);
Run Code Online (Sandbox Code Playgroud)
但问题是它需要大约 1 秒,这太慢了。
这是查询计划:
EXPLAIN SELECT * FROM addresses WHERE address LIKE '123 Main St%';
----
Seq Scan on addresses (cost=0.00..161309.76 rows=740 width=41)
Filter: (address ~~ '123 Main St%'::text)
Run Code Online (Sandbox Code Playgroud)
我尝试创建几种类型的gin索引,但它们要么没有效果,要么使查询变慢。我不确定我是否正确使用它们。
关于如何创建针对此类查询优化的索引的任何想法?
编辑
迄今为止找到的最佳解决方案是使用文本范围扫描: …
我需要一些帮助来在 django 视图中使用 GIN 索引为全文搜索构建正确的查询。我有一个相当大的数据库(约 40 万行),需要对其中的 3 个字段进行全文搜索。尝试使用django 文档搜索,这是 GIN 之前的代码。它有效,但需要 6 秒以上的时间来搜索所有字段。接下来,我尝试实现GIN索引以加快搜索速度。已经有很多问题如何构建它。但我的问题是 -使用 GIN 索引进行搜索时,视图查询如何更改?我应该搜索哪些字段?
在 GIN 之前:
models.py
class Product(TimeStampedModel):
product_id = models.AutoField(primary_key=True)
shop = models.ForeignKey("Shop", to_field="shop_name")
brand = models.ForeignKey("Brand", to_field="brand_name")
title = models.TextField(blank=False, null=False)
description = models.TextField(blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
视图.py
def get_cosmetic(request):
if request.method == "GET":
pass
else:
search_words = request.POST.get("search")
search_vectors = (
SearchVector("title", weight="B")
+ SearchVector("description", weight="C")
+ SearchVector("brand__brand_name", weight="A")
)
products = (
Product.objects.annotate(
search=search_vectors, rank=SearchRank(search_vectors, search)
) …Run Code Online (Sandbox Code Playgroud) postgresql gwt-gin django-models django-queryset django-views
我在Guice中找到了Guice Overriding Binding的答案,但是不知道如何在GWT中对GIN做同样的事情.
提前致谢!
看起来GWT有自己的烘焙DI机制(GWT.create(Class<?>)).除此之外,GIN还能带来哪些好处?您是否应该将它们彼此结合使用,还是相互排斥?我喜欢Guice所以我很想使用GIN,但是如果GWT已经开箱即用,那么我不想介绍它.
如何最小化GWT应用程序用户浏览器最初下载的代码量?好吧,只需在GWT.runAsync()调用中包含可能很大的操作.但是,由于我们的应用程序使用GWT最佳实践(依赖注入,MVP模式),因此它并不像GWT doc所描述的那样简单.您能否告诉我如何将代码拆分与GIN结合使用?
我的每个活动都需要一个对应的单例View实现.将它们注入活动的最佳策略是什么?
构造函数注入 Activity构造函数是从ActivityMapper的getActivity()调用的.ctor已经有一个参数(一个Place对象).我必须创建ActivityMapper,注入所有可能的视图.不好...
方法注入 - "在执行构造函数后自动执行注释的函数." (GWT in Action,2nd Ed.)好吧,"在执行ctor之后"显然不够快,因为当start()调用Activity的方法时,视图(或以这种方式注入的RPC服务)仍未初始化,我得到了一个NPE.
在Activity的ctor中用GWT.create构造注入器.没用,因为他们不再是单身人士.
有人可以帮我解决问题吗?
我正在从GWT 2.5.1升级到2.6.1并且在尝试运行我的项目的代码服务器时出现以下错误...
[INFO] Compiling module <SOME_MODULE>
[INFO] Validating units:
[INFO] Ignored 1 unit with compilation errors in first pass.
[INFO] Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
[INFO] Computing all possible rebind results for 'com.gwtplatform.mvp.client.DesktopGinjector'
[INFO] Rebinding com.gwtplatform.mvp.client.DesktopGinjector
[INFO] Invoking generator com.google.gwt.inject.rebind.GinjectorGenerator
[INFO] [ERROR] Error injecting com.gwtplatform.mvp.client.proxy.PlaceManager: Unable to create or inherit binding: No @Inject or default constructor found for com.gwtplatform.mvp.client.proxy.PlaceManager
[INFO] Path to required node:
[INFO]
[INFO] com.gwtplatform.mvp.client.proxy.PlaceManager [com.gwtplatform.mvp.client.ClientGinjector#getPlaceManager()]
[INFO] …Run Code Online (Sandbox Code Playgroud) 在https://www.postgresql.org/docs/current/static/pgtrgm.html上,解释了如何使用带有 gin_trgm_ops 选项的特殊 GIN 索引来提高 trigram 相似性运算符的性能。
CREATE INDEX trgm_idx ON test_trgm USING GIN (t gin_trgm_ops);
Run Code Online (Sandbox Code Playgroud)
也有人说:
这些索引不支持相等或简单的比较运算符,因此您可能还需要常规 B 树索引。
然而,还有 BTREE_GIN 扩展,它应该允许 GIN 索引用作 BTREE 索引的替代品。https://www.postgresql.org/docs/current/static/btree-gin.html
我的问题是:如果我安装 BTREE_GIN 扩展,pg_trgm GIN 索引(带有 gin_trgm_ops 选项)可以用作 BTREE 索引的替代品吗?它是否结合了 BTREE_GIN 和 trigram GIN 索引的属性,或者仍然需要额外的 BTREE 索引来进行连接和相等表达式等?
我创建了一个这样的表,
create table mytable(hash char(40), title varchar(500));
create index name_fts on mytable using gin(to_tsvector('english', 'title'));
CREATE UNIQUE INDEX md5_uniq_idx ON mytable(hash);
Run Code Online (Sandbox Code Playgroud)
当我查询标题时,
test=# explain analyze select * from mytable where to_tsvector('english', title) @@ 'abc | def'::tsquery limit 10;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..277.35 rows=10 width=83) (actual time=0.111..75.549 rows=10 loops=1)
-> Seq Scan on mytable (cost=0.00..381187.45 rows=13744 width=83) (actual time=0.110..75.546 rows=10 loops=1)
Filter: (to_tsvector('english'::regconfig, (title)::text) @@ '''abc'' | ''def'''::tsquery)
Rows Removed by Filter: 10221
Planning time: 0.176 ms
Execution time: 75.564 …Run Code Online (Sandbox Code Playgroud) 我正在使用PostgreSQL 9.4.我的表有一jsonb栏:
CREATE TABLE "PreStage".transaction (
transaction_id serial NOT NULL,
transaction jsonb
CONSTRAINT pk_transaction PRIMARY KEY (transaction_id)
);
CREATE INDEX idxgin ON "PreStage".transaction USING gin (transaction);
Run Code Online (Sandbox Code Playgroud)
我在JSONB列中按键/值存储事务.其中一个要求是从键值中搜索客户名称,因此我运行的查询如下:
SELECT transaction as data FROM "PreStage".transaction
WHERE transaction->>('HCP_FST_NM') ilike ('%neer%');
Run Code Online (Sandbox Code Playgroud)
我做什么似乎查询不喜欢GIN索引.如何使查询使用不区分大小写模式搜索的GIN索引?
我尝试将jsonb列更改为文本,使用gin_trgm_ops搜索所需文本对其进行索引,然后将结果转换为json然后搜索所需的键/值.这种方法似乎不起作用.