Originariamente inviato da nicola75ss
Hai indicizzato dei campi per la soluzione che hai proposto?
non ho fatto nulla più di usare il db di prova che hai messo.
copia-incolla, control-invio e basta
questa è la situazione corrente
CREATE TABLE `ps_order_history` (
`id_order_history` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_employee` int(10) unsigned NOT NULL,
`id_order` int(10) unsigned NOT NULL,
`id_order_state` int(10) unsigned NOT NULL,
`date_add` datetime NOT NULL,
PRIMARY KEY (`id_order_history`),
KEY `indice` (`id_order`,`date_add`)
) ENGINE=MyISAM AUTO_INCREMENT=2550501 DEFAULT CHARSET=utf8'
---
Al crescere delle righe, ovviamente, la differenza diventa incomparabile.
Con 1.000.000 di righe si vede come la crescita "quasi" lineare della join "prenda il volo"
codice:
select SQL_NO_CACHE t1.* from ps_order_history as t1
inner join (
select id_order_history
from ps_order_history
order by `date_add` desc) as t2
on t1.id_order_history = t2.id_order_history
group by t1.id_order
having id_order_state = 6
codice:
Z:\data\pluto>ptime mysql -uroot -p pluto <f:\join.sql
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>
=== mysql -uroot -p1 pluto ===
id_order_history id_employee id_order id_order_state date_add
2985956 6 5 6 2011-12-31 21:34:00
2936259 4 10 6 2011-12-31 23:56:26
2597744 9 39 6 2011-12-31 23:25:55
3333550 9 42 6 2011-12-31 23:35:40
2710505 10 44 6 2011-12-31 23:45:19
3164700 6 45 6 2011-12-31 23:02:49
3149934 7 50 6 2011-12-31 22:17:55
3364834 9 82 6 2011-12-31 22:28:30
Execution time: 5.760 s
rispetto alla subquery dipendente
(cosa interessante, almeno dà gli stessi risultati)
codice:
SELECT SQL_NO_CACHE *
FROM ps_order_history p1
WHERE (date_add=(SELECT MAX(p2.date_add)
FROM ps_order_history p2
WHERE p1.id_order=p2.id_order))
and (id_order_state=6)
codice:
Z:\data\pluto>ptime mysql -uroot -p pluto <f:\inveloce.sql
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>
=== mysql -uroot -p1 pluto ===
id_order_history id_employee id_order id_order_state date_add
2597744 9 39 6 2011-12-31 23:25:55
2710505 10 44 6 2011-12-31 23:45:19
2936259 4 10 6 2011-12-31 23:56:26
2985956 6 5 6 2011-12-31 21:34:00
3149934 7 50 6 2011-12-31 22:17:55
3164700 6 45 6 2011-12-31 23:02:49
3333550 9 42 6 2011-12-31 23:35:40
3364834 9 82 6 2011-12-31 22:28:30
Execution time: 193.779 s
che mi dici? se sei davvero curioso di fare qualche raffronto, magari ti posto il dump del mio db di prova