我有一个只有超过1000万行的数据库。结构如下:
CREATE TABLE IF NOT EXISTS `map` (
`id` int(11) NOT NULL,
`good` tinyint(1) NOT NULL DEFAULT '0',
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`terrian_type` int(11) NOT NULL,
... 6 more int columns
PRIMARY KEY (`id`),
KEY `x` (`x`),
KEY `y` (`y`),
KEY `terrian_type` (`terrian_type`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
这包含一张世界地图,其中(x,y)被定义为特定方格的坐标。每个方格都有地形类型。
我想做的是用布尔值(“好”)更新每个方块。如果8个周围的正方形都共享相同的terrain_type(所讨论的正方形无关紧要),则将正方形分类为“良好”。
这是我的询问,判断1个特定的方形/行是否良好(x,y)=(5,5):
SELECT
COUNT(in1.terrian_type),
in1.terrian_type
FROM `worldmap` w1
INNER JOIN `worldmap` in1
ON in1.x >= w1.x-1
AND in1.x <= w1.x+1
AND in1.y >= w1.y-1
AND in1.y <= w1.y+1
AND IF(in1.y = w1.y, IF(in1.x = w1.x, 0, 1), 1) = 1
WHERE w1.x = '5'
AND w1.y = '5'
GROUP BY in1.terrian_type
如果返回的行超过1行,则不是“好”。
希望有人可以帮助确定每个广场是否合适(每个广场都是1000万)。使用上面的查询循环每一个将花费太长时间!这只需要每月一次。
此致 马特
答案 0 :(得分:0)
以下看起来很难看,可能效率不高(除非你有x和y的索引),但我认为这应该有效:
SELECT maptext.x, maptest.y, IF(maptext.terrian_type = x1y.terrian_type AND maptext.terrian_type = x1y1.terrian_type AND maptext.terrian_type = xy1.terrian_type AND maptext.terrian_type = x_1y1.terrian_type AND maptext.terrian_type = x_1y.terrian_type AND maptext.terrian_type = x_1y_1.terrian_type AND maptext.terrian_type = xy_1.terrian_type AND maptext.terrian_type = x1y_1.terrian_type, 'GOOD','BAD')
FROM worldmap maptest
INNER JOIN worldmap x1y
on maptest.x = x1y.x + 1 and maptest.y = x1y.y
INNER JOIN worldmap x1y1
on maptest.x = x1y1.x + 1 and maptest.y = x1y1.y + 1
INNER JOIN worldmap xy1
on maptest.x = xy1.x and maptest.y = xy1.y + 1
INNER JOIN worldmap x_1y1
on maptest.x = x_1y1.x - 1 and maptest.y = x_1y1.y + 1
INNER JOIN worldmap x_1y
on maptest.x = x_1y.x - 1 and maptest.y = x_1y.y
INNER JOIN worldmap x_1y_1
on maptest.x = x_1y_1.x - 1 and maptest.y = x_1y_1.y - 1
INNER JOIN worldmap xy_1
on maptest.x = xy_1.x and maptest.y = xy_1.y - 1
INNER JOIN worldmap x1y_1
on maptest.x = x1y_1.x + 1 and maptest.y = x1y_1.y - 1