简体   繁体   English

优化这个mysql查询

[英]Optimize this mysql query

Can some one optimize this mysql query 有人可以优化这个mysql查询

SELECT submittedform.*, inspectors.first_name, inspectors.last_name
  FROM (
       SELECT `dinsp`,`departure`,`arrival`,'cabin' as type FROM cabinets 
       UNION
       SELECT `dinsp`,`departure`,`arrival`,'cockpit' as type FROM cockpits
       ORDER BY `date_of_inspection` ASC
       ) AS submittedform 
       INNER JOIN inspectors ON inspectors.id = submittedform.dinsp

I don't want to rely on nested query or is it fine in this case? 我不想依赖嵌套查询,或者在这种情况下是否正常? Also suggest me a cakephp solution but the tables can't be related. 还建议我一个cakephp解决方案,但表格无法相关。

You can try: 你可以试试:

SELECT sf.`dinsp`, sf.`departure`, sf.`arrival`, sf.`type`, i.`first_name`, i.`last_name`
FROM
    `inspectors` AS i INNER JOIN (
    SELECT `dinsp`, `departure`, `arrival`, `date_of_inspection`, 'cabin' AS `type`
    FROM `cabinets`
    UNION ALL
    SELECT `dinsp`, `departure`, `arrival`, `date_of_inspection`, 'cockpit' AS `type`
    FROM `cockpits`
) AS sf ON sf.`dinsp` = i.`id`
ORDER BY sf.`date_of_inspection`

UNION ALL will not check for duplicates. UNION ALL不会检查重复项。 Always put the ORDER BY clause in the outer query to ensure proper ordering. 始终将ORDER BY子句放在外部查询中以确保正确排序。

It would be better to avoid using UNION because it will not allow the query optimizer to use any index you may have on dinsp and date_of_inspection . 最好避免使用UNION因为它不允许查询优化器使用你在dinspdate_of_inspection上可能有的任何索引。 But that would mean changing the schema. 但这意味着要改变架构。

An alternative to a UNION sub-query is to make the main query into two parts with a UNION between: UNION子查询的替代方法是将主查询分为两部分,其中UNION位于:

SELECT c.dinsp, c.departure, d.arrival, 'cabin'   AS type, i.first_name, i.last_name
  FROM cabinets AS c JOIN inspectors AS i ON i.id = c.dinsp
SELECT c.dinsp, c.departure, d.arrival, 'cockpit' AS type, i.first_name, i.last_name
  FROM cockpits AS c JOIN inspectors AS i ON i.id = c.dinsp

It is not clear that this would give significantly different performance. 目前尚不清楚这会产生明显不同的表现。 If anything, it would be worse since it involves two scans of the Inspectors table, but that isn't likely to be very big so it may not matter very much. 如果有的话,它会更糟,因为它涉及Inspectors表的两次扫描,但这可能不是很大,所以它可能并不重要。 Your UNION sub-query minus the ORDER BY is likely to be as good as or slightly better than this. 您的UNION子查询减去ORDER BY可能与此一样好或稍好一些。 Your ORDER BY on a non-selected field is problematic in the inner query; 您在非选定字段上的ORDER BY在内部查询中存在问题; and needs careful handling in the UNION I'm proposing (probably by selecting the extra column). 并且需要在我提议的UNION中小心处理(可能通过选择额外的列)。

SELECT c.dinsp, c.date_of_inspection, c.departure, d.arrival, 'cabin'   AS type,
       i.first_name, i.last_name
  FROM cabinets AS c JOIN inspectors AS i ON i.id = c.dinsp
SELECT c.dinsp, c.date_of_inspection, c.departure, d.arrival, 'cockpit' AS type,
       i.first_name, i.last_name
  FROM cockpits AS c JOIN inspectors AS i ON i.id = c.dinsp
 ORDER BY date_of_inspection;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM