Hallo,
ja, die Mehrfachberechnungen stören mich auch - allerdings sah ich bislang keine Möglichkeit, auf ein einmal berechnetes Ergebnis z.B. per Variable im Query zuzugreifen.
Beim Datenmodell ist sicher nicht alles optimal, was aber die Verknüpfungen der Tabellen untereinander angeht - da bin ich doch recht zuversichtlich, dass es so ok ist.
Es wäre vermutlich viel gewonnen, wenn man die Mehrfachberechnungen nur jeweils einmal machen müsste und dann auf diesen Wert zugreifen könnte.
Ich strukturiere gerade nebenbei noch den kompletten Query um, so dass die invoicelines nicht per LEFT JOIN kommen, sondern in einzelnen SubSelects - das löst auch das GROUP BY Problem.
Hier mal der EXPLAIN für den gesamten String:
+----+--------------------+--------------+------+---------------+-----------+---------+--------------------------------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+--------------+------+---------------+-----------+---------+--------------------------------+--------+---------------------------------+
| 1 | PRIMARY | items | ALL | NULL | NULL | NULL | NULL | 72176 | Using temporary; Using filesort |
| 1 | PRIMARY | <derived15> | ALL | NULL | NULL | NULL | NULL | 39683 | |
| 15 | DERIVED | invoicelines | ALL | NULL | NULL | NULL | NULL | 551160 | Using temporary |
| 14 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 13 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 12 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 11 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 10 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 9 | DEPENDENT SUBQUERY | so | ALL | ItemNrInt | NULL | NULL | NULL | 1333 | Using where |
| 8 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 7 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 6 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 5 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 4 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 3 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
| 2 | DEPENDENT SUBQUERY | items_stock | ref | ItemNrInt | ItemNrInt | 4 | reporting_test.items.ItemNrInt | 8 | Using where |
+----+--------------------+--------------+------+---------------+-----------+---------+--------------------------------+--------+---------------------------------+
Grüße
Mark