2010-12-10 3 views
0

Ich versuche, einfach die Reihen von 12 Tabellen "Union". Alle Tabellen zusammen haben 115 Zeilen. Allerdings, wenn ich die Abfrage ausführen unten erhalte ich die folgenden, wenn in dem mysql-slow.log, wenn auf ‚log-Abfragen-nicht-mit-Indizes‘ ‚erklären‘ sowie einen Eintrag mit:Mysql-Abfrage verwendet keine Indizes für Union alle (Rows_sent: 115 Rows_examined: 1008)

Rows_sent: 115 Rows_examined: 1008 

Ich bin etwas mystifiziert wie mysql 1008 Zeilen untersucht, wenn alles, was es zu tun hat, einfach ist, alle Zeilen zusammen zu verbinden (gut, 'Union'). Hinweise oder Hinweise wären sehr willkommen.

Hier ist die Abfrage:

(SELECT id, var_lng_1, 0 as tbl_col FROM tbl1) 

UNION ALL 

(SELECT id, var_lng_1, 1 as tbl_col FROM tbl2) 

UNION ALL 

(SELECT id, var_lng_1, 2 as tbl_col FROM tbl3) 

UNION ALL 

(SELECT id, var_lng_1, 3 as tbl_col FROM tbl4) 

UNION ALL 

(SELECT id, var_lng_1, 4 as tbl_col FROM tbl5) 

UNION ALL 

(SELECT id, var_lng_1, 5 as tbl_col FROM tbl6) 

UNION ALL 

(SELECT id, var_lng_1, 6 as tbl_col FROM tbl7) 

UNION ALL 

(SELECT id, var_lng_1, 7 as tbl_col FROM tbl8) 

UNION ALL 

(SELECT id, var_lng_1, 8 as tbl_col FROM tbl9) 

UNION ALL 

(SELECT id, var_lng_1, 9 as tbl_col FROM tbl10) 

UNION ALL 

(SELECT id, var_lng_1, 10 as tbl_col FROM tbl11) 

UNION ALL 

(SELECT id, var_lng_1, 11 as tbl_col FROM tbl12); 

Jede Eingabe sehr geschätzt

PS: (nur für den Fall würde dies einen Unterschied machen) alle IDs sind primäre, tiny_int (3), auto_increment Spalten. Ich habe auch versucht die gleiche Abfrage nur mit der ids (dh ‚wählen id aus ....‘ aber das machte keinen Unterschied :(

Voll erklären Ausgabe:

[id] => 1 
[select_type] => PRIMARY 
[table] => tbl1 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 7 
[Extra] => 

[id] => 2 
[select_type] => UNION 
[table] => tbl2 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 18 
[Extra] => 

[id] => 3 
[select_type] => UNION 
[table] => tbl3 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 8 
[Extra] => 

[id] => 4 
[select_type] => UNION 
[table] => tbl4 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 10 
[Extra] => 

[id] => 5 
[select_type] => UNION 
[table] => tbl5 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 11 
[Extra] => 

[id] => 6 
[select_type] => UNION 
[table] => tbl6 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 14 
[Extra] => 

[id] => 7 
[select_type] => UNION 
[table] => tbl7 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 10 
[Extra] => 

[id] => 8 
[select_type] => UNION 
[table] => tbl8 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 6 
[Extra] => 

[id] => 9 
[select_type] => UNION 
[table] => tbl9 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 3 
[Extra] => 

[id] => 10 
[select_type] => UNION 
[table] => tbl10 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 2 
[Extra] => 

[id] => 11 
[select_type] => UNION 
[table] => tbl11 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 6 
[Extra] => 

[id] => 12 
[select_type] => UNION 
[table] => tbl12 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 20 
[Extra] => 


[id] => 
[select_type] => UNION RESULT 
[table] => <union1,2,3,4,5,6,7,8,9,10,11,12> 
[type] => ALL 
[possible_keys] => 
[key] => 
[key_len] => 
[ref] => 
[rows] => 
[Extra] => 
+0

Es sollte ein Tisch sein, albern. –

Antwort

1

Sie haben keine WHERE Klausel, so dass Sie Alle Zeilen werden zurückgegeben, und die Verwendung von UNION ALL (im Gegensatz zu UNION) verhindert die Überprüfung auf Duplikate in den verschiedenen Sätzen. Ein vollständiger Tabellenscan jeder Tabelle ist hier erforderlich, um die richtigen Ergebnisse zu erzielen. Und Sie haben keine ORDER BY Klausel. Es gibt nichts über diese Abfrage, die möglicherweise von einem Index profitieren könnte.Ein Index würde einfach nicht helfen

+0

Blimey, das war schnell .... Ich denke, ich verstehe nicht, einen Index usw. zu verwenden. Ich bin nur neugierig, warum es 1008 Zeilen untersucht, wenn es nur 115 insgesamt gibt? Sollte es nicht nur 115 prüfen? Auch - obwohl es keine Duplikate in den Tabellen gibt - wenn es welche gibt, möchte ich auch diese, also scheint UNION ALL der Weg zu sein (?) – olly

+0

Zeigen Sie uns die * komplette * Ausgabe von 'EXPLAIN (SELECT id, var_lng_1 ... FROM tbl9) 'und ich könnte diese Frage vielleicht beantworten. Ich denke, es könnte etwas damit zu tun haben, wie "UNION ALL" implementiert wird, aber ich bin mir nicht sicher. – Asaph

+0

Ich habe die vollständige Ausgabe zu der obigen Frage hinzugefügt ... – olly

Verwandte Themen