ORDER BY
派生テーブルのサブクエリは、MySQL5.7では無視されます。
https://dev.mysql.com/を参照してください。 doc / refman / 5.7 / en / defined-table-optimization.html
外部クエリにはJOINとGROUPBYがあるため、ORDER BYを伝播する資格がないため、ORDERBYを無視します。
このオプティマイザーの動作は、オプティマイザースイッチderived_merge
によって制御されます。 。無効にすることができます。
デモ:
mysql [localhost] {msandbox} (test) > select @@version;
+-----------+
| @@version |
+-----------+
| 5.7.21 |
+-----------+
mysql [localhost] {msandbox} (test) > SELECT columnPrimaryKey, column1, column2, column3 FROM (SELECT columnPrimaryKey, column1, column2, column3 FROM testTable ORDER BY column2 ) AS tbl GROUP BY column3;
+------------------+----------------+---------+---------+
| columnPrimaryKey | column1 | column2 | column3 |
+------------------+----------------+---------+---------+
| 1 | Some Name 8-4 | 4 | 8 |
| 6 | Some Name 9-1 | 1 | 9 |
| 8 | Some Name 10-2 | 2 | 10 |
+------------------+----------------+---------+---------+
mysql [localhost] {msandbox} (test) > set optimizer_switch = 'derived_merge=off';
Query OK, 0 rows affected (0.00 sec)
mysql [localhost] {msandbox} (test) > SELECT columnPrimaryKey, column1, column2, column3 FROM (SELECT columnPrimaryKey, column1, column2, column3 FROM testTable ORDER BY column2 ) AS tbl GROUP BY column3;
+------------------+----------------+---------+---------+
| columnPrimaryKey | column1 | column2 | column3 |
+------------------+----------------+---------+---------+
| 5 | Some Name 8-1 | 1 | 8 |
| 6 | Some Name 9-1 | 1 | 9 |
| 8 | Some Name 10-2 | 2 | 10 |
+------------------+----------------+---------+---------+