当一个整数列在SQLite表中被标记为主键时,是否应该为它显式创建索引?SQLite似乎不会自动为主键列创建索引,但考虑到其目的,它可能无论如何都要对其进行索引?(我会一直在搜索那个专栏).
对于字符串主键,情况会有所不同吗?
我有一个表示安全摄像机NVR元数据的数据库.recording
每1分钟的视频片段有一个26字节的行.(如果你很好奇,一个设计文档正在进行这里.)我的设计限制是8个摄像头,1年(约4万行,半一万元左右的相机).我已经伪造了一些数据来测试性能.此查询比我预期的要慢:
select
recording.start_time_90k,
recording.duration_90k,
recording.video_samples,
recording.sample_file_bytes,
recording.video_sample_entry_id
from
recording
where
camera_id = ?
order by
recording.start_time_90k;
Run Code Online (Sandbox Code Playgroud)
这只是扫描相机的所有数据,使用索引过滤掉其他相机和订购.索引看起来像这样:
create index recording_camera_start on recording (camera_id, start_time_90k);
Run Code Online (Sandbox Code Playgroud)
explain query plan
看起来像预期:
0|0|0|SEARCH TABLE recording USING INDEX recording_camera_start (camera_id=?)
Run Code Online (Sandbox Code Playgroud)
行很小.
$ sqlite3_analyzer duplicated.db
...
*** Table RECORDING w/o any indices *******************************************
Percentage of total database...................... 66.3%
Number of entries................................. 4225560
Bytes of storage consumed......................... 143418368
Bytes of payload.................................. 109333605 76.2%
B-tree depth...................................... 4
Average payload per entry......................... 25.87
Average unused …
Run Code Online (Sandbox Code Playgroud)