PostgreSQL 性能,使用仅两个百分比的 ILIKE 与完全不使用
PostgreSQL performance, using ILIKE with just two percentages versus not at all
我正在使用 ILIKE 根据用户输入搜索行的标题。当用户没有输入任何内容(空字符串)时,所有行都应该 return.
如果使用 ILIKE '%%'
查询 SELECT 语句与完全不使用 SELECT 语句,性能会有所不同吗?换句话说,如果没有搜索过滤器文本,只查询 ILIKE 是否可以,或者我应该在查询中删除它吗?
在 PostgreSQL (13.1) 上,两个查询不等价:
test=# select count(*) from test_ilike where test_string ilike '%%';
count
--------
100000
(1 row)
Time: 87,211 ms
test=# select count(*) from test_ilike where test_string ilike '';
count
-------
0
(1 row)
Time: 85,521 ms
test=# explain analyze select count(*) from test_ilike where test_string ilike '%%';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Aggregate (cost=2333.97..2333.99 rows=1 width=8) (actual time=86.859..86.860 rows=1 loops=1)
-> Seq Scan on test_ilike (cost=0.00..2084.00 rows=99990 width=0) (actual time=0.022..81.497 rows=100000 loops=1)
Filter: (test_string ~~* '%%'::text)
Planning Time: 0.313 ms
Execution Time: 86.893 ms
(5 rows)
Time: 87,582 ms
test=# explain analyze select count(*) from test_ilike where test_string ilike '';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Aggregate (cost=2084.00..2084.01 rows=1 width=8) (actual time=83.223..83.224 rows=1 loops=1)
-> Seq Scan on test_ilike (cost=0.00..2084.00 rows=1 width=0) (actual time=83.219..83.219 rows=0 loops=1)
Filter: (test_string ~~* ''::text)
Rows Removed by Filter: 100000
Planning Time: 0.104 ms
Execution Time: 83.257 ms
(6 rows)
Time: 83,728 ms
there a performance difference if you query a SELECT statement with ILIKE '%%' versus without it at all?
两个查询:
select *
from some_table
where some_column ILIKE '%'
和
select *
from some_table
将return不同的结果。
第一个等效于 where some_column is not null
- 因此它永远不会 return 行 some_column
为空,但第二个会。
所以这不仅关乎性能,还关乎正确性。
在性能方面,它们很可能是相同的 - 在两种情况下都执行 Seq Scan
。
我正在使用 ILIKE 根据用户输入搜索行的标题。当用户没有输入任何内容(空字符串)时,所有行都应该 return.
如果使用 ILIKE '%%'
查询 SELECT 语句与完全不使用 SELECT 语句,性能会有所不同吗?换句话说,如果没有搜索过滤器文本,只查询 ILIKE 是否可以,或者我应该在查询中删除它吗?
在 PostgreSQL (13.1) 上,两个查询不等价:
test=# select count(*) from test_ilike where test_string ilike '%%';
count
--------
100000
(1 row)
Time: 87,211 ms
test=# select count(*) from test_ilike where test_string ilike '';
count
-------
0
(1 row)
Time: 85,521 ms
test=# explain analyze select count(*) from test_ilike where test_string ilike '%%';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Aggregate (cost=2333.97..2333.99 rows=1 width=8) (actual time=86.859..86.860 rows=1 loops=1)
-> Seq Scan on test_ilike (cost=0.00..2084.00 rows=99990 width=0) (actual time=0.022..81.497 rows=100000 loops=1)
Filter: (test_string ~~* '%%'::text)
Planning Time: 0.313 ms
Execution Time: 86.893 ms
(5 rows)
Time: 87,582 ms
test=# explain analyze select count(*) from test_ilike where test_string ilike '';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Aggregate (cost=2084.00..2084.01 rows=1 width=8) (actual time=83.223..83.224 rows=1 loops=1)
-> Seq Scan on test_ilike (cost=0.00..2084.00 rows=1 width=0) (actual time=83.219..83.219 rows=0 loops=1)
Filter: (test_string ~~* ''::text)
Rows Removed by Filter: 100000
Planning Time: 0.104 ms
Execution Time: 83.257 ms
(6 rows)
Time: 83,728 ms
there a performance difference if you query a SELECT statement with ILIKE '%%' versus without it at all?
两个查询:
select *
from some_table
where some_column ILIKE '%'
和
select *
from some_table
将return不同的结果。
第一个等效于 where some_column is not null
- 因此它永远不会 return 行 some_column
为空,但第二个会。
所以这不仅关乎性能,还关乎正确性。
在性能方面,它们很可能是相同的 - 在两种情况下都执行 Seq Scan
。