postgresql - Optimising Hash And Hash Joins in SQL Query -


i have bunch of tables in postgresql , run query follows:

select distinct on ...some stuff...  "rent_flats"  inner join "rent_flats_linked_users"    on "rent_flats_linked_users"."rent_flat_id" = "rent_flats"."id"  inner join "users"    on "users"."id" = "rent_flats_linked_users"."user_id"  inner join "owners"    on "owners"."id" = "users"."profile_id" , "users"."profile_type" = 'owner'  inner join "phone_numbers"    on "phone_numbers"."person_id" = "owners"."id" , "phone_numbers"."person_type" = 'owner'  inner join "phone_number_categories"    on "phone_number_categories"."id" = "phone_numbers"."phone_number_category_id"  inner join "localities"    on "localities"."id" = "rent_flats"."locality_id"  inner join "regions"    on "regions"."id" = "localities"."region_id"  inner join "cities"    on "cities"."id" = "regions"."city_id"  inner join "property_types"    on "property_types"."id" = "rent_flats"."property_type_id"  inner join "apartment_types"    on "apartment_types"."id" = "rent_flats"."apartment_type_id"  "rent_flats"."status" = 3    , (((extract(epoch age(current_date,rent_flats.date_added))/86400)::int) in (cities.short_period,cities.long_period))    , (phone_number_categories.name in ('sms','sms & mobile'))  order rf_id, phone_numbers.priority asc 

note: rent_flats table contains around 5 million rows, , rent_flats_linked_users contains around 600k rows , users contains 350k rows.other tables small in size.

the query takes 6.8 secs execute , explain analyses shows around 99% of total time goes in hash , hash joins.

on setting seq_scan off...the query takes longer ~11 secs

here's explain query plan analyses. have put indices on fields involved in inner joins on fields involved in filters phone_numbers.priority , cities.short_period , cities.long_period. how can optimise further , reduce hash , hash joins times?

your second where clause not sargable:

 , (((extract(epoch age(current_date,rent_flats.date_added))/86400)::int) in (cities.short_period,cities.long_period))  

if involved columns date , integer types (which see in table definition), can rewrite as:

and rent_flats.date_added in (current_date - cities.short_period - 1                             , current_date - cities.long_period - 1) 

which odd predicate. sure don't mean this?

and rent_flats.date_added between current_date - cities.short_period - 1                               , current_date - cities.long_period - 1 

you can lot more, pending missing information. along these lines:


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -