我有一个查询插入语句,如下所示
struct win32_struct
{
DWORD stuff;
char data[1];
};
#pragma pack(push, 1)
template <size_t DATA_SIZE>
struct my_struct
{
win32_struct base;
char rest_of_data[DATA_SIZE - (sizeof(win32_struct) - offsetof(win32_struct, data))];
};
#pragma pack(pop)
这里我们有case语句,它引用相同的表(TABLE_A)但在每个语句中检查不同的值(XXX,YYY等等)。这花了很多时间。我们正在尝试优化此 insert into my_table
select col1,col2,CASE
WHEN EXISTS
(SELECT 1 FROM Table_A m WHERE m.COLA='XXX' AND m.id=b.id
)
THEN
(SELECT m.COLB FROM Table_A m WHERE m.COLA='XXX' AND m.id=b.id
)
ELSE 'Not Open'
END XXX,
CASE
WHEN EXISTS
(SELECT 1 FROM Table_A m WHERE m.COLA='YYY' AND m.id=b.id
)
THEN
(SELECT m.COLB FROM Table_A m WHERE m.COLA='YYY' AND m.id=b.id
)
ELSE 'Not Open'
END YYY,
.
.
.
.
. -- ~150 case statement
from Test b,table_main c
where b.id=c.id
查询。有人可以在这帮忙吗?
答案 0 :(得分:1)
尝试透视查询的变体:
<checkbox-input checked.bind="selectAll"></checkbox-input>
toggleAll()
{
this.items.forEach( item =>
{
item.selected = this.selectAll;
});
this.showHideToolbar();
}
答案 1 :(得分:0)
使用decode
和LEFT JOIN
重写如下。
SELECT col1, col2 ,
decode ( m.COLA , 'XXX' , m.COLB , 'Not Open' ) XXX ,
decode ( m.COLA , 'YYY', m.COLB , 'Not Open' ) YYY ,
...
...
FROM Test b LEFT JOIN Table_A m ON m.id=b.id JOIN table_main c
ON b.id=c.id;
答案 2 :(得分:-1)
这需要时间,因为您要对表格进行多次调用以获取您的值。只需减少对Table_A的调用次数。
如果条件Table_A.id = m.id且m.COLA ='Any Value'将始终返回一行,那么您可以将表Table_A,Test和Table_main连接到单个连接中,如下所示
Insert into my_table(col1, col2) values
select
decode(a.COLA,'XXX' , a.COLB , 'Not Open'),
decode(a.COLA,'YYY' , a.COLB , 'Not Open'),
...
...
from Table_A a, Test b, Table_main c
where a.id = b.id
and b.id = c.id
如果条件可能返回多个记录,那么您可以使用如下的With子句
Insert into my_table(col1, col2) values
with table_a as (select col1, col2 , a.COLA, a.COLB
from Table_A a, Test b, Table_main c
where a.id = b.id
and b.id = c.id
group by col1, col2,a.COLA, a.COLB)
select col1,col2,
decode(x.COLA,'XXX' , x.COLB , 'Not Open'),
decode(x.COLA,'YYY' , x.COLB , 'Not Open'),
....
....
from table_a x