我现有的sql查询与各种表中的条件数据提取非常慢,需要了解如何优化查询。
现有查询正在尝试从“材料”表中获取包含某些条件的材料信息。如果'Config'表中有'printer',则只返回'Config'表中'材料'的信息。如果“配置”表中没有“打印机”,请从“可用物料”表中返回所有物料的信息。这是使用case和left join来处理的。但是在'连接'和'where'子句中重复了所需的表达式。因此需要一种方法来存储某些子查询的“结果”并在以后重用它。我有sqlite查询,如下所示: -
select m.name from material m
left join available_material_printmode_config ac on ac.material_name = m.name and
(SELECT EXISTS (SELECT 1 FROM available_material_printmode_config
WHERE printer_name ='123' LIMIT 1)) and
(SELECT case when material_name is null then 0 else 1 end
FROM available_material_printmode_config WHERE printer_name ='123'
and printmode_name = '2' LIMIT 1)
left join available_materials am1 on am1.material_name = m.name
and ((SELECT EXISTS (SELECT 1 FROM available_material_printmode_config
WHERE printer_name ='123' LIMIT 1)) !=1
or ((SELECT EXISTS (SELECT 1 FROM available_material_printmode_config
WHERE printer_name ='123' LIMIT 1))
and (SELECT case when material_name is null then 1 else 0 end
FROM available_material_printmode_config WHERE printer_name ='123'
and printmode_name = '2' LIMIT 1)))
where
case
when 'true' == (select case when exists
(select distinct 1 from available_material_printmode_config where printer_name = '123')
THEN 'true' ELSE 'false' END)
and (SELECT case when material_name is null then 0 else 1 end
FROM available_material_printmode_config WHERE printer_name ='123'
and printmode_name = '2' LIMIT 1)
then
ac.printer_name = '123' and ac.printmode_name = '2'
else
am1.printer_name = '123'
end
想要知道是否有任何方法可以重复使用下面提到的sqls的结果:
(SELECT EXISTS (SELECT 1 FROM available_material_printmode_config
WHERE printer_name ='123' LIMIT 1))
和
(SELECT case when material_name is null then 0 else 1 end
FROM available_material_printmode_config
WHERE printer_name ='123' and printmode_name = '2' LIMIT 1)
或者我想出了类似的东西: -
with
printercheck as (SELECT EXISTS (SELECT 1 FROM available_material_printmode_config
WHERE printer_name ='123' LIMIT 1) as p),
materialcheck as (SELECT case when material_name is null then 0 else 1 end as m
FROM available_material_printmode_config WHERE printer_name ='123'
and printmode_name = '2' LIMIT 1)
select m.name from material m
left join available_material_printmode_config ac on ac.material_name = m.name
and (select p from printercheck limit 1)
and (select m from materialcheck limit 1)
left join available_materials am1 on am1.material_name = m.name
and ((select p from printercheck limit 1) != 1
or ((select p from printercheck limit 1)
and (select m from materialcheck limit 1) !=1 ))
where
case
when (select p from printercheck limit 1) and (select m from materialcheck limit 1)
then
ac.printer_name = '123' and ac.printmode_name = '2'
else
am1.printer_name = '123'
end
这是我现在使用的最终查询:灵感来自已接受的答案
select m.name, m.color from material m inner join
available_material_printmode_config ac on ac.material_name is not null
and m.name=ac.material_name
where ac.printmode_name='2' and ac.printer_name='123'
UNION
select m.name,m.color from material m
inner join available_materials am on m.name=am.material_name
where (not exists (select distinct 1 from available_material_printmode_config
where printer_name = am.printer_name limit 1)
or exists (select 1 from available_material_printmode_config
where printer_name = am.printer_name and printmode_name = '2'
and material_name is null LIMIT 1)) and am.printer_name='123';
我的原始查询有28个步骤,另外一个有20个步骤,最终查询有9个步骤。步骤基本上是我使用解释查询计划 sql命令。
答案 0 :(得分:2)
考虑两组的联合查询。虽然没有发布数据或架构,但下面可能需要调整。此外,子查询现在与外部查询行相关联:
Dim str as String = 'some text
Dim btn as Button = FlowLayoutPanel.Controls.Button.Text with that string
'do something with btn