如何添加索引以优化SQLITE查询?

发布于 2025-02-01 04:54:03 字数 412 浏览 2 评论 0原文

我正在为我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2025-02-08 04:54:03

您绝对可以在DateTime列中添加索引。根据您的查询,它肯定会有所帮助。

因此,最后,您将在此处查看类似的内容:

Create Index myIndex On myTable(Date);

您可以在此处找到有关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:

Create Index myIndex On myTable(Date);

You can find more info regarding Sqlite indexes here: https://www.tutorialspoint.com/sqlite/sqlite_indexes.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文