import sqlite3 as sql
base1 = sql.connect('test1.db', isolation_level=None)
base2 = sql.connect('test2.db', isolation_level=None)
cursor1 = base1.cursor()
cursor1.execute("""
CREATE TABLE test(
key INT,
data BLOB
)""")
cursor2 = base2.cursor()
cursor2.execute("""
CREATE TABLE test(
key INT,
data BLOB,
PRIMARY KEY (key)
) WITHOUT ROWID""")
bl = b'\xFE' * 1200
for i in range(500):
query = 'insert into test(key, data) values (?, ?)'
params = (i + 1000, bl)
cursor1.execute(query, params)
cursor2.execute(query, params)
base1.close()
base2.close()
Вопрос - почему в первом варианте база с таблицей получилась 692 КБ, а во втором - 2347 КБ, более чем в 3 раза больше!
открой бинарник во втором варианте в Hex редакторе и просто посмотри на него с прищуром.
открой бинарник во втором варианте в Hex редакторе и просто посмотри на него с прищуром.
SQLite version 3.37.2 2022-01-06 13:25:41
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
Тут либо внутри sqlite файла нерационально распределяется место между сегментом таблицы индекса либо сам индес в блоках постоянно сплиттится.
Connection conn = DriverManager.getConnection("jdbc:sqlite:/db/sqlite-demo/demo.db");
conn.setAutoCommit(false);
int BYTES = 300;
int ROWS = 500;
//Blob blob = conn.createBlob(); // java.sql.SQLFeatureNotSupportedException
PreparedStatement pst = conn.prepareStatement("insert into test(key) values(?)");
for(int i = 0;i < ROWS;i++) {
pst.setInt(1, i);
pst.execute();
}
pst.close();
conn.commit();
PreparedStatement pst2 = conn.prepareStatement("update test set data = ? where key = ?");
byte[] deadBeef = prepareDeadBeef(BYTES); // DE AD BE EF ....
for(int i = 0; i < 500 ; i++) {
pst2.setBytes(1, deadBeef);
pst2.setInt(2, i);
pst2.execute();
}
pst2.close();
conn.commit();
conn.close();
/db/sqlite-demo$ ls -lF
total 176
-rwxrwxrwx 1 user user 180224 Jan 11 13:44 demo.db*
$ hexdump -C demo.db | head -n 100
00000000 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 |SQLite format 3.|
00000010 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 2c |.....@ .......,|
PreparedStatement pst2 = conn.prepareStatement("update test set data = ? where key = ?");
byte[] deadBeef = prepareDeadBeef(BYTES); // DE AD BE EF ...
System.out.printf("size(deadBeef blob) = %d\n", deadBeef.length);
for(int i = 0; i < 500 ; i++) {
pst2.setBytes(1, deadBeef);
pst2.setInt(2, i);
pst2.execute();
}
pst2.close();
conn.commit();
conn.close();
size(deadBeef blob) = 1200
Zeroes : 2154456
Dead beefs : 160096
(sizeof(int) + 1200) * 500 = (logical size of information) = 602000
Zeroes to Used : 0.926039
Zeroes : 1706596
Dead beefs : 4568
(sizeof(int) + 1200) * 500 = (logical size of information) = 602000
Zeroes to Used : 0.727137
$ sqlite3 test2.db .dbinfo
database page size: 4096
write format: 1
read format: 1
reserved bytes: 0
file change counter: 501
database page count: 573
freelist page count: 0
schema cookie: 1
schema format: 4
default cache size: 0
autovacuum top root: 0
incremental vacuum: 0
text encoding: 1 (utf8)
user version: 0
application id: 0
software version: 3037002
number of tables: 1
number of indexes: 0
number of triggers: 0
number of views: 0
schema size: 100
data version 1
/** Disk-Space Utilization Report For test1.db
Page size in bytes................................ 4096
Pages in the whole file (measured)................ 169
Pages in the whole file (calculated).............. 169
Pages that store data............................. 169 100.0%
Pages on the freelist (per header)................ 0 0.0%
Pages on the freelist (calculated)................ 0 0.0%
Pages of auto-vacuum overhead..................... 0 0.0%
Number of tables in the database.................. 2
Number of indices................................. 0
Number of defined indices......................... 0
Number of implied indices......................... 0
Size of the file in bytes......................... 692224
Bytes of user payload stored...................... 603000 87.1%
*** Page counts for all tables with their indices *****************************
TEST.............................................. 168 99.41%
SQLITE_SCHEMA..................................... 1 0.59%
*** Page counts for all tables and indices separately *************************
TEST.............................................. 168 99.41%
SQLITE_SCHEMA..................................... 1 0.59%
*** All tables ****************************************************************
Percentage of total database...................... 100.0%
Number of entries................................. 501
Bytes of storage consumed......................... 692224
Bytes of payload.................................. 603080 87.1%
Bytes of metadata................................. 5619 0.81%
Average payload per entry......................... 1203.75
Average unused bytes per entry.................... 166.72
Average metadata per entry........................ 11.22
Average fanout.................................... 167.00
Maximum payload per entry......................... 1206
Entries that use overflow......................... 0 0.0%
Index pages used.................................. 1
Primary pages used................................ 168
Overflow pages used............................... 0
Total pages used.................................. 169
Unused bytes on index pages....................... 2798 68.3%
Unused bytes on primary pages..................... 80727 11.7%
Unused bytes on overflow pages.................... 0
Unused bytes on all pages......................... 83525 12.1%
*** Table SQLITE_SCHEMA *******************************************************
Percentage of total database...................... 0.59%
Number of entries................................. 1
Bytes of storage consumed......................... 4096
Bytes of payload.................................. 80 2.0%
Bytes of metadata................................. 112 2.7%
B-tree depth...................................... 1
Average payload per entry......................... 80.00
Average unused bytes per entry.................... 3904.00
Average metadata per entry........................ 112.00
Maximum payload per entry......................... 80
Entries that use overflow......................... 0 0.0%
Primary pages used................................ 1
Overflow pages used............................... 0
Total pages used.................................. 1
Unused bytes on primary pages..................... 3904 95.3%
Unused bytes on overflow pages.................... 0
Unused bytes on all pages......................... 3904 95.3%
*** Table TEST ****************************************************************
Percentage of total database...................... 99.41%
Number of entries................................. 500
Bytes of storage consumed......................... 688128
Bytes of payload.................................. 603000 87.6%
Bytes of metadata................................. 5507 0.80%
B-tree depth...................................... 2
Average payload per entry......................... 1206.00
Average unused bytes per entry.................... 159.24
Average metadata per entry........................ 11.01
Average fanout.................................... 167.00
Non-sequential pages.............................. 0 0.0%
Maximum payload per entry......................... 1206
Entries that use overflow......................... 0 0.0%
Index pages used.................................. 1
Primary pages used................................ 167
Overflow pages used............................... 0
Total pages used.................................. 168
Unused bytes on index pages....................... 2798 68.3%
Unused bytes on primary pages..................... 76823 11.2%
Unused bytes on overflow pages.................... 0
Unused bytes on all pages......................... 79621 11.6%
/** Disk-Space Utilization Report For test2.db
Page size in bytes................................ 4096
Pages in the whole file (measured)................ 573
Pages in the whole file (calculated).............. 573
Pages that store data............................. 573 100.0%
Pages on the freelist (per header)................ 0 0.0%
Pages on the freelist (calculated)................ 0 0.0%
Pages of auto-vacuum overhead..................... 0 0.0%
Number of tables in the database.................. 2
Number of indices................................. 0
Number of defined indices......................... 0
Number of implied indices......................... 0
Size of the file in bytes......................... 2347008
Bytes of user payload stored...................... 603000 25.7%
*** Page counts for all tables with their indices *****************************
TEST.............................................. 572 99.83%
SQLITE_SCHEMA..................................... 1 0.17%
*** Page counts for all tables and indices separately *************************
TEST.............................................. 572 99.83%
SQLITE_SCHEMA..................................... 1 0.17%
*** All tables ****************************************************************
Percentage of total database...................... 100.0%
Number of entries................................. 501
Bytes of storage consumed......................... 2347008
Bytes of payload.................................. 603121 25.7%
Bytes of metadata................................. 6972 0.30%
Average payload per entry......................... 1203.83
Average unused bytes per entry.................... 3466.90
Average metadata per entry........................ 13.92
Average fanout.................................... 7.00
Maximum payload per entry......................... 1206
Entries that use overflow......................... 500 99.80%
Index pages used.................................. 9
Primary pages used................................ 64
Overflow pages used............................... 500
Total pages used.................................. 573
Unused bytes on index pages....................... 5694 15.4%
Unused bytes on primary pages..................... 43721 16.7%
Unused bytes on overflow pages.................... 1687500 82.4%
Unused bytes on all pages......................... 1736915 74.0%
*** Table SQLITE_SCHEMA *******************************************************
Percentage of total database...................... 0.17%
Number of entries................................. 1
Bytes of storage consumed......................... 4096
Bytes of payload.................................. 121 3.0%
Bytes of metadata................................. 112 2.7%
B-tree depth...................................... 1
Average payload per entry......................... 121.00
Average unused bytes per entry.................... 3863.00
Average metadata per entry........................ 112.00
Maximum payload per entry......................... 121
Entries that use overflow......................... 0 0.0%
Primary pages used................................ 1
Overflow pages used............................... 0
Total pages used.................................. 1
Unused bytes on primary pages..................... 3863 94.3%
Unused bytes on overflow pages.................... 0
Unused bytes on all pages......................... 3863 94.3%
*** Table TEST ****************************************************************
Percentage of total database...................... 99.83%
Number of entries................................. 500
Bytes of storage consumed......................... 2342912
Bytes of payload.................................. 603000 25.7%
Bytes of metadata................................. 6860 0.29%
B-tree depth...................................... 3
Average payload per entry......................... 1206.00
Average unused bytes per entry.................... 3466.10
Average metadata per entry........................ 13.72
Average fanout.................................... 7.00
Non-sequential pages.............................. 0 0.0%
Maximum payload per entry......................... 1206
Entries that use overflow......................... 500 100.0%
Index pages used.................................. 9
Primary pages used................................ 63
Overflow pages used............................... 500
Total pages used.................................. 572
Unused bytes on index pages....................... 5694 15.4%
Unused bytes on primary pages..................... 39858 15.4%
Unused bytes on overflow pages.................... 1687500 82.4%
Unused bytes on all pages......................... 1733052 74.0%
X is U-35 for table btree leaf pages or ((U-12)*64/255)-23 for index pages.