如何在不使用全文检索的情况下提高查询速度?

发布于 2024-10-10 11:41:09 字数 809 浏览 2 评论 0原文

这是我的简单查询;通过搜索 selectnothing 我确信我不会有任何点击。

SELECT nome_t FROM myTable WHERE nome_t ILIKE '%selectnothing%';

这是 EXPLAIN ANALYZE VERBOSE

Seq Scan on myTable  (cost=0.00..15259.04 rows=37 width=29) (actual time=2153.061..2153.061 rows=0 loops=1)
  Output: nome_t
  Filter: (nome_t ~~* '%selectnothing%'::text)
Total runtime: 2153.116 ms

myTable 有大约 350k 行,表定义类似于:

CREATE TABLE myTable (
    nome_t text NOT NULL,
)

我在 nome_t 上有一个索引,如下所示:

CREATE INDEX idx_m_nome_t ON myTable
USING btree (nome_t);

尽管这显然是全文搜索的一个很好的候选者,我想暂时排除该选项。
该查询本应从 Web 应用程序运行,目前大约需要 2 秒,这显然太多了;
我可以采取什么措施(例如使用其他索引方法)来提高此查询的速度?

This is my simple query; By searching selectnothing I'm sure I'll have no hits.

SELECT nome_t FROM myTable WHERE nome_t ILIKE '%selectnothing%';

This is the EXPLAIN ANALYZE VERBOSE

Seq Scan on myTable  (cost=0.00..15259.04 rows=37 width=29) (actual time=2153.061..2153.061 rows=0 loops=1)
  Output: nome_t
  Filter: (nome_t ~~* '%selectnothing%'::text)
Total runtime: 2153.116 ms

myTable has around 350k rows and the table definition is something like:

CREATE TABLE myTable (
    nome_t text NOT NULL,
)

I have an index on nome_t as stated below:

CREATE INDEX idx_m_nome_t ON myTable
USING btree (nome_t);

Although this is clearly a good candidate for Fulltext search I would like to rule that option out for now.
This query is meant to be run from a web application and currently it's taking around 2 seconds which is obviously too much;
Is there anything I can do, like using other index methods, to improve the speed of this query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏已如深秋| 2024-10-17 11:41:09

不,ILIKE '%selectnothing%' 总是需要全表扫描,每个索引都是无用的。您需要全文搜索,实现起来并不难。


编辑:你可以使用Wildspeed,我忘记了这个选项。索引会很大,但你的性能也会好得多。

Wildspeed 扩展提供 GIN 索引
支持 LIKE 通配符搜索
运算符。

http://www.sai.msu.su/~megera/wiki/wildspeed

No, ILIKE '%selectnothing%' always needs a full table scan, every index is useless. You need full text search, it's not that hard to implement.


Edit: You could use a Wildspeed, I forgot about this option. The indexes will be huge, but your performance will also be much better.

Wildspeed extension provides GIN index
support for wildcard search for LIKE
operator.

http://www.sai.msu.su/~megera/wiki/wildspeed

絕版丫頭 2024-10-17 11:41:09

您可以做的另一件事 - 是将表 myTable 中的 nome_t 列分解到它自己的表中。从表中搜索一列很慢(如果还有五十个其他宽列),因为其他数据有效地减慢了对该列的扫描速度(因为每页/范围的记录较少)。

another thing you can do-- is break this nome_t column in table myTable into it's own table. Searching one column out of a table is slow (if there are fifty other wide columns) because the other data effectively slows down the scan against that column (because there are less records per page/extent).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文