[linux] sql vraag: waarom gaat n1 wel naar disk en n2 niet?

Folkert van Heusden folkert op vanheusden.com
Vr Jul 4 14:29:16 CEST 2008


Hi,

Ik heb een SQL vraag. Zie deze query:
SELECT
	n1.nick AS nick1,
	n2.nick AS nick2,
	SUM(n1.count) AS sum1, SUM(n2.count) AS sum2, SUM(n1.count * n1.count) AS sum1sq, SUM(n2.count * n2.count) AS sum2sq, SUM(n1.count * n2.count) AS psum,
	COUNT(*) AS n
FROM
	linux_nl3 AS n1,
	linux_nl3 AS n2
WHERE
	n1.word1=n2.word1 AND
	n1.word2=n2.word2 AND
	n1.word3=n2.word3
GROUP BY
	n1.nick,
	n2.nick
ORDER BY
	n1.nick,
	n2.nick

Om te achterhalen waarom e.e.a. zo traag is in mysql heb ik 'm 'ns door
"explain" gehaald en zag daarbij iets vreemds:
+----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | SIMPLE      | n1    | ALL  | NULL          | NULL | NULL    | NULL | 6629 | Using temporary; Using filesort | 
|  1 | SIMPLE      | n2    | ALL  | NULL          | NULL | NULL    | NULL | 6629 | Using where                     | 
+----+-------------+-------+------+---------------+------+---------+------+------+---------------------------------+

Zoals je hier ziet wordt 'n1' anders behandeld als 'n2' hoewel het
dezelfde tabellen zijn (zie 'FROM') waarop dezelfde operaties gedaan
worden (zie 'SELECT', 'WHERE' en 'GROUP/ORDER BY').
Kan iemand dit verklaren en/of een tip geven hoe die filesort te
voorkomen?


Folkert van Heusden

-- 
MultiTail is een flexibele tool voor het volgen van logfiles en
uitvoer van commando's. Filteren, van kleur voorzien, mergen,
'diff-view', etc. http://www.vanheusden.com/multitail/
----------------------------------------------------------------------
Phone: +31-6-41278122, PGP-key: 1F28D8AE, www.vanheusden.com



More information about the Linux mailing list