多表关联的视图产生重复行的原因 ----笛卡尔积

今天处理两个报障都是关于视图优化的问题
在视图中看到重复行很多
视图语句如下
 select 
left(b.code_record,10) as DJLSH,
(CASE d.yl_code WHEN 'QT' then (d.yl_code+'-'+left(a.ybh,2)) else  d.yl_code end as XMBH,
d.item_name as XMMC,
c.test_value2 as JG,
c.text_danwei as DW,
c.text_range as CKFW,
e.name as SHR,
a.date_report as SHRQ,
c.text_note as PROMPT
from ut_check_result c, ut_check_collection b
,ut_check_patient a
,UT_DICT_USER e
,UT_DICT_ITEMS d
where 
b.date_Test=c.test_date and b.ybh=c.ybh
and a.date_Test=c.test_date and a.ybh=c.ybh
and a.Reporter=e.user_id
and b.patient_kind='3'
and a.flag_send='5'
and b.code_record<>''
and b.date_test>'2014-01-01'
and c.item_code=d.item_code

因为关联的表较多,如果不对表字段熟悉,没有过多去关注一对多的原因,产生笛卡尔积数据是几何倍数增长的
以后如果遇到此类状况应第一时间想到这个问题
 

posted on 2015-10-14 17:43  MaxGeek  阅读(123)  评论(0)    收藏  举报