请问mysql专家可以帮助我优化子查询这个复杂的视图定义吗?
问题是我只需要获得每次计算的最后状态(按日期排序)。 是否有可能将其转换为某种另一种左连接? 或者我应该选择哪种方法,因为它变得非常慢。
感谢您提供任何帮助或建议。
CREATE VIEW admin_calculation_state_view AS SELECT
`povruc_calculation`.`id`,
`povruc_session`.`id` AS sessionId,
`povruc_calculation`.`createdDate`,
CONCAT(`povruc_calculation_subject`.`safeMonths`, '/', `povruc_calculation_subject`.`eventCount`) AS relevantTime,
`povruc_calculation_subject`.`phone`,
`povruc_calculation_subject`.`email`,
`povruc_calculation_vehicle`.`manufacturer`,
`povruc_calculation_vehicle`.`model`,
`povruc_calculation_vehicle`.`engineDisplacement`,
`povruc_calculation`.`priceMin`,
`admin_user`.`username`,
(SELECT `povruc_calculation_status`.`status` FROM `povruc_calculation_status` WHERE `povruc_calculation`.`id` = `povruc_calculation_status`.`calculation` ORDER BY `povruc_calculation_status`.`date` DESC LIMIT 1) AS lastStatus,
(SELECT `povruc_calculation_status`.`message` FROM `povruc_calculation_status` WHERE `povruc_calculation`.`id` = `povruc_calculation_status`.`calculation` ORDER BY `povruc_calculation_status`.`date` DESC LIMIT 1) AS lastStatusMessage,
(SELECT `povruc_calculation_statustype`.`name` FROM `povruc_calculation_status` LEFT JOIN `povruc_calculation_statustype` ON `povruc_calculation_status`.`status`=`povruc_calculation_statustype`.`id` WHERE `povruc_calculation`.`id` = `povruc_calculation_status`.`calculation` ORDER BY `povruc_calculation_status`.`date` DESC LIMIT 1) AS lastStatusName FROM `povruc_calculation`
LEFT JOIN `povruc_calculation_subject` on `povruc_calculation_subject`.`id` = `povruc_calculation`.`subject`
LEFT JOIN `povruc_session` on `povruc_calculation`.`session` = `povruc_session`.`id`
LEFT JOIN `povruc_calculation_vehicle` on `povruc_calculation_vehicle`.`id` = `povruc_calculation`.`vehicle`
LEFT JOIN `admin_user` on `povruc_calculation`.`creatorOwner` = `admin_user`.`id`;
答案 0 :(得分:0)
从SELECT中移动相关的子查询,并将它们更改为JOIN,这可能会更有效。
这样的事情: -
SELECT
povruc_calculation.id,
povruc_session.id AS sessionId,
povruc_calculation.createdDate,
CONCAT(povruc_calculation_subject.safeMonths, '/', povruc_calculation_subject.eventCount) AS relevantTime,
povruc_calculation_subject.phone,
povruc_calculation_subject.email,
povruc_calculation_vehicle.manufacturer,
povruc_calculation_vehicle.model,
povruc_calculation_vehicle.engineDisplacement,
povruc_calculation.priceMin,
admin_user.username,
povruc_calculation_status.status AS lastStatus,
povruc_calculation_status.message AS lastStatusMessage,
povruc_calculation_statustype.name AS lastStatusName
FROM povruc_calculation
LEFT OUTER JOIN
(
SELECT calculation, MAX(date) AS MaxDate
FROM povruc_calculation_status
GROUP BY calculation
) Sub1
ON povruc_calculation.id = Sub1.calculation
LEFT OUTER JOIN povruc_calculation_status ON povruc_calculation_status.calculation = Sub1.calculation AND povruc_calculation_status.date = Sub1.MaxDate
LEFT OUTER JOIN povruc_calculation_statustype ON povruc_calculation_status.status = povruc_calculation_statustype.id
LEFT JOIN povruc_calculation_subject on povruc_calculation_subject.id = povruc_calculation.subject
LEFT JOIN povruc_session on povruc_calculation.session = povruc_session.id
LEFT JOIN povruc_calculation_vehicle on povruc_calculation_vehicle.id = povruc_calculation.vehicle
LEFT JOIN admin_user on povruc_calculation.creatorOwner = admin_user.id;
编辑 - 将子查询拆分为另一个视图: -
CREATE VIEW admin_calculation_max_date AS SELECT calculation, MAX(date) AS MaxDate
FROM povruc_calculation_status
GROUP BY calculation;
CREATE VIEW admin_calculation_state_view AS SELECT
povruc_calculation.id,
povruc_session.id AS sessionId,
povruc_calculation.createdDate,
CONCAT(povruc_calculation_subject.safeMonths, '/', povruc_calculation_subject.eventCount) AS relevantTime,
povruc_calculation_subject.phone,
povruc_calculation_subject.email,
povruc_calculation_vehicle.manufacturer,
povruc_calculation_vehicle.model,
povruc_calculation_vehicle.engineDisplacement,
povruc_calculation.priceMin,
admin_user.username,
povruc_calculation_status.status AS lastStatus,
povruc_calculation_status.message AS lastStatusMessage,
povruc_calculation_statustype.name AS lastStatusName
FROM povruc_calculation
LEFT OUTER JOIN admin_calculation_max_date Sub1
ON povruc_calculation.id = Sub1.calculation
LEFT OUTER JOIN povruc_calculation_status ON povruc_calculation_status.calculation = Sub1.calculation AND povruc_calculation_status.date = Sub1.MaxDate
LEFT OUTER JOIN povruc_calculation_statustype ON povruc_calculation_status.status = povruc_calculation_statustype.id
LEFT JOIN povruc_calculation_subject on povruc_calculation_subject.id = povruc_calculation.subject
LEFT JOIN povruc_session on povruc_calculation.session = povruc_session.id
LEFT JOIN povruc_calculation_vehicle on povruc_calculation_vehicle.id = povruc_calculation.vehicle
LEFT JOIN admin_user on povruc_calculation.creatorOwner = admin_user.id;
虽然
可能效果不佳