Can anyone explain such a big performance difference between these SQL?
SELECT count(*) as cnt FROM table WHERE name ~ '\*{3}';
As you can see, the difference more than doubles between the LIKE operator and a simple regular expression (I thought that the LIKE operator would be internally converted to a regular expression and there shouldn't be any difference)
There are almost 13,000 rows here, and the "name" column is of type "text". There are no indexes associated with the "name" column defined in the table.
EDIT:
EXPLAIN ANALYSIS OF EACH OF THEM:
EXPLAIN ANALYZE SELECT count(*) as cnt FROM datos WHERE nombre ~ '\*{3}'; Aggregate (cost=894.32..894.33 rows=1 width=0) (actual time=18.279..18.280 rows=1 loops=1) -> Seq Scan on datos (cost=0.00..894.31 rows=1 width=0) (actual time=0.620..18.266 rows=25 loops=1) Filter: (nombre ~ '\*{3}'::text) Total runtime: 18.327 ms
EXPLAIN ANALYZE SELECT count(*) as cnt FROM datos WHERE nombre ~ '\*\*\*'; Aggregate (cost=894.32..894.33 rows=1 width=0) (actual time=17.404..17.405 rows=1 loops=1) -> Seq Scan on datos (cost=0.00..894.31 rows=1 width=0) (actual time=0.608..17.396 rows=25 loops=1) Filter: (nombre ~ '\*\*\*'::text) Total runtime: 17.451 ms
EXPLAIN ANALYZE SELECT count(*) as cnt FROM datos WHERE nombre LIKE '%***%'; Aggregate (cost=894.32..894.33 rows=1 width=0) (actual time=4.258..4.258 rows=1 loops=1) -> Seq Scan on datos (cost=0.00..894.31 rows=1 width=0) (actual time=0.138..4.249 rows=25 loops=1) Filter: (nombre ~~ '%***%'::text) Total runtime: 4.295 ms
source share