如何添加索引以优化SQLITE查询?
我正在为我的Python应用程序使用SQLite数据库。
在该数据库中,有一个巨大的表(约500万记录),并且一些查询太慢了。
这是表:
CREATE TABLE mytable(
ID integer PRIMARY KEY,
Code text,
Value1 float,
Value2 float,
Date text);
慢速查询的示例:
SELECT MIN(ID) FROM mytable WHERE Date >= datetime('2022-05-25');
如何优化表以更快地执行上一个查询?由于日期列的ID列的顺序相同,我可以在日期列中添加索引吗?
I'm using a Sqlite database for my Python application.
In that database there's a huge table (about 5M records) and some queries are too slow.
Here is the table:
CREATE TABLE mytable(
ID integer PRIMARY KEY,
Code text,
Value1 float,
Value2 float,
Date text);
and an example of slow query:
SELECT MIN(ID) FROM mytable WHERE Date >= datetime('2022-05-25');
How can I optimise the table to perform the previous query faster? Since the Date column have the same order of the ID column, can I add an index on the Date column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对可以在DateTime列中添加索引。根据您的查询,它肯定会有所帮助。
因此,最后,您将在此处查看类似的内容:
您可以在此处找到有关SQLite索引的更多信息: https://www.tutorialspoint.com/sqlite/sqlite_indexes.htm
You most definitely can add an index to the datetime column. Based on your query it would certainly help.
So in the end, you would be looking at something like this:
You can find more info regarding Sqlite indexes here: https://www.tutorialspoint.com/sqlite/sqlite_indexes.htm