【发布时间】:2014-01-21 08:22:57
【问题描述】:
使用 Play Framwork 1.x - 我已经定义了这个模型:
@Entity
public class ShareHistoryResult extends GenericModel {
@Id
@Index(name = "isinOnly")
public String ISIN;
@Id
@Index(name = "calDate")
public Date calDate;
public Double opening;
public Double closing;
public Double highest;
public Double lowest;
public Integer count;
public Double turnover;
要获得某个日期之前的最新结果,我选择:
ShareHistoryResult shareDay = ShareHistoryResult.find("calDate <= ? AND ISIN = ? AND closing > 0 ORDER BY calDate DESC", date, isin).first();
工作正常。
对于大多数 isin 键,请求所需的时间少于 50 毫秒。但是对于几个键(每次都相同),它需要长达 300-400 毫秒。
JPA 生成的 SQL:
select sharehisto0_.calDate as calDate9_, sharehisto0_.ISIN as ISIN9_, sharehisto0_.closing as closing9_, sharehisto0_.count as count9_, sharehisto0_.highest as highest9_, sharehisto0_.lowest as lowest9_, sharehisto0_.opening as opening9_, sharehisto0_.turnover as turnover9_ from ShareHistoryResult sharehisto0_ where sharehisto0_.calDate<=? and sharehisto0_.ISIN=? and sharehisto0_.closing>0 order by sharehisto0_.calDate DESC limit ?
如果我直接在 MySqlWorkbench 中使用慢速键之一运行此 SQL。它以 0.02 - 0.2 秒的闪电速度进行评估。
这是表的创建语句(复制自MySqlWorkbench):
CREATE TABLE `ShareHistoryResult` (
`calDate` datetime NOT NULL,
`ISIN` varchar(255) NOT NULL,
`closing` double DEFAULT NULL,
`count` int(11) DEFAULT NULL,
`highest` double DEFAULT NULL,
`lowest` double DEFAULT NULL,
`opening` double DEFAULT NULL,
`turnover` double DEFAULT NULL,
PRIMARY KEY (`calDate`,`ISIN`),
KEY `calDate` (`calDate`),
KEY `isinOnly` (`ISIN`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET
知道为什么有些键比其他键慢得多吗?
【问题讨论】:
-
你对内存数据库中的 hsql 有同样的行为吗?
-
不知道。这是一个相当大的表,所以我从未在内存数据库中测试过。
-
如果您在没有 first() 的情况下运行查询,结果大小是多少?如果您在不排序的情况下运行查询,也会有区别吗?
标签: java sql jpa playframework