2011-07-17 117 views
0

目前我们使用MySQL。我们有空间索引表(所以没有InnoDB),并且每x秒执行整个表更新(将对象从一个地方移动到另一个地方)。整个表更新

目前我们做的:

UPDATE LOW_PRIORITY IGNORE `table` 
SET `column1` = CASE `column2` 
       WHEN 1 THEN ... 
       WHEN 2 THEN ... 
       WHEN 3 THEN ... 
       ... 
       ELSE `column1` 
       END; 

,但它得到的行数增加痛苦万分。是否有更好的方法来完成整个表更新?也许我们应该切换到其他数据库(PostGIS,NoSQL)?

对此有何看法?

+0

为什么你要“移动”对象?这是什么?你能举一个真实的例子,而不是你的标注版本,因为某些原因删除了所有多汁的东西? –

+0

我同意...移动似乎是错误的,而是更新某个地方的状态。并显示实际帮助的实际努力。 – Randy

+0

我有iphone游戏Floonr,人们可以在其中启动虚拟气球并在实际地图上进行跟踪。 im模拟风的变化每次更新取决于经纬度,lon – RolandasR

回答

0

一次更新许多行的最好方法是:

  • 创建临时表
  • 与(主键,新的值)有一个大的INSERT
  • 更新表JOIN不是Temptable使用强制它(主键)SET table.oldvalue = temptable.newvalue

更新:

create table test (id integer primary key, p point not null); 
insert into test (id,p) select id, POINT(id%100, floor(id/100)) from serie limit 10000; 
alter table test add spatial index spi (p); 
select id, AsText(p) from test; 

+----+-------------+ 
| id | AsText(p) | 
+----+-------------+ 
| 1 | POINT(1 0) | 
| 2 | POINT(2 0) | 
| 3 | POINT(3 0) | 
... 

| 98 | POINT(98 0) | 
| 99 | POINT(99 0) | 
| 100 | POINT(0 1) | 
| 101 | POINT(1 1) | 
... 

| 9999 | POINT(99 99) | 
| 10000 | POINT(0 100) | 
+-------+--------------+ 


EXPLAIN SELECT id,AsText(p) from test where Contains(GeomFromText('POLYGON((0 0,0 10,10 10,10 0,0 0))'), p); 
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE  | test | range | spi   | spi | 34  | NULL | 112 | Using where | 
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 

create temporary table test2 (id integer primary key, p point not null); 
insert into test2 (id,p) select id, POINT(1+(id%100), 1+floor(id/100)) from serie limit 10000; 
update test, test2 set test.p=test2.p where test.id=test2.id; 

EXPLAIN SELECT id,AsText(p) from test where Contains(GeomFromText('POLYGON((0 0,0 10,10 10,10 0,0 0))'), p); 
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE  | test | range | spi   | spi | 34  | NULL | 102 | Using where | 
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 

这里没问题(MySQL 5.1.41)

+0

看起来很有趣,会尝试 – RolandasR

+0

嗯,它似乎是这样的更新不适用于空间索引。如果我包括索引列,我总是收到错误:'#126 - 表'tablename.MYI'的密钥文件不正确;尝试修复它'和修复没有帮助。如果我删除空间索引,似乎一切工作 – RolandasR

+0

我做了一些测试,它似乎工作... – peufeu