场景:
表 stockpooldata_flash
CREATE TABLE `stockpooldata_flash` (
`id` bigint(15) NOT NULL AUTO_INCREMENT,
`formula_id` int(8) DEFAULT NULL,
`period_type` tinyint(3) DEFAULT NULL,
`gpMarket` int(4) DEFAULT NULL,
`gpcode` varchar(20) DEFAULT NULL,
`ymd` int(11) DEFAULT NULL,
`hms` int(11) DEFAULT NULL,
`f1` decimal(18,4) DEFAULT NULL,
`f2` decimal(18,4) DEFAULT NULL,
`f3` decimal(18,4) DEFAULT NULL,
`f4` decimal(18,4) DEFAULT NULL,
`f5` decimal(18,4) DEFAULT NULL,
`f6` decimal(18,4) DEFAULT NULL,
`f7` decimal(18,4) DEFAULT NULL,
`f8` decimal(18,4) DEFAULT NULL,
`f9` decimal(18,4) DEFAULT NULL,
`f10` decimal(18,4) DEFAULT NULL,
`f11` decimal(18,4) DEFAULT NULL,
`f12` decimal(18,4) DEFAULT NULL,
`f13` decimal(18,4) DEFAULT NULL,
`f14` decimal(18,4) DEFAULT NULL,
`f15` decimal(18,4) DEFAULT NULL,
`f16` decimal(18,4) DEFAULT NULL,
`buyPrice` decimal(18,4) DEFAULT NULL,
`rise_max` decimal(18,4) DEFAULT NULL,
`rise_signalDay` decimal(18,4) DEFAULT NULL,
`high_max` decimal(18,4) DEFAULT NULL,
`yClose_signalDay` decimal(18,4) DEFAULT NULL,
`iTime` bigint(15) DEFAULT NULL,
`MaxRise` decimal(18,4) DEFAULT NULL,
`Rise_NextDay` decimal(18,4) DEFAULT NULL,
`MaxRise_NextDay` decimal(18,4) DEFAULT NULL,
`Rise_FiveDays` decimal(18,4) DEFAULT NULL,
`MaxRise_FiveDays` decimal(18,4) DEFAULT NULL,
`Rise_TenDays` decimal(18,4) DEFAULT NULL,
`MaxRise_TenDays` decimal(18,4) DEFAULT NULL,
`dayNum` int(8) DEFAULT '-1',
`tag` varchar(1024) DEFAULT NULL,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `index_formulaId_gpcode_ymd` (`formula_id`,`period_type`,`ymd`,`gpMarket`,`gpcode`,`f10`) USING BTREE,
KEY `index_formulaId_period_ymd` (`formula_id`,`period_type`,`ymd`) USING BTREE,
KEY `index_formulaId_ymd` (`formula_id`,`ymd`) USING BTREE,
KEY `index_uptime` (`update_time`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=88197314 DEFAULT CHARSET=utf8;
建立了一个视图 v_stockpooldata_flash
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_stockpooldata_flash` AS select
case formula_id
when '4' then 'DXJJ'
else `t`.`formula_id`
end
AS `gscode`,concat(convert(if((`t`.`gpMarket` = 0),'SZ','SH') using utf8),`t`.`gpcode`) AS `gpcode`,`t`.`ymd` AS `ymd`,
( floor(`t`.`hms` / 10000) * 60 + floor(`t`.`hms` % 10000 / 100) ) AS `hms`,
`t`.`f1` AS `f1`, `t`.`f2` AS `f2`, `t`.`f3` AS `f3`, `t`.`f4` AS `f4`, `t`.`f5` AS `f5`, `t`.`f6` AS `f6`,
case
when formula_id = 4 then ( floor(`t`.`f7` / 10000) * 60 + floor(`t`.`f7` % 10000 / 100) )
else `t`.`f7` end
AS `f7`,
`t`.`f8` AS `f8`, `t`.`f9` AS `f9`, `t`.`f10` AS `f10`, `t`.`f11` AS `f11`, `t`.`f12` AS `f12`, `t`.`f13` AS `f13`, `t`.`f14` AS `f14`, `t`.`f15` AS `f15`, `t`.`f16` AS `f16`, `t`.`rise_max` AS `rise_max`, `t`.`rise_signalDay` AS `rise_signalDay`, `t`.`high_max` AS `high_max`, `t`.`yClose_signalDay` AS `yClose_signalDay`, `t`.`update_time` AS `update_time`
from `stockpooldata_flash` `t`
“SELECT * FROM v_stockpooldata_flash where gscode = 'DXJJ' and ymd = 20190123” 语句查询速度太慢:耗时 256.445 s
解决方法:利用源表中的索引 KEY `index_formulaId_period_ymd`
在创建视图时,select `t`.`formula_id` AS `formula_id`
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_stockpooldata_flash` AS select `t`.`formula_id` AS `formula_id`,
case formula_id
when '4' then 'DXJJ'
else `t`.`formula_id`
end
AS `gscode`,concat(convert(if((`t`.`gpMarket` = 0),'SZ','SH') using utf8),`t`.`gpcode`) AS `gpcode`,`t`.`ymd` AS `ymd`,
( floor(`t`.`hms` / 10000) * 60 + floor(`t`.`hms` % 10000 / 100) ) AS `hms`,
`t`.`f1` AS `f1`, `t`.`f2` AS `f2`, `t`.`f3` AS `f3`, `t`.`f4` AS `f4`, `t`.`f5` AS `f5`, `t`.`f6` AS `f6`,
case
when formula_id = 4 then ( floor(`t`.`f7` / 10000) * 60 + floor(`t`.`f7` % 10000 / 100) )
else `t`.`f7` end
AS `f7`,
`t`.`f8` AS `f8`, `t`.`f9` AS `f9`, `t`.`f10` AS `f10`, `t`.`f11` AS `f11`, `t`.`f12` AS `f12`, `t`.`f13` AS `f13`, `t`.`f14` AS `f14`, `t`.`f15` AS `f15`, `t`.`f16` AS `f16`, `t`.`rise_max` AS `rise_max`, `t`.`rise_signalDay` AS `rise_signalDay`, `t`.`high_max` AS `high_max`, `t`.`yClose_signalDay` AS `yClose_signalDay`, `t`.`update_time` AS `update_time`
from `stockpooldata_flash` `t`
查询语句改为“SELECT * FROM v_stockpooldata_flash where formula_id = 4 and ymd = 20190123” 查询效果如下:耗时 0.962 s
来源:oschina
链接:https://my.oschina.net/u/4328407/blog/3660932