[英]MySQL: improving efficiency of sub-select query
我有一组(MySQL)表,如下所示。 我需要根据匹配的型号/序列号检索“故障单”列表以及任一设备表中的匹配设备的ID。
请注意,型号/序列号组合在两个设备表中应该是唯一的,但是型号/序列号输入框是自由格式的,因此用户有可能两次输入相同的型号/序列号。 在这种情况下,检索哪个设备单位都没有关系,但是每张票仅应返回结果,因为我们显示的是票清单,而不是设备清单。
'票'[故障票表]
user_equipment [用户设备表]
site_equipment [现场设备]
当前,我正在使用子查询来返回用户和站点设备ID,但是性能很差:
SELECT tickets.*, ... other tables/columns here,
(SELECT id FROM user_equipment WHERE model = tickets.model AND serial = tickets.serial LIMIT 1) as user_equipment_id,
(SELECT id FROM site_equipment WHERE model = tickets.model AND serial = site_equipment.serial LIMIT 1) as site_equipment_id
FROM
tickets
... other joins here
WHERE ...
HAVING ...
ORDER BY ...
LIMIT ...
我将不胜感激任何有关改善此查询性能的建议。 不幸的是,由于许多其他依赖关系,此时更改表结构不是一个选择。
谢谢!
您想要索引:
user_equipment(model, serial, id)
site_equipment(model, serial, id)
前两列可以采用任何顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.