加快 SQLite 中的 SQL 选择速度

发布于 2024-11-02 09:48:55 字数 938 浏览 0 评论 0原文

我正在创建一个大型数据库,为了解决这个问题,假设包含 3 个表:

A.表“员工”,包含字段:

id = INTEGER PRIMARY INDEX AUTOINCRMENT

其他不重要

B.表“Job_Sites”,包含字段:

id = INTEGER PRIMARY INDEX AUTOINCRMENT

其他不重要

C.表“Workdays”,包含以下字段:

id = INTEGER PRIMARY INDEX AUTOINCRMENT

emp_id = 是Employees(id) 的外键

job_id = 是Job_Sites(id) 的外键

datew = INTEGER 代表实际工作日,表示按自 1970 年 1 月 1 日午夜以来的 Unix 日期(以秒为单位)


此数据库中最常见的操作是显示特定员工的工作日。我执行以下 select 语句:

SELECT * FROM Workdays WHERE emp_id='Actual Employee ID' AND job_id='Actual Job Site ID' AND datew>=D1 AND datew

我需要指出,D1 和 D2 是针对分别是搜索的月初和下个月。

我其实有两个问题:

  1. 除了主索引之外,我还应该设置任何字段作为索引吗? (抱歉,我似乎误解了整个索引概念)

  2. 有什么方法可以重写 Select 语句以加快速度。例如,其中的大多数检查都是为了查看实际员工 ID 和工作地点 ID 是否匹配。也许有办法将其拆分?

附言。忘了说,我在 Windows C++ 应用程序中使用 SQLite。

I'm making a large database that, for the sake of this question, let's say, contains 3 tables:

A. Table "Employees" with fields:

id = INTEGER PRIMARY INDEX AUTOINCREMENT

Others don't matter

B. Table "Job_Sites" with fields:

id = INTEGER PRIMARY INDEX AUTOINCREMENT

Others don't matter

C. Table "Workdays" with fields:

id = INTEGER PRIMARY INDEX AUTOINCREMENT

emp_id = is a foreign key to Employees(id)

job_id = is a foreign key to Job_Sites(id)

datew = INTEGER that stands for the actual workday, represented by a Unix date in seconds since midnight of Jan 1, 1970


The most common operation in this database is to display workdays for a specific employee. I perform the following select statement:

SELECT * FROM Workdays WHERE emp_id='Actual Employee ID' AND job_id='Actual Job Site ID' AND datew>=D1 AND datew<D2

I need to point out that D1 and D2 are calculated for the beginning of the month in search and for the next month, respectively.

I actually have two questions:

  1. Should I set any fields as indexes besides primary indexes? (Sorry, I seem to misunderstand the whole indexing concept)

  2. Is there any way to re-write the Select statement to maybe speed it up. For instance, most of the checks in it would be to see that the actual employee ID and job site ID match. Maybe there's a way to split it up?

PS. Forgot to say, I use SQLite in a Windows C++ application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无言温柔 2024-11-09 09:48:55

如果您经常使用上述查询,那么您可以通过创建包含查询中的列的多列索引来获得更好的性能:

CREATE INDEX WorkdaysLookupIndex ON Workdays (emp_id, job_id, datew);

有时您只需创建索引并尝试查询以查看哪个更快。

If you use the above query often, then you may get better performance by creating a multicolumn index containing the columns in the query:

CREATE INDEX WorkdaysLookupIndex ON Workdays (emp_id, job_id, datew);

Sometimes you just have to create the index and try your queries to see what is faster.

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