we’re not likely to be able to offer much in the way of analysis without seeing the table definitions and the query too
also, here’s your EXPLAIN laid out in a more readable fashion than a CSV
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE dl ref device_id device_id 22 const 231769 Using index condition; Using temporary; Using filesort
1 SIMPLE rfinfo ALL device_id \N \N \N 4 Using where; Using join buffer (Block Nested Loop)
1 SIMPLE ud ALL user_id \N \N \N 14 Using where; Using join buffer (Block Nested Loop)
CREATE TABLE `user_devices` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`device_id` varchar(20) NOT NULL,
`dateTime_added` datetime NOT NULL,
`device_type` int(11) NOT NULL,
`device_name` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=52 DEFAULT CHARSET=latin1
CREATE TABLE `rfidinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`device_id` varchar(20) NOT NULL DEFAULT '0',
`rfid` varchar(15) NOT NULL DEFAULT '0',
`datepunch` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `device_id` (`device_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
CREATE TABLE `device_locations` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`device_id` varchar(20) NOT NULL,
`latitude` varchar(15) NOT NULL,
`longitude` varchar(15) NOT NULL,
`dateTime_created` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `device_id` (`device_id`)
) ENGINE=MyISAM AUTO_INCREMENT=750632 DEFAULT CHARSET=latin1
This is how I query.,
First I get all the devices_id which is belong to the $loginid.
Select device_id from user_devices where user_id = '$loginid'
Then I create a while loop in-order to get each record base on device_id from user_devices
select dl.col1,
dl.col2,
dl.col3,
dl.col4,
dl.col5,
rfinfo.rfcol1,
rfinfo.rfcol2,
ud.col1
from user_devices ud
inner join device_locations dl
on ud.device_id = dl.device_id
left outer join rfidinfo rfinfo
on dl.device_id =rfinfo.device_id
where dl.device_id = '$from_user_deviceid' AND ud.user_id = '$loginid'
order by dl.dateTime_created desc LIMIT 0,1
SELECT dl.col1
, dl.col2
, dl.col3
, dl.col4
, dl.col5
, dl.dateTime_created AS latest
, rfinfo.rfcol1
, rfinfo.rfcol2
, ud.col1
FROM user_devices AS ud
INNER
JOIN ( SELECT device_id
, MAX(dateTime_created) AS latest
FROM device_locations
GROUP
BY device_id ) AS dx
ON dx.device_id = ud.device_id
INNER
JOIN device_locations AS dl
ON dl.device_id = dx.device_id
AND dl.dateTime_created = dx.latest
LEFT OUTER
JOIN rfidinfo AS rfinfo
ON rfinfo.device_id = ud.device_id
WHERE ud.user_id = '$loginid'
it’s normal if you don’t have the appropriate indexes on your tables
you have only the index on user_id in the first table
add these and try your query again –
ALTER TABLE user_devices
ADD INDEX (device_id)
;
ALTER TABLE rfidinfo
ADD INDEX (device_id)
;
ALTER TABLE device_locations
ADD INDEX (device_id, dateTime_created)
;
okay, here we go, there will be four cases of data to look at
this is an exercise to show you the power of indexing, and why un-indexed tables are so slow to read
here’s what i want you to do – using only inspection, i.e. just by looking at the data, please tell me the MAX(dateTime_created) for each of the three device_id values 111, 777, 1234
for the first example, the data has no indexes at all –
@r937,
maximum date,here it takes time because the device_id are not group,and I need to to go top and down down to top just to look the device_id with max date.
111 2014-11-23
777 2014-07-16
1234 2014-11-19
minimum date,here it’s easy for me to see find the device_id.because the device_id are in there group.but the date is not arranged.
[quote=“jemz, post:15, topic:104530, full:true”]
yes it easy to find the maxdate because the date are arranged…but the device_id are not.
[/quote]but you had to read the entire table to find them, which was the result of the index on dateTime_created only – and you had the benefit of “looking ahead” or in this case backwards from the end
last example, index on ( device_id , dateTime_created )