2016-05-06 46 views
0

我有一個關於試圖ompimise我在我的數據庫中的表上運行這個命令的小問題。這個桌子有934,83​​6排,每天增長大約12,000。mysql不存在,很慢 - 如何改進?

它擁有每天服用坦克的快照。我想要實現的是看到快照中的差異。即查看玩家是否購買了任何新的坦克。

每ACCOUNT_ID實際的快照數據僅100〜250行。

表wot_snapshots:

CREATE TABLE `wot_snapshots` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `snapshot` varchar(30) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=95 DEFAULT CHARSET=latin1 

表wot_tanks_all:

CREATE TABLE `wot_tanks_all` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `snapshot` int(11) NOT NULL, 
    `account_id` int(11) NOT NULL, 
    `tank_id` int(6) NOT NULL, 
    `wn8` float DEFAULT NULL, 
    `spotted` int(11) NOT NULL, 
    `avg_damage_blocked` decimal(6,2) NOT NULL, 
    `capture_points` int(11) NOT NULL, 
    `explosion_hits` int(11) NOT NULL, 
    `piercings` int(11) NOT NULL, 
    `xp` int(11) NOT NULL, 
    `survived_battles` int(11) NOT NULL, 
    `dropped_capture_points` int(11) NOT NULL, 
    `damage_dealt` int(11) NOT NULL, 
    `hits_percents` int(11) NOT NULL, 
    `draws` int(11) NOT NULL, 
    `battles` int(11) NOT NULL, 
    `damage_received` int(11) NOT NULL, 
    `frags` int(11) NOT NULL, 
    `direct_hits_received` int(11) NOT NULL, 
    `hits` int(11) NOT NULL, 
    `battle_avg_xp` int(11) NOT NULL, 
    `wins` int(11) NOT NULL, 
    `losses` int(11) NOT NULL, 
    `piercings_received` int(11) NOT NULL, 
    `no_damage_direct_hits_received` int(11) NOT NULL, 
    `shots` int(11) NOT NULL, 
    `explosion_hits_received` int(11) NOT NULL, 
    `tanking_factor` decimal(2,2) NOT NULL, 
    `mark_of_mastery` int(1) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM AUTO_INCREMENT=934837 DEFAULT CHARSET=utf8 

查詢:

SELECT t1.tank_id, wot_tanks.short_name_i18n FROM wot_tanks_all t1 
JOIN wot_tanks ON t1.tank_id = wot_tanks.tank_id 
WHERE NOT EXISTS (SELECT tank_id 
       FROM wot_tanks_all t2 
       WHERE t1.tank_id = t2.tank_id AND account_id = 527080765 AND snapshot = 60) 
AND account_id = 527080765 AND snapshot = 93 

輸出:

tank_id short_name_i18n 
8465 Panther II 
53505 T-127 
54865 Light VIC 

它目前需要大約30秒才能運行。我最好在mysql中完成這些操作,或者將其中的一部分卸載到PHP中?

任何建議和幫助是極大的讚賞

感謝, 傑森

編輯:這款剛剛從谷歌放在一起。仍在學習!

+0

表定義將有助於 – Mihai

回答

0

上有槽ID,以便加入它會很慢沒有索引,I`ve改寫它來消除自加入

SELECT t1.tank_id, wot_tanks.short_name_i18n FROM wot_tanks_all t1 
JOIN wot_tanks 
ON t1.tank_id = wot_tanks.tank_id 
GROUP BY t1.tank_id,wot_tanks.short_name_i18n 
HAVING SUM(account_id = 527080765 AND snapshot = 60)=0 
AND SUM(account_id = 527080765 AND snapshot = 93)>0 

這些指標將加快東西

ALTER TABLE wot_tanks_all ADD KEY(account_id ,snapshot) 
ALTER TABLE wot_tanks_all ADD KEY(tank_id) 
+0

我還沒有運行查詢。我先索引這些字段。我猜這將需要一段時間來索引近100萬行? – jmutton