sql server - Query slowdown when joining small table -
i've hit strange problem. have query uses 4 tables (sorry latvian names):
- kl_precesizejvielas: 20 rows
- parvietots_details: 27897 rows
- razots: 282 rows
- kl_simple: 25 rows
tables have clustered index (primary key), no other indexes.
i'm executing query:
select kl_precesizejvielas.apraksts precenosauk , count(*) razosanuskaits , sum(skaits) kopskaits , mervienibas.apraksts mervieniba kl_simple mervienibas inner join ( kl_precesizejvielas inner join ( parvietots_details inner join razots on parvietots_details.id_parvietots_master = razots.id_preces_ieks_kust_id ) on kl_precesizejvielas.id = parvietots_details.id_prece_izejviela ) on mervienibas.id = kl_precesizejvielas.default_mervieniba razots.id_atbildpersona = 27 , razots.datumslaiks >= ( select top 1 datums loginhistory id_user = 27 order datums desc ) group kl_precesizejvielas.apraksts , mervienibas.apraksts order kl_precesizejvielas.apraksts , mervienibas.apraksts
here execution plan: link bigger picture
the query takes 6 seconds process! execution plan shows biggest work done when joining big parvietots_details table kl_precesizejvielas , done before applying filter on "razots". seems wrong.
the strange thing is, if take away small 25-row table kl_simple/mervienibas execution time 126 milliseconds. big table joined after applying filter on "razots" , think makes difference (?)
select kl_precesizejvielas.apraksts precenosauk , count(*) razosanuskaits , sum(skaits) kopskaits kl_precesizejvielas inner join ( parvietots_details inner join razots on parvietots_details.id_parvietots_master = razots.id_preces_ieks_kust_id ) on kl_precesizejvielas.id = parvietots_details.id_prece_izejviela razots.id_atbildpersona = 27 , razots.datumslaiks >= ( select top 1 datums loginhistory id_user = 27 order datums desc ) group kl_precesizejvielas.apraksts order kl_precesizejvielas.apraksts
what cause of slowdown? should rewrite query? why query processor chooses such plan?
i tried dropping statistics, there no improvement.
edit: here execution plan xmls:
slow query
fast query
its difficult read screenshot/sql because of size/language. looks if way structuring join kl_simple means rest of query being repeated every row in kl_simple.
Comments
Post a Comment