Edit - 3
Added two new indexes on lower(brand) and created_at in products
CREATE INDEX products_lower_brand_created_at_idx ON products (created_at, lower(brand));
CREATE INDEX brands_lower_brand_idx ON products (lower(name));
Here is the EXPLAIN plan for the same query
Update on products (cost=7.97..435.50 rows=1 width=3187)
-> Nested Loop Semi Join (cost=7.97..435.50 rows=1 width=3187)
Join Filter: (products.id = "ANY_subquery".id)
-> Hash Join (cost=7.54..426.80 rows=77 width=3159)
Hash Cond: (lower((brands.name)::text) = lower((products.brand)::text))
-> Seq Scan on brands (cost=0.00..321.76 rows=15476 width=24)
-> Hash (cost=7.53..7.53 rows=1 width=3149)
-> Index Scan using products_lower_brand_created_at_idx on products (cost=0.43..7.53 rows=1 width=3149)
Index Cond: (created_at >= ('now'::cstring)::date)
Filter: (clean_brand_id IS NULL)
-> Materialize (cost=0.43..7.54 rows=1 width=32)
-> Subquery Scan on "ANY_subquery" (cost=0.43..7.54 rows=1 width=32)
-> Limit (cost=0.43..7.53 rows=1 width=4)
-> Index Scan using products_lower_brand_created_at_idx on products products_1 (cost=0.43..7.53 rows=1 width=4)
Index Cond: (created_at >= ('now'::cstring)::date)
Filter: (clean_brand_id IS NULL)
These changes are implemented in local database and number of records are around .4 million. Can anyone help to findout if the new index can help in improving the query execution time