I want to optimize the following DB SELECT query:
SELECT
ls.caller_id,
ls.caller_path_id,
ls.caller_path_name,
ls.caller_data->>'worker' AS worker,
ls.caller_stage,
ls.current_status,
ls.date_inserted AS date_closed,
ls.caller_data->>'name' AS firstname,
ls.caller_data->>'surname' AS surname,
ls.caller_data->>'cell' AS cell,
ls.caller_data->>'home' AS home,
ls.caller_data->>'work' AS work,
ls.caller_data->>'other' AS other
FROM mv.caller_stage ls
WHERE ls.caller_stage = 'ACTIVE'
AND (REPLACE(ls.caller_data->>'caller_expiry_date', '/', '-' ))::DATE > NOW()::DATE
AND ls.active
AND caller_data @> jsonb_build_object('worker', 'Alice')
AND ls.date_inserted >= (NOW()- CONCAT('3 days')::INTERVAL)
AND ((ls.caller_path_name LIKE 'Mode %' AND ls.current_status IN (
'Received',
'Wrong Number',
'Does Not Know ID',
'No Such Person','Unreachable',
'Under 21',
'No Permission',
'Do Not Contact',
'Not Interested',
'Does Not Understand',
'Query',
'Engaged',
'No Address',
'Wrong ID'))
OR (ls.caller_path_name LIKE 'Method %' AND ls.current_status IN (
'Successfully Captured'
)))
AND ( _search_text IS NULL
OR(
_search_text IS NOT NULL AND(
ls.caller_data->>'surname' ILIKE CONCAT('%',_search_text,'%')
OR ls.caller_data->>'name' ILIKE CONCAT('%',_search_text,'%')
OR ls.caller_data->>'cell' ILIKE CONCAT('%',_search_text,'%')
OR ls.caller_data->>'home' ILIKE CONCAT('%',_search_text,'%')
OR ls.caller_data->>'work' ILIKE CONCAT('%',_search_text,'%')
OR ls.caller_data->>'other' ILIKE CONCAT('%',_search_text,'%')
)
))
Runtime is still too high so I was asked to further optimize it.
I tried adding relevant indexes, and I was able to get a 3x improvement.
Would like to know if I can further improve the SELECT query performance via modifying any of the clauses, such as the IN or ILIKE clauses?
explain(analyze, verbose, buffers)for this statement and the DDL for the table and all its indexes involved? All in plain text, as an update to this question. Without this information, it's going to be almost impossible to help you since we don't know how much time is spent on the query, and where the time is spent.