I have around 700 tables in my DB.When I run the script below, it takes almost 1 hour. How can I optimised this query?
Database: Postgres
DO $$
DECLARE
tables_list CURSOR FOR
select distinct t_name, t_schema from information_schema.columns
where column_name = 'deleted_flag'
and t_schema='customer' and t_name not like 'v_%';
BEGIN
FOR t_record IN tables_list LOOP
EXECUTE 'update ' || t_record.table_schema || '.' || t_record.table_name || ' set deleted_flag=false';
END LOOP;
end;
$$;
At the end, all the tables in this schema should have this field deleted_flag as false. I have to run this script very often on production environment. Grateful if someone can help how to optimise this script.
Do you think if add a check in the where clause,
EXECUTE 'update ' || t_record.table_schema || '.' || t_record.table_name || ' set deleted_flag=false where deleted_flag=true';
the execution time will be less?
FORMAT()function instead (which handles mixed case and embedded whithespace in identifiers)