Ho due tabelle: una con punti, l'altra con polys.Join su indici mysql spaziali
CREATE TABLE `points` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`point` point NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
CREATE TABLE `ranges` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`poly` polygon NOT NULL,
PRIMARY KEY (`id`),
SPATIAL KEY `poly` (`poly`)
) ENGINE=MyISAM;
Desidero unire intervalli a punti su punti all'interno di poligoni. Le query sembrano semplici:
SELECT *
FROM points
LEFT JOIN ranges
ON MBRCONTAINS(poly, point)
WHERE points.id = 2;
Questa query funziona veloce e utilizza gli indici, parte di spiegare:
table | type | possible_keys | key | key_len ranges | range | poly | poly | 34
Ma, quando provo ad unire con più righe dalla tabella points
:
SELECT *
FROM points
LEFT JOIN ranges
ON MBRCONTAINS(poly, point)
WHERE points.id IN (1,2,3);
tutto si rompe:
+----+-------------+------------+-------+---------------+---------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+-------+---------------+---------+---------+------+--------+-------------+ | 1 | SIMPLE | points | range | PRIMARY | PRIMARY | 4 | NULL | 3 | Using where | | 1 | SIMPLE | ranges | ALL | poly | NULL | NULL | NULL | 155183 | | +----+-------------+------------+-------+---------------+---------+---------+------+--------+-------------+
L'aggiunta di FORCE INDEX (poly)
non aiuta.
dati campione per testare le query (mi dispiace, solo la versione PHP, io non sono comuni con le procedure SQL):
//points
for($i=0;$i<=500;$i++) {
$point = mt_rand();
mysql_query('INSERT INTO points (point) VALUES (POINTFROMWKB(POINT('.$point.', 0)))');
}
$qty = 20000;
$max = mt_getrandmax();
$add = $max/$qty
$end = 0;
//polys
while($end < $max) {
$start = $end;
$end = mt_rand($start, $start + $add);
mysql_query('INSERT INTO ranges (poly) VALUES (
GEOMFROMWKB(POLYGON(LINESTRING(
POINT('.$start.', -1),
POINT('.$end.', -1),
POINT('.$end.', 1),
POINT('.$start.', 1),
POINT('.$start.', -1)
)))
)');
}
http://dba.stackexchange.com/? –
Puoi spiegare cosa intendi con "tutto si rompe"? Il messaggio di errore è difficile da leggere ?! – Bytemain
Sarebbe gentile pubblicare un inserto di esempio per quelle tabelle, e hai provato a riscrivere quella condizione IN per usare Joins? – Pentium10