I have a query like this which use index on call_id while if I add _ in value then it changes from index search to seq search.
explain analyze
DELETE
FROM completedcalls
WHERE call_id like '560738a563616c6c004c7621@198.148.114.67-b2b1';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Delete on completedcalls (cost=0.00..8.67 rows=1 width=6) (actual time=0.036..0.036 rows=0 loops=1)
-> Index Scan using i_call_id on completedcalls (cost=0.00..8.67 rows=1 width=6) (actual time=0.034..0.034 rows=0 loops=1)
Index Cond: ((call_id)::text = '560738a563616c6c004c7621@198.148.114.67-b2b1'::text)
Filter: ((call_id)::text ~~ '560738a563616c6c004c7621@198.148.114.67-b2b1'::text)
Total runtime: 0.069 ms
(5 rows)
This statement:
explain analyze
DELETE
FROM completedcalls
WHERE call_id like '560738a563616c6c004c7621@198.148.114.67-b2b_1';
Returns this execution plan:
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Delete on completedcalls (cost=0.00..39548.64 rows=84 width=6) (actual time=194.313..194.313 rows=0 loops=1)
-> Seq Scan on completedcalls (cost=0.00..39548.64 rows=84 width=6) (actual time=194.310..194.310 rows=0 loops=1)
Filter: ((call_id)::text ~~ '560738a563616c6c004c7621@198.148.114.67-b2b_1'::text)
Total runtime: 194.349 ms
(4 rows)
My Question is how to escape these characters in query. Using psycopg2 in python.
You would need to escape the
_with a backslash, like so:Also, if you don’t want pattern matching, it would probably make more sense to use
=instead ofLIKE.