有没有一个工具可以分析 sqlite 查询?

发布于 2024-09-08 17:47:09 字数 200 浏览 4 评论 0原文

我正在使用 SQLite 数据库,并且希望通过索引或完全重组它们来加快查询速度。

是否有一个工具可以分析查询,可以帮助我确定哪些地方速度变慢?

我知道我可以将查询输入到像 SQLite Administrator 这样的工具中来计时,但我正在寻找比这更系统的东西——也许是一些位于后台并查看我在一个数据库中输入的所有查询的东西。期间,给出任何瓶颈的分解。

I am using a SQLite database and would like to speed up my queries, perhaps with indexes or by restructuring them altogether.

Is there a tool to profile queries, that might help me decide where things are slowing down?

I know I could just enter queries into a tool like SQLite Administrator to time them, but I'm looking for something a bit more systematic than that -- perhaps something that sits in the background and looks at all the queries that I enter over a period, giving a breakdown of any bottle necks.

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

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

发布评论

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

评论(4

心病无药医 2024-09-15 17:47:09

这只能回答问题的一部分(不幸的是,这是最无用的部分)。

我用谷歌搜索了这个,因为我正在寻找一些东西来计时查询,而 sqlite3 客户端有一个计时器元命令。

sqlite> .timer on

从那里开始,所有查询结果都将附加 cpu 计时器统计信息。
希望这至少有一点帮助。

This will only answer one part of the question (the most unhelpful part, unfortunately).

I googled this up because I was looking for something to time queries and the sqlite3 client has a timer meta command.

sqlite> .timer on

from there on in, all query results will have cpu timer statistics appended.
Hope this helps at least a little bit.

猥︴琐丶欲为 2024-09-15 17:47:09

你在这里有各种各样的问题。要查看运行的查询以及每个查询需要多长时间,您需要修改 sqlite3.dll(如果应用程序链接到该应用程序)或者如果它是您自己的应用程序,您可以更轻松地将其写入代码中(我们这样做并长期执行)查询、交易、计时等)。

对于单个查询分析,您可以使用EXPLAIN。它不会告诉您查询中各个步骤的时间,但会告诉您查询是如何执行的。

http://www.sqlite.org/lang_explain.html

SQL 语句前面可以带有关键字“EXPLAIN”或短语“EXPLAIN QUERY PLAN”。任一修改都会导致 SQL 语句表现为查询,并返回有关在省略 EXPLAIN 关键字或短语的情况下 SQL 语句将如何操作的信息。

当 EXPLAIN 关键字单独出现时,它会导致语句表现为查询,返回虚拟机指令序列(如果 EXPLAIN 关键字不存在的话,虚拟机指令序列将用于执行该命令)。当 EXPLAIN QUERY PLAN 短语出现时,该语句返回有关将使用哪些索引的高级信息。

EXPLAIN 和 EXPLAIN QUERY PLAN 的输出仅用于交互式分析和故障排除。输出格式的详细信息可能会因 SQLite 的一个版本而异。应用程序不应使用 EXPLAIN 或 EXPLAIN QUERY PLAN,因为它们的确切行为未记录、未指定且可变。

You have a mix of questions in here. To view what queries are run and how long each takes, you'll need to either modify sqlite3.dll if an application is linking to that or if it's your own application you can write it into your code easier (we do this and long all queries, transactions, timings, etc.).

For individual query analysis, you can use EXPLAIN. It won't tell you timing of individual steps within a query but it will tel you how the query was executed.

http://www.sqlite.org/lang_explain.html

An SQL statement can be preceded by the keyword "EXPLAIN" or by the phrase "EXPLAIN QUERY PLAN". Either modification causes the SQL statement to behave as a query and to return information about how the SQL statement would have operated if the EXPLAIN keyword or phrase had been omitted.

When the EXPLAIN keyword appears by itself it causes the statement to behave as a query that returns the sequence of virtual machine instructions it would have used to execute the command had the EXPLAIN keyword not been present. When the EXPLAIN QUERY PLAN phrase appears, the statement returns high-level information about what indices would have been used.

The output from EXPLAIN and EXPLAIN QUERY PLAN is intended for interactive analysis and troubleshooting only. The details of the output format are subject to change from one release of SQLite to the next. Applications should not use EXPLAIN or EXPLAIN QUERY PLAN since their exact behavior is undocumented, unspecified, and variable.

鹿港小镇 2024-09-15 17:47:09

现在 SQLite 有实验性的 sqlite3_tracesqlite3_profile (请参阅 https://www.sqlite.org/c3ref/profile.html 了解详细信息)。它们可以在长时间测试期间统计/调查罪魁祸首时派上用场。

Now SQLite has experimental sqlite3_trace and sqlite3_profile (see https://www.sqlite.org/c3ref/profile.html for details). They can come in handy for having statistics/investigating culprit during long tests.

半世晨晓 2024-09-15 17:47:09

这个古老的话题值得一个现代的答案:

使用 SQLite 客户端,您可以启用以下功能来帮助您在进行查询时进行基准测试和分析迭代它们:

  1. .timer on 将计时信息输出到 stdout,例如
sqlite> select * from foo;
a|1
b|2
Run Time: real 0.000 user 0.000056 sys 0.000053
  1. .eqp full 为查询启用 EXPLAIN QUERY PLAN
sqlite> select * from foo;
QUERY PLAN
`--SCAN foo
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     8     0                    0   Start at 8
1     OpenRead       0     2     0     2              0   root=2 iDb=0; foo
2     Rewind         0     7     0                    0   
3       Column         0     0     1                    0   r[1]=foo.x
4       Column         0     1     2                    0   r[2]=foo.y
5       ResultRow      1     2     0                    0   output=r[1..2]
6     Next           0     3     0                    1   
7     Halt           0     0     0                    0   
8     Transaction    0     0     1     0              1   usesStmtJournal=0
9     Goto           0     1     0                    0   
a|1
b|2
  1. .trace stdout --profile 启用分析器输出,例如
sqlite> select * from foo;
a|1
b|2
select * from foo; -- 0 ns

这种有状态的方法效果很好如果您正在使用 .read 分析文件中的现有查询,并且您不想手动修改解释语句。您可以使用 .parameter set提供查询参数 用于参数化查询。

This old thread deserves a modern answer: yes.

With the SQLite client, you can enable the following features to help you benchmark and profile your queries as you iterate on them:

  1. .timer on to output timing information to stdout, e.g.
sqlite> select * from foo;
a|1
b|2
Run Time: real 0.000 user 0.000056 sys 0.000053
  1. .eqp full to enable EXPLAIN QUERY PLAN for your queries
sqlite> select * from foo;
QUERY PLAN
`--SCAN foo
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     8     0                    0   Start at 8
1     OpenRead       0     2     0     2              0   root=2 iDb=0; foo
2     Rewind         0     7     0                    0   
3       Column         0     0     1                    0   r[1]=foo.x
4       Column         0     1     2                    0   r[2]=foo.y
5       ResultRow      1     2     0                    0   output=r[1..2]
6     Next           0     3     0                    1   
7     Halt           0     0     0                    0   
8     Transaction    0     0     1     0              1   usesStmtJournal=0
9     Goto           0     1     0                    0   
a|1
b|2
  1. .trace stdout --profile to enable profiler output, e.g.
sqlite> select * from foo;
a|1
b|2
select * from foo; -- 0 ns

This stateful approach works nicely if you are profiling existing queries from files with .read <file> and you do not want to manually hack in explain statements. You can provide query parameters with .parameter set <name> <value> for parameterized queries.

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