- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我使用默认精度设置创建 DECIMAL
列时,在 H2 Web 控制台中,该列被定义为 DECIMAL(65535, 32767)
,命令“show columns from .. ."给我 DECIMAL(65535)
。
H2 中的 Decimal 数据类型映射到 BigDecimal
(来自 H2 文档),但我不确定 H2 如何处理它。
如果我使用较小的精度,性能是否会提高?
最佳答案
要了解有关 H2 的 DECIMAL
数据类型的更多信息,请检查 h2-x.y.z.jar 附带的
.org.h2.value.ValueDecimal
类
仔细观察,您会发现默认值如您所提到的:
/** The default precision for a decimal value. */
static final int DEFAULT_PRECISION = 65535;
/** The default scale for a decimal value. */
static final int DEFAULT_SCALE = 32767;
仔细观察ValueDecimal
:
private final BigDecimal value;
和org.h2.store.Data
:
public Value readValue() {
...
case Value.DECIMAL: {
int scale = readVarInt();
int len = readVarInt();
byte[] buff = DataUtils.newBytes(len);
read(buff, 0, len);
BigInteger b = new BigInteger(buff);
return ValueDecimal.get(new BigDecimal(b, scale));
}
您可以看到DECIMAL
只不过是BigDecimal
。也就是说,您在使用 java.math.BigDecimal
时将面临的所有性能问题,在使用 DECIMAL
时都会面临。
如果您真的很喜欢它,您可以进一步研究该类(class),看看精度/比例发挥的确切作用。
如果我们查看文档,所有 H2 关于 DECIMAL
数据类型和性能的说法是:
The
DECIMAL
/NUMERIC
type is slower and requires more storage than theREAL
andDOUBLE
types.
所以他们说这是事实。
但是既然你谈论的是性能,我们可以切入主题并做一些测试。测试类的代码如下,让我们看看输出/结果:
TYPE INSERT time COUNT() time .db Size (kb)
DECIMAL(20,2) 6.978 0.488 27958.0
DECIMAL(100,2) 4.879 0.407 25648.0
DECIMAL(100,80) 8.794 0.868 90818.0
DECIMAL(60000,2) 4.388 0.4 25104.0
DECIMAL(1000,900) 112.905 6.549 1016534.0
REAL 5.938 0.318 22608.0
DOUBLE 6.985 0.416 25088.0
正如您所看到的,当精度改变时,时间或存储大小没有明显变化(精度20
大约需要与60000
一样多的时间/大小! )。
问题是当你改变比例时。这是你应该担心的;如您所见,DECIMAL(100,2)
和 DECIMAL(100,80)
显示时间和存储空间都有很大增加。
DECIMAL(1000,900)
需要超过 1 GB(!!!) 来存储完全相同的值。
最后,在上面的测试中,REAL
和 DOUBLE
似乎并不比 DECIMAL
好多少(它们甚至可能看起来更差)。但尝试更改插入的行数(测试方法中的 for
循环),数字越大,它们似乎响应越好。
* DECIMAL(20,2)
似乎比其他的慢/大。那不是真的。实际上,无论您选择先运行什么,都会稍微慢一些/更大一些。想想看...
public class Main {
public static void main(String[] a) throws Exception {
Class.forName("org.h2.Driver");
System.out.format("%-18s%-15s%-15s%-15s", "TYPE", "INSERT time", "COUNT() time", ".db Size (kb)");
System.out.println();
testPerformance("TEST_DECIMAL_20_2", "DECIMAL(20,2)");
testPerformance("TEST_DECIMAL_100_2", "DECIMAL(100,2)");
testPerformance("TEST_DECIMAL_100_80", "DECIMAL(100,80)");
testPerformance("TEST_DECIMAL_60000_2", "DECIMAL(60000,2)");
testPerformance("TEST_DECIMAL_1000_900", "DECIMAL(1000,900)");
testPerformance("TEST_REAL", "REAL");
testPerformance("TEST_DOUBLE", "DOUBLE");
}
private static void testPerformance(String dbName, String type) throws SQLException {
System.out.format("%-18s", type);
Connection conn = DriverManager.getConnection("jdbc:h2:" + dbName, "sa", "");
conn.createStatement().execute("DROP TABLE IF EXISTS TEST;");
conn.createStatement().execute("CREATE TABLE TEST (DECTEST " + type +" )");
long insertStartTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
conn.createStatement().execute("INSERT INTO TEST (DECTEST) VALUES (12345678901234.45)");
}
double insertTime = ((double)(System.currentTimeMillis()-insertStartTime))/1000;
System.out.format("%-15s", insertTime+"");
long countStartTime = System.currentTimeMillis();
conn.createStatement().executeQuery("select COUNT(DECTEST) from TEST");
double countTime = ((double)(System.currentTimeMillis()-countStartTime))/1000;
System.out.format("%-15s", countTime+"");
conn.close();
double fileSize = (double)new File(dbName+".h2.db").length() / 1024;
System.out.format("%-15s", fileSize+"");
System.out.println();
}
}
关于sql - h2默认DECIMAL精度性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16274887/
我是 Python 编程的新手,最近我一直在学习数据类型的某些方面。正如我所读,decimal.Decimal() 方法创建了一个小数对象; decimal.Decimal.from_float()
这是我今天在 filmmaster.com: 遇到的错误 PicklingError: Can't pickle : it's not the same object as decimal.Decim
简单的问题 - 为什么 Decimal 类型定义这些常量?何必呢? 我正在寻找一个原因,为什么这是由语言定义的,而不是可能的用途或对编译器的影响。为什么首先把它放在那里?编译器可以像 Decimal.
我在用 Python 编码为 JSON 时遇到问题,特别是 decimal.Decimal 值。我正在使用它为 Google App Engine 应用程序输出 JSON。 为了避免 Python 中
我在 Django 项目中有一些 python 代码曾经可以正常工作。托管该项目的服务器丢失了,我不得不将代码复制到新服务器上。现在,我收到一个似乎毫无意义的错误。 我的一个 python 文件中包含
我发现生成具有特定数量 的 decimal.Decimal 数字的最佳方法 significant figures 是用来初始化下面的变量 kluge 的: import decimal THIS_I
我在应用空合并运算符时遇到以下错误。 private decimal _currentImpulseId; // ... later on used in public property getter
我想使用 decimal.Decimal 对转换后的数字字符串进行算术运算。无论转换后的数字字符串有多少小数,我都希望返回有两位小数。 在这种特殊情况下,为什么两位小数精度返回一位小数,为什么三位小数
我想使用 decimal.Decimal 对转换后的数字字符串进行算术运算。无论转换后的数字字符串有多少小数,我都希望返回有两位小数。 在这种特殊情况下,为什么两位小数精度返回一位小数,为什么三位小数
是什么导致 sum 始终为 null? object[] data = new object[] { 2.2M, 3m, 1, "string", true,
我似乎失去了很多 float 的精度。 例如我需要求解一个矩阵: 4.0x -2.0y 1.0z =11.0 1.0x +5.0y -3.0z =-6.0 2.0x +2.0y +5.0z =7.0
这个问题在这里已经有了答案: Why python decimal.Decimal precision differs with equable args? (2 个答案) 关闭 5 年前。 我知道
当我尝试转换 Task 时出现此错误至 decimal : Could not load file or assembly 'EntityFramework, Version=5.0.0.0, Cul
我听到有人说在 C# 中,大写十进制比小写十进制使用更多的内存,因为小写十进制被解析为小写十进制并且需要内存。 这是真的吗? 最佳答案 没有。 decimal 只是 System.Decimal 的别
我有一个对象,它是 dict、list、常规数据类型和 decimal.Decimal 的嵌套组合。我想用 PyMongo 将这个对象插入到 MongoDB 中。 PyMongo 拒绝插入 Decim
sdr 是我的 sqldatareader,我想检查小数类型的 curPrice 值是否为空。 inrec.curPrice = sdr.IsDBNull(7) ? (十进制?)空:sdr.GetDe
变量 'a' 的类型可以是 - int/float/decimal.Decimal(但不是字符串) 我想检查它是否是 decimal.Decimal 类型。 以下作品: import decimal
我正在使用一个在函数调用中使用一些十进制类型参数的库。但是我有 numpy.int32 类型变量。当我将它传递给函数调用时,出现以下错误: TypeError: conversion from num
考虑以下代码: public class DecimalWrapper { public static implicit operator DecimalWrapper
考虑以下代码: public class DecimalWrapper { public static implicit operator DecimalWrapper
我是一名优秀的程序员,十分优秀!