Hi,
ich habe in einem Programm hier einen riesen MySQL Query der in der Ausführung 90sek dauert.. auf der Suche nach der Ursache habe ich das ganze auf ein paar Zeilen zusammen gekürzt die das Problem sehr verdeutlichen:
SQL-Anweisung
1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
| SELECT DISTINCT
f.`version`, tvs.`id_task_status`
FROM `file` f LEFT JOIN `file_task_connection` ftc USING(`id_file`) LEFT JOIN `task_version_status` tvs USING(`id_task`, `version`)
WHERE f.`id_project` = 56 AND f.`id_file_master` IS NOT NULL |
Diese Abfrage dauert 17sek (file und file_task_connection haben jeweils etwa 700.000 Einträge, task_version_status hat 50.000).
Auch ist auf allen Feldern die in dem Query vorkommen ein Index (BTREE).
Der Query liefert etwa 600 Ergebnisse, wenn ich das
DISTINCT wegnehme bekomme ich 200.000 Stück, allerdings dauert der Query dann auch nur 0.2 Sekunden statt 17..
Wenn ich das ganze mit
EXPLAIN mir anschaue bekomme ich:
SQL-Anweisung
1: 2: 3: 4: 5: 6: 7:
| +----+-------------+-------+------+---------------------------------------+----------------------------+---------+------------------------+--------+------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------------------------------+----------------------------+---------+------------------------+--------+------------------------------+ | 1 | SIMPLE | f | ref | fk_file_project,fk_file_file_master | fk_file_project | 4 | const | 263232 | Using where; Using temporary | | 1 | SIMPLE | ftc | ref | PRIMARY,fk_file_has_task_file1 | PRIMARY | 4 | vfxdb_v001.f.id_file | 1 | Using index | | 1 | SIMPLE | tvs | ref | fk_taskVersionStatus_task1,SpeedIndex | fk_taskVersionStatus_task1 | 5 | vfxdb_v001.ftc.id_task | 11 | | +----+-------------+-------+------+---------------------------------------+----------------------------+---------+------------------------+--------+------------------------------+ |
Ohne
DISTINCT und mit
EXPLAIN:
SQL-Anweisung
1: 2: 3: 4: 5: 6: 7:
| +----+-------------+-------+------+---------------------------------------+----------------------------+---------+------------------------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------------------------------+----------------------------+---------+------------------------+--------+-------------+ | 1 | SIMPLE | f | ref | fk_file_project,fk_file_file_master | fk_file_project | 4 | const | 263232 | Using where | | 1 | SIMPLE | ftc | ref | PRIMARY,fk_file_has_task_file1 | PRIMARY | 4 | vfxdb_v001.f.id_file | 1 | Using index | | 1 | SIMPLE | tvs | ref | fk_taskVersionStatus_task1,SpeedIndex | fk_taskVersionStatus_task1 | 5 | vfxdb_v001.ftc.id_task | 11 | | +----+-------------+-------+------+---------------------------------------+----------------------------+---------+------------------------+--------+-------------+ |
Gibt es da jetzt irgendeine Möglichkeit das ganze schneller zu bekommen?
Danke~
Aya
PS: Es sind alles InnoDBs