SELECT
SUM(cost*ld_qty_oh) AS js,
SUM(ld_qty_oh) AS js1
FROM VIEW_ld_det
WHERE (
((pt_prod_line NOT LIKE 'cut%') AND
(pt_prod_line<>'mold') AND
(ltrim(pt_prod_line)<>'spa') AND
(pt_prod_line<>'raad') AND
(Type<>'somi') AND
(Type <> 'wip') AND
(Type <> 'wipn') AND
(ld_loc <> 'cl13') AND
(pt_prod_line NOT LIKE 'c%')) OR
((pt_prod_line NOT LIKE 'cut%') AND
(pt_prod_line<>'mold') AND
(ltrim(pt_prod_line)<>'spa') AND
(pt_prod_line<>'raad') AND
(Type<>'somi') AND
(Type = 'wip') AND
(ld_loc <> 'cl13') AND
(pt_prod_line NOT LIKE 'c%')) OR
((pt_prod_line NOT LIKE 'cut%') AND
(pt_prod_line<>'mold') AND
(ltrim(pt_prod_line)<>'spa') AND
(pt_prod_line<>'raad') AND
(Type<>'somi') AND
(Type = 'wipn') AND
(ld_loc <> 'cl13') AND
(pt_prod_line NOT LIKE 'c%'))) and
rq='2012-01-21'
这是从一个视图查询统计,视图来自多个表,执行他的时候总是超时,如果把rq='2012-01-21'去掉,可以正常执行,这是什么原因?rq字段是varchar 视图中大概有数据18万多条
------解决方案--------------------
先以UNION ALL为分界线,单独执行3部分,如果都不慢,一起执行试试
SELECT SUM(cost * ld_qty_oh) AS js ,
SUM(ld_qty_oh) AS js1
FROM VIEW_ld_det
WHERE ( ( pt_prod_line NOT LIKE 'cut%' )
AND ( pt_prod_line <> 'mold' )
AND ( LTRIM(pt_prod_line) <> 'spa' )
AND ( pt_prod_line <> 'raad' )
AND ( Type <> 'somi' )
AND ( Type <> 'wip' )
AND ( Type <> 'wipn' )
AND ( ld_loc <> 'cl13' )
AND ( pt_prod_line NOT LIKE 'c%' )
)
AND rq = '2012-01-21'
UNION ALL
SELECT SUM(cost * ld_qty_oh) AS js ,
SUM(ld_qty_oh) AS js1
FROM VIEW_ld_det
WHERE ( ( pt_prod_line NOT LIKE 'cut%' )
AND ( pt_prod_line <> 'mold' )
AND ( LTRIM(pt_prod_line) <> 'spa' )
AND ( pt_prod_line <> 'raad' )
AND ( Type <> 'somi' )
AND ( Type = 'wip' )
AND ( ld_loc <> 'cl13' )
AND ( pt_prod_line NOT LIKE 'c%' )
)
AND rq = '2012-01-21'
UNION ALL
SELECT SUM(cost * ld_qty_oh) AS js ,
SUM(ld_qty_oh) AS js1
FROM VIEW_ld_det
WHERE ( ( pt_prod_line NOT LIKE 'cut%' )
AND ( pt_prod_line <> 'mold' )
AND ( LTRIM(pt_prod_line) <> 'spa' )
AND ( pt_prod_line <> 'raad' )
AND ( Type <> 'somi' )
AND ( Type = 'wipn' )
AND ( ld_loc <> 'cl13' )
AND ( pt_prod_line NOT LIKE 'c%' )
)