- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在这种情况下,该表有很多文本列,我需要对每一列执行(相似)搜索。我继续为这些列中的每一列创建一个 gin 索引(扩展 pg_trgm 中的 gin_trgm_ops)以加快搜索速度,实际上 yield 很大。这些是常规索引的补充(一些查询使用简单的相等条件)。
但是,测试表明,如果太多条件用 OR 连接在一起,Postgres 将无法选择正确的计划。
查询正确的计划:
db=# explain analyze select topics_id from t_topics_header a
join t_topics_group b using (topics_group_id)
where a.subject ilike '%aaa%' or a.contents ilike '%aaa%' or a.topics_group_id = 7 and
( ( a.ext_col_01 ilike '%aaa%') or ( a.ext_col_50 ilike '%aaa%') or
( a.ext_col_57 ilike '%aaa%') or ( a.ext_col_56 ilike '%aaa%') or
( a.ext_col_63 ilike '%aaa%') or ( a.ext_col_64 ilike '%aaa%') or
( a.ext_col_54 ilike '%aaa%') or ( a.ext_col_69 ilike '%aaa%') or
( a.ext_col_31 ilike '%aaa%') or ( a.ext_col_32 ilike '%aaa%') or
( a.ext_col_41 ilike '%aaa%') or ( a.ext_col_91 ilike '%aaa%') or
( a.ext_col_42 ilike '%aaa%') or ( a.ext_col_92 ilike '%aaa%') or
( a.ext_col_43 ilike '%aaa%') or ( a.ext_col_93 ilike '%aaa%') or
( a.ext_col_44 ilike '%aaa%') or ( a.ext_col_94 ilike '%aaa%') or
( a.ext_col_45 ilike '%aaa%') or ( a.ext_col_95 ilike '%aaa%')
) order by topics_id desc OFFSET 0 LIMIT 10;
查询计划:
Limit (cost=2739.84..2739.87 rows=10 width=4) (actual time=0.437..0.437 rows=4 loops=1)
-> Sort (cost=2739.84..2741.03 rows=473 width=4) (actual time=0.436..0.436 rows=4 loops=1)
Sort Key: a.topics_id DESC
Sort Method: quicksort Memory: 25kB
-> Hash Join (cost=708.03..2729.62 rows=473 width=4) (actual time=0.416..0.426 rows=4 loops=1)
Hash Cond: (a.topics_group_id = b.topics_group_id)
-> Bitmap Heap Scan on t_topics_header a (cost=706.72..2721.80 rows=473 width=8) (actual time=0.381..0.391 rows=4 loops=1)
Recheck Cond: ((subject ~~* '%aaa%'::text) OR (contents ~~* '%aaa%'::text) OR ((ext_col_01 ~~* '%aaa%'::text) OR (ext_col_50 ~~* '%aaa%'::text) OR (ext_col_57 ~~* '%aaa%'::text) OR (ext_col_56 ~~* '%aaa%'::text) OR
(ext_col_63 ~~* '%aaa%'::text) OR (ext_col_64 ~~* '%aaa%'::text) OR (ext_col_54 ~~* '%aaa%'::text) OR (ext_col_69 ~~* '%aaa%'::text) OR (ext_col_31 ~~* '%aaa%'::text) OR (ext_col_32 ~~* '%aaa%'::text) OR (ext_col_41 ~~* '%aaa%'::text) O
R (ext_col_91 ~~* '%aaa%'::text) OR (ext_col_42 ~~* '%aaa%'::text) OR (ext_col_92 ~~* '%aaa%'::text) OR (ext_col_43 ~~* '%aaa%'::text) OR (ext_col_93 ~~* '%aaa%'::text) OR (ext_col_44 ~~* '%aaa%'::text) OR (ext_col_94 ~~* '%aaa%'::text)
OR (ext_col_45 ~~* '%aaa%'::text) OR (ext_col_95 ~~* '%aaa%'::text)))
Filter: ((subject ~~* '%aaa%'::text) OR (contents ~~* '%aaa%'::text) OR ((topics_group_id = 7) AND ((ext_col_01 ~~* '%aaa%'::text) OR (ext_col_50 ~~* '%aaa%'::text) OR (ext_col_57 ~~* '%aaa%'::text) OR (ext_col_56 ~
~* '%aaa%'::text) OR (ext_col_63 ~~* '%aaa%'::text) OR (ext_col_64 ~~* '%aaa%'::text) OR (ext_col_54 ~~* '%aaa%'::text) OR (ext_col_69 ~~* '%aaa%'::text) OR (ext_col_31 ~~* '%aaa%'::text) OR (ext_col_32 ~~* '%aaa%'::text) OR (ext_col_41
~~* '%aaa%'::text) OR (ext_col_91 ~~* '%aaa%'::text) OR (ext_col_42 ~~* '%aaa%'::text) OR (ext_col_92 ~~* '%aaa%'::text) OR (ext_col_43 ~~* '%aaa%'::text) OR (ext_col_93 ~~* '%aaa%'::text) OR (ext_col_44 ~~* '%aaa%'::text) OR (ext_col_
94 ~~* '%aaa%'::text) OR (ext_col_45 ~~* '%aaa%'::text) OR (ext_col_95 ~~* '%aaa%'::text))))
Heap Blocks: exact=4
-> BitmapOr (cost=706.72..706.72 rows=516 width=0) (actual time=0.375..0.375 rows=0 loops=1)
-> Bitmap Index Scan on t_topics_header_idx_subject_gin_trgm (cost=0.00..12.91 rows=122 width=0) (actual time=0.066..0.066 rows=4 loops=1)
Index Cond: (subject ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_contents_gin_trgm (cost=0.00..8.00 rows=1 width=0) (actual time=0.016..0.016 rows=0 loops=1)
Index Cond: (contents ~~* '%aaa%'::text)
-> BitmapOr (cost=685.32..685.32 rows=394 width=0) (actual time=0.292..0.292 rows=0 loops=1)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_01_gin_trgm (cost=0.00..8.85 rows=113 width=0) (actual time=0.014..0.014 rows=0 loops=1)
Index Cond: (ext_col_01 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_50_gin_trgm (cost=0.00..12.06 rows=8 width=0) (actual time=0.022..0.022 rows=0 loops=1)
Index Cond: (ext_col_50 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_57_gin_trgm (cost=0.00..12.12 rows=16 width=0) (actual time=0.022..0.022 rows=0 loops=1)
Index Cond: (ext_col_57 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_56_gin_trgm (cost=0.00..80.00 rows=1 width=0) (actual time=0.023..0.023 rows=0 loops=1)
Index Cond: (ext_col_56 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_63_gin_trgm (cost=0.00..84.05 rows=7 width=0) (actual time=0.020..0.020 rows=0 loops=1)
Index Cond: (ext_col_63 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_64_gin_trgm (cost=0.00..72.04 rows=6 width=0) (actual time=0.020..0.020 rows=0 loops=1)
Index Cond: (ext_col_64 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_54_gin_trgm (cost=0.00..68.00 rows=1 width=0) (actual time=0.022..0.022 rows=0 loops=1)
Index Cond: (ext_col_54 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_69_gin_trgm (cost=0.00..12.02 rows=3 width=0) (actual time=0.022..0.022 rows=0 loops=1)
Index Cond: (ext_col_69 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_31_gin_trgm (cost=0.00..12.04 rows=6 width=0) (actual time=0.021..0.021 rows=0 loops=1)
Index Cond: (ext_col_31 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_32_gin_trgm (cost=0.00..16.76 rows=101 width=0) (actual time=0.022..0.022 rows=0 loops=1)
Index Cond: (ext_col_32 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_41_gin_trgm (cost=0.00..36.00 rows=1 width=0) (actual time=0.021..0.021 rows=0 loops=1)
Index Cond: (ext_col_41 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_91_gin_trgm (cost=0.00..20.58 rows=78 width=0) (actual time=0.020..0.020 rows=0 loops=1)
Index Cond: (ext_col_91 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_42_gin_trgm (cost=0.00..36.00 rows=1 width=0) (actual time=0.005..0.005 rows=0 loops=1)
Index Cond: (ext_col_42 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_92_gin_trgm (cost=0.00..12.19 rows=25 width=0) (actual time=0.008..0.008 rows=0 loops=1)
Index Cond: (ext_col_92 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_43_gin_trgm (cost=0.00..36.00 rows=1 width=0) (actual time=0.004..0.004 rows=0 loops=1)
Index Cond: (ext_col_43 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_93_gin_trgm (cost=0.00..8.20 rows=26 width=0) (actual time=0.006..0.006 rows=0 loops=1)
Index Cond: (ext_col_93 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_44_gin_trgm (cost=0.00..36.00 rows=1 width=0) (actual time=0.004..0.004 rows=0 loops=1)
Index Cond: (ext_col_44 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_94_gin_trgm (cost=0.00..8.04 rows=5 width=0) (actual time=0.006..0.006 rows=0 loops=1)
Index Cond: (ext_col_94 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_45_gin_trgm (cost=0.00..36.00 rows=1 width=0) (actual time=0.004..0.004 rows=0 loops=1)
Index Cond: (ext_col_45 ~~* '%aaa%'::text)
-> Bitmap Index Scan on t_topics_header_idx_ext_col_95_gin_trgm (cost=0.00..76.00 rows=1 width=0) (actual time=0.004..0.004 rows=0 loops=1)
Index Cond: (ext_col_95 ~~* '%aaa%'::text)
-> Hash (cost=1.14..1.14 rows=14 width=4) (actual time=0.027..0.027 rows=14 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 9kB
-> Seq Scan on t_topics_group b (cost=0.00..1.14 rows=14 width=4) (actual time=0.013..0.019 rows=14 loops=1)
Planning time: 3.767 ms
Execution time: 0.687 ms
计划不好的查询:
db=# explain analyze select topics_id from t_topics_header a
join t_topics_group b using (topics_group_id)
where a.subject ilike '%aaa%' or a.contents ilike '%aaa%' or a.topics_group_id = 7 and
( ( a.ext_col_01 ilike '%aaa%') or ( a.ext_col_50 ilike '%aaa%') or
( a.ext_col_57 ilike '%aaa%') or ( a.ext_col_56 ilike '%aaa%') or
( a.ext_col_63 ilike '%aaa%') or ( a.ext_col_64 ilike '%aaa%') or
( a.ext_col_54 ilike '%aaa%') or ( a.ext_col_69 ilike '%aaa%') or
( a.ext_col_31 ilike '%aaa%') or ( a.ext_col_32 ilike '%aaa%') or
( a.ext_col_41 ilike '%aaa%') or ( a.ext_col_91 ilike '%aaa%') or
( a.ext_col_42 ilike '%aaa%') or ( a.ext_col_92 ilike '%aaa%') or
( a.ext_col_43 ilike '%aaa%') or ( a.ext_col_93 ilike '%aaa%') or
( a.ext_col_44 ilike '%aaa%') or ( a.ext_col_94 ilike '%aaa%') or
( a.ext_col_45 ilike '%aaa%') or ( a.ext_col_95 ilike '%aaa%') or
( a.ext_col_70 ilike '%aaa%')
) order by topics_id desc OFFSET 0 LIMIT 10;
请注意,唯一的区别是添加了 ( a.ext_col_70 ilike '%aaa%')
。
新的查询计划变为:
Limit (cost=0.43..2036.67 rows=10 width=4) (actual time=626.343..2784.151 rows=4 loops=1)
-> Nested Loop (cost=0.43..418447.61 rows=2055 width=4) (actual time=626.341..2784.147 rows=4 loops=1)
Join Filter: (a.topics_group_id = b.topics_group_id)
Rows Removed by Join Filter: 52
-> Index Scan Backward using t_report_master_pkey on t_topics_header a (cost=0.43..418014.89 rows=2055 width=8) (actual time=626.328..2784.119 rows=4 loops=1)
Filter: ((subject ~~* '%aaa%'::text) OR (contents ~~* '%aaa%'::text) OR ((topics_group_id = 7) AND ((ext_col_01 ~~* '%aaa%'::text) OR (ext_col_50 ~~* '%aaa%'::text) OR (ext_col_57 ~~* '%aaa%'::text) OR (ext_col_56 ~~* '%a
aa%'::text) OR (ext_col_63 ~~* '%aaa%'::text) OR (ext_col_64 ~~* '%aaa%'::text) OR (ext_col_54 ~~* '%aaa%'::text) OR (ext_col_69 ~~* '%aaa%'::text) OR (ext_col_31 ~~* '%aaa%'::text) OR (ext_col_32 ~~* '%aaa%'::text) OR (ext_col_41 ~~* '
%aaa%'::text) OR (ext_col_91 ~~* '%aaa%'::text) OR (ext_col_42 ~~* '%aaa%'::text) OR (ext_col_92 ~~* '%aaa%'::text) OR (ext_col_43 ~~* '%aaa%'::text) OR (ext_col_93 ~~* '%aaa%'::text) OR (ext_col_44 ~~* '%aaa%'::text) OR (ext_col_94 ~~*
'%aaa%'::text) OR (ext_col_45 ~~* '%aaa%'::text) OR (ext_col_95 ~~* '%aaa%'::text) OR (ext_col_70 ~~* '%aaa%'::text))))
Rows Removed by Filter: 1237807
-> Materialize (cost=0.00..1.21 rows=14 width=4) (actual time=0.002..0.003 rows=14 loops=4)
-> Seq Scan on t_topics_group b (cost=0.00..1.14 rows=14 width=4) (actual time=0.004..0.007 rows=14 loops=1)
Planning time: 3.704 ms
Execution time: 2784.262 ms
我有大约两倍的 ext_cols(我将它们减少到计划更改的最小断点)。我在每个 ext_col 上都有 gin pg_trgm 索引,除了其中一些的常规索引(双重检查)。我还在两个表上运行 VACUUM ANALYZE。
数据库版本为 PostgreSQL 9.6.6
那么有没有办法向分析器提示使用位图索引?有什么想法吗?
编辑:显然字符串的长度很重要。对于 3 个或更少的字母,分析器选择(坏的)索引扫描。对于4个或更多的字符串,选择好的(位图索引)方案。
最佳答案
如果您坚持使用这种设计,您可以在所有列的串联值上创建一个索引,并在您的 where 条件中使用该表达式。
类似于:
create function query_columns(p_rec t_topics_header)
returns text
as
$$
select concat_ws(' ', p_rec.ext_col_01, p_rec.ext_col_50, p_rec.ext_col_57, p_rec.ext_col_56);
$$
language sql
immutable;
将 concat_ws()
函数中的列数调整为您要查询的列数。我不确定 GIN 索引的索引表达式的最大长度是多少。也许您的许多列会超过该限制。
然后您可以在该函数上创建索引:
create index on t_topics_header using gin ( (query_columns(x)) public.gin_trgm_ops);
那么下面应该使用索引:
select topics_id
from t_topics_header a
join t_topics_group b using (topics_group_id)
where (a.subject ilike '%aaa%' or a.contents ilike '%aaa%'
or a.topics_group_id = 7 )
and query_columns(a) ilike ='%aaa%';
关于postgresql - Postgres : forcing analyzer to use bitmap scan instead of index scan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49063990/
据我所知,有两种复制位图的方法。 Bitmap.Clone() Bitmap A = new Bitmap("somefile.png"); Bitmap B = (Bitmap)A.Clone();
我收到这个错误,我不知道为什么或不明白原因: vector fourier_descriptor(Gdiplus::Bitmap myBitmap) { vector res;
我试图了解位图原始数据是如何存储的。我读过很多关于位图存储的文章,但有一篇文章指出位图文件的原始位域数据将以相反的顺序 (ABGR) 存储。但是,我找到了另一个显示 ARGB 的图表。因此,我不确定如
Bitmap bmp = BitmapFactory.decodeStream(inputStream, null, op); bmp.getConfig() = null; 为什么bmp.getC
位图中有300帧图像,设置的帧率为30,但视频长度只有3秒;如果有600帧图像,则视频长度为6秒等;视频可以播放所有内容,但播放速度更快,速度是两倍或三倍;原因是什么?应该如何(encodeVideo
List url = new ArrayList(); public Bitmap[] thumbs = { }; 我从我的方法中获取图像fetchImage(String url) for (int
AFAIK 在 Android 上,建议将 Bitmap 对象引用为 WeakReferences 以避免内存泄漏。当不再保留位图对象的硬引用时,垃圾收集器将自动收集它。 现在,如果我理解正确,必须始
我必须从 XML 文件加载图像。 XML 文件中没有关于图像是否为 JPG/GIF/BMP 的信息。加载图像后,我需要将其转换为位图。 有人知道如何在不知道实际文件格式的情况下将图像转换为位图吗?我正
几天前,我们在 Play 商店发布了一个应用程序,它处理高质量的位图并且完全是关于编辑它们。 当我们意识到 20% 的设备出现内存不足错误时,一切进展顺利。所以我们检查了我们的代码,发现 Androi
您好,我已经加载了位图,我需要设置自己的高度和宽度, bitmap.height = 100; 但是这个声明不允许我因为它说 'System.Drawing.Image.Width' cannot b
这是我写的测试,目前会失败: var unusableColor = Color.FromArgb(13, 19, 20, 19); var retrievedColor = Color.Empty;
我是否还需要在 Bitmap.Recycle() 之后调用 Bitmap.Dispose()?或者只是 Bitmap.Dispose() 就足够了? 最佳答案 根据 Android 文档 Bitmap
我试图将所有小图像(如草、水和沥青等)放入一张位图中。 我有一个这样的数组: public int Array[]={3, 1, 3, 3, 1, 1, 3, 3, 3, 3,
开发人员 website简单地说 getHeight() 将返回位图的高度,但有人可以告诉我是像素单位还是 dp 单位? 最佳答案 这是像素。在 Java 代码中,您通常使用像素,例如 View 的宽
我正在 F# 中编写一个项目,该项目使用 System.Drawing、NET Standard 2.0 的位图功能,但无法构建该项目,因为类型“Bitmap”不是在“System.Drawing”中
我正在尝试在 Android 中扩展可缩放的图像查看器以并排使用两个图像。为此,我使用了 Bitmap.createBitmap(int, int, Bitmap.Config)。不幸的是,这似乎会使
我正在使用 Java 创建这款 Android 游戏。但是,我加载位图,然后调整它们的大小以适合屏幕等(dpi 不是很准确)。但我的想法也是为具有少量 ram 的设备加载 16b (mBitmapOp
API 26 adds new option Bitmap.Config.HARDWARE: Special configuration, when bitmap is stored only in
11-24 23:19:18.434: ERROR/AndroidRuntime(12660): Uncaught handler: thread main exiting due to uncaug
我有一组位图。它们在某种程度上都是透明的,我事先不知道哪些部分是透明的。我想从排除透明部分但在正方形中的原始位图中创建一个新位图。我认为这张图片解释了这一点: 我知道如何从现有位图中创建位图,但我不知
我是一名优秀的程序员,十分优秀!