I have the following table in MySQL:
DATE EDGE VALUE
D E1 X1
D E2 Y1
D E3 Z1
D1 E1 X2
D1 E2 Y2
D1 E3 Z2
D2 E1 X3
D2 E2 Y3
D2 E3 Z3
Now I would like to calculate Euclidean distance of D to D1 and D to D2 like
Distance(D-D1)= Sqrt((X1-X2)^2 +(Y1-Y2)^2 +(Z1-Z2)^2 );
Distance(D-D2)= Sqrt((X1-X3)^2 +(Y1-Y3)^2 +(Z1-Z3)^2 );
....... and so on..
From this distance I want to choose 'k' nearest neighbors of D.
(Please Note that It might be possible that record D might have any no of entries for edges(E1,E2...En). In this case other D1,D2,D3 will have same no of entries for edges...
Please suggest me solution as a stored procedure in MySQL...
Thanks in advance
@eggyal:
I tried to work on building similar query as answered by you.
Query:
SELECT b.id,SQRT(SUM(POW(a.score - b.score, 2))) score1
FROM (select * from data d1 where d1.id = (select max(t1.id) from Timestamp t1)
and d1.edge_id in (select m1.src_edge from mapping m1
where m1.dst = (select m2.src from mapping m2 where m2.src_edge=2 limit 1))) a
JOIN (select * from data d2 where d2.id in ( select t2.id from Timestamp t2 where DAYOFWEEK(NOW())=DAYOFWEEK(t2.timestamp)) and d2.edge_id in (select m3.src_edge from mapping m3
where m3.dst = (select m4.src from mapping m4
where m4.src_edge=2 limit 1))) as b
ON b.id <> a.id AND b.edge_id = a.edge_id
GROUP BY b.id
ORDER BY score1
LIMIT 5;
However this is not good designed query. Please suggest all the improvements needed for the above query..
Thanks in advance
Table used for the above query:
DATA table:
CREATE TABLE `data` (
`id` bigint(20) DEFAULT NULL,
`edge_id` bigint(20) NOT NULL,
`score` int(11) NOT NULL,
KEY `edge_id` (`edge_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE (edge_id)
(PARTITION p0 VALUES LESS THAN (10000) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (20000) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (30000) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (40000) ENGINE = InnoDB,
PARTITION p4 VALUES LESS THAN (50000) ENGINE = InnoDB,
PARTITION p5 VALUES LESS THAN (60000) ENGINE = InnoDB,
PARTITION p6 VALUES LESS THAN (70000) ENGINE = InnoDB,
PARTITION p7 VALUES LESS THAN (80000) ENGINE = InnoDB) */$$
Mapping table;
CREATE TABLE `mapping` (
`dst` bigint(20) NOT NULL DEFAULT '0',
`src` bigint(20) DEFAULT NULL,
`src_edge` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`dst`,`src_edge`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
Timestamp Table:
CREATE TABLE `Timestamp` (
`timestamp` datetime NOT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
KEY `time` (`timestamp`,`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1$$