mysql NOT EXISTS,非常慢 - 如何改进?

时间:2016-05-06 09:27:22

标签: mysql

我有一个小问题,试图在我的数据库中的某个表上运行这个命令。该表有超过934,83​​6行,每天增长约12,000行。

它包含每天拍摄的坦克快照。我想要实现的是看快照中的差异。即查看玩家是否已购买任何新坦克。

每个account_id的实际快照数据只有100到250行。

表wot_snapshots:

userUpdateRequest.AppMetadata = new Dictionary<string, dynamic>
{
 {"toysNumber",new List<int> {2,3,9,77,itemsToAddInMetadata} }
};

表wot_tanks_all:

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

查询:

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

目前大约需要30秒才能运行。我最好在mysql中完成这一切,或者将其中一部分卸载到PHP?

非常感谢任何建议和帮助

谢谢, 杰森

编辑:这是刚从谷歌整理而来的。还在学习!

1 个答案:

答案 0 :(得分:0)

坦克ID上没有索引,所以加入它会非常慢,我已经重写它以消除自我加入

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 )
相关问题