2011-07-17 35 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