加快mysql更新查询

时间:2012-02-12 14:26:19

标签: mysql performance

我正在运行我认为相当直接的计算相当大(65,000行)数据集的财务回报。我在较小的数据集上运行查询并且工作正常,但是在大数据集上我在大约20小时后终止它。

查询计算3,6,12,18&在大约20年的月度数据中,宇宙中500种股票中的每一种都有24个月的回报率。

UPDATE 
  master_backup AS curr
  LEFT OUTER JOIN
      master AS mo3
      ON 
        ((curr.new_date = LAST_DAY(mo3.new_date - INTERVAL 3 MONTH)) AND (curr.sedol = mo3.sedol))
  LEFT OUTER JOIN 
      master AS mo6
      ON 
        ((curr.new_date = LAST_DAY(mo6.new_date - INTERVAL 6 MONTH)) AND (curr.sedol = mo6.sedol))
  LEFT OUTER JOIN 
      master AS mo12
      ON 
        ((curr.new_date = LAST_DAY(mo12.new_date - INTERVAL 12 MONTH)) AND (curr.sedol = mo12.sedol))
  LEFT OUTER JOIN 
      master AS mo18
      ON 
        ((curr.new_date = LAST_DAY(mo18.new_date - INTERVAL 18 MONTH)) AND (curr.sedol = mo18.sedol))
  LEFT OUTER JOIN 
      master AS mo24  
      ON 
        ((curr.new_date = LAST_DAY(mo24.new_date - INTERVAL 24 MONTH)) AND (curr.sedol = mo24.sedol))
SET 
    curr.ret_3mth = (mo3.price - curr.price)/curr.price,
    curr.ret_6mth = (mo6.price - curr.price)/curr.price,
    curr.ret_12mth = (mo12.price - curr.price)/curr.price,
    curr.ret_18mth = (mo18.price - curr.price)/curr.price,
    curr.ret_24mth = (mo24.price - curr.price)/curr.price;

表'master'

'master', 'CREATE TABLE `master` (
  `mdate` date NOT NULL,
  `new_date` date DEFAULT NULL,
  `id_connah` varchar(45) DEFAULT NULL,
  `ticker` varchar(45) NOT NULL,
  `pnum` varchar(45) NOT NULL,
  `sedol` varchar(45) NOT NULL,
  `cusip` varchar(45) NOT NULL,
  `price` double DEFAULT NULL,
  `mcap` double DEFAULT NULL,
  `ret` double DEFAULT NULL,
  `ebit` double DEFAULT NULL,
  `debt_st` double DEFAULT NULL,
  `debt_lt` double DEFAULT NULL,
  `cash` double DEFAULT NULL,
  `assets_total` double DEFAULT NULL,
  `assets_intang` double DEFAULT NULL,
  `assets_curr` double DEFAULT NULL,
  `ev` double DEFAULT NULL,
  `ey` double DEFAULT NULL,
  `roce` double DEFAULT NULL,
  `ret_3mth` double DEFAULT NULL,
  `ret_6mth` double DEFAULT NULL,
  `ret_12mth` double DEFAULT NULL,
  `ret_18mth` double DEFAULT NULL,
  `ret_24mth` double DEFAULT NULL,
  `rank_ey` int(11) DEFAULT NULL,
  `rank_roce` int(11) DEFAULT NULL,
  `rank_combined` int(11) DEFAULT NULL,
  `rank_final` int(11) DEFAULT NULL,
  `fractile` int(11) DEFAULT NULL,
  KEY `sedol` (`sedol`),
  KEY `cusip` (`cusip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8'

我绝不是MySQL专家,并使用默认的cnf文件设置设置了数据库。任何建议将不胜感激。我认为内存可能是一个问题,但是这个过程似乎比内存密集型CPU密集。

由于

1 个答案:

答案 0 :(得分:1)

您是否尝试在new_date字段上创建索引?

此外,我认为您还可以在24个月的间隔内仅使用1 LEFT JOIN,然后根据您当前加入这些表的条件使用if()函数更新字段。