Java(openjdk版本“1.8.0”,linux)内存中有数百万条记录。经常添加和请求已排序的数据
每个记录都有id、价格和数量。若id已经存在,则添加新记录时,新记录将替换旧记录,id在数据集中是唯一的。通常我需要得到排序数据(按价格)。如数据集中的前100名或25-50名。好的是,每次我需要排序的数据都是从数据集开始的。(我在可能的情况下使用了Comparable compareTo(对象o) 我已尝试将数据存储到:Java(openjdk版本“1.8.0”,linux)内存中有数百万条记录。经常添加和请求已排序的数据,java,sorting,types,Java,Sorting,Types,每个记录都有id、价格和数量。若id已经存在,则添加新记录时,新记录将替换旧记录,id在数据集中是唯一的。通常我需要得到排序数据(按价格)。如数据集中的前100名或25-50名。好的是,每次我需要排序的数据都是从数据集开始的。(我在可能的情况下使用了Comparable compareTo(对象o) 我已尝试将数据存储到: ArrayList在id相同时替换元素或在末尾添加元素。在需要数据时对其进行排序。太慢了 ArrayQue删除旧元素(如果存在)并添加新元素。在需要数据时对其进行排序。太慢
id
进行查找的HashMap
,以及一个用于排序访问的TreeSet
您没有指定使用的排序方式,因此下面我假设您是按price
排序的
TreeSet
要求元素是唯一的,因此要按price
排序,还需要按id
进行二次排序,以使排序键唯一。额外的副作用:使用相同的价格对记录进行一致的排序
首先,我们完全定义您的记录
类:
class Record implements Comparable<Record> {
private int id;
private double price;
private int quantity;
public Record(int id, double price, int quantity) {
this.id = id;
this.price = price;
this.quantity = quantity;
}
public Record(double price, int id) {
this.id = id;
this.price = price;
}
public int getId() {
return this.id;
}
public double getPrice() {
return this.price;
}
public int getQuantity() {
return this.quantity;
}
@Override
public int compareTo(Record that) {
int cmp = Double.compare(this.price, that.price);
if (cmp == 0)
cmp = Integer.compare(this.id, that.id);
return cmp;
}
@Override
public boolean equals(Object obj) {
if (! (obj instanceof Record))
return false;
Record that = (Record) obj;
return (this.id == that.id);
}
@Override
public int hashCode() {
return this.id;
}
}
您是否考虑过为您的数据使用单独的数据库层?数据库的结构针对您描述的查询类型进行了优化。在内部,数据库通常使用B-树数据结构对数据进行排序,无论它们是否在内存数据库中。您能否量化记录的实际数量以及您认为“足够快”的内容?目前,使用任何类型的数据库都不是一种选择。例如,可能有大约1亿条记录被更新(添加/替换)。如果不在4Ghz的八核/四核(八步CPU)上使用任何基准测试软件,应该需要2,3分钟左右。这很好,但我需要它尽可能快。拥有HashMap和TreeSet非常耗时。例如,在addOrReplace中,我们有HashMap put方法(O(1))+TreeSet-remove-optional(O(logn))+TreeSet-add方法(O(logn))。这意味着有O(1)+O(logn)(可能)+O(logn)。Witch的速度比几乎任何一个集合都慢。当我们有数以百万计的唱片时,运营成本就会很高。例如,在TreeSet中,更快的方法是调用remove(Object o),add(Object o),然后检查contain(Object o),可选的remove(Object o)和add(Object o)。@exp2Tapavicki刚刚创建了100万条随机记录,其中大约50000条记录具有相同的id
,并将它们添加到数据存储中。运行时间:1.2秒。这是“费时”吗?我想都是亲戚吧。因为您需要对数据进行排序,所以您的总体性能将是O(n log n),这也是此代码的性能。对此你无能为力,那么“足够好”是什么呢?你确定你没有无法实现的目标吗?对不起,我在对问题的评论中添加了一些信息。例如,大约有1亿条记录被更新(添加/替换)和排序。如果不在4Ghz的八核/四核(八步CPU)上使用任何基准测试软件,时间应该在2,3分钟左右。请记住,随着记录数量的增加,添加/替换和排序的时间越来越长。@exp2Tapavicki您每天有1亿条新记录吗?或者你的意思是说你的“数据库”现在有1亿条记录,每天有N条新记录,并且你需要以id查找和查询top X的形式持续访问数据?因为它听起来越来越像你不想要一个内存中的解决方案(你最终会用完),但需要一个数据库解决方案,就像我们已经向你建议的那样。它没有任何数据库。数据库通信只会增加时间。代码应该尽可能简单。只有一亿张唱片。完成后就这样了。不要再动手术了。
class DataStore {
private Map<Integer, Record> recordsById = new HashMap<>();
private TreeSet<Record> recordsByPrice = new TreeSet<>();
public Optional<Record> addOrReplace(Record newRecord) {
Record oldRecord = this.recordsById.put(newRecord.getId(), newRecord);
if (oldRecord != null)
this.recordsByPrice.remove(oldRecord);
this.recordsByPrice.add(newRecord);
return Optional.ofNullable(oldRecord);
}
public Optional<Record> remove(int id) {
Record oldRecord = this.recordsById.remove(id);
if (oldRecord != null)
this.recordsByPrice.remove(oldRecord);
return Optional.ofNullable(oldRecord);
}
public Optional<Record> getById(int id) {
return Optional.ofNullable(this.recordsById.get(id));
}
public NavigableSet<Record> getByPrice(double price) {
return this.recordsByPrice.subSet(new Record(price, Integer.MIN_VALUE), true,
new Record(price, Integer.MAX_VALUE), true);
}
public NavigableSet<Record> getByPriceRange(double fromPriceInclusive, double toPriceExclusive) {
return this.recordsByPrice.subSet(new Record(fromPriceInclusive, Integer.MIN_VALUE), true,
new Record(toPriceExclusive, Integer.MIN_VALUE), false);
}
}