我在运行Linux的ARM嵌入式平台上有一个sqlite数据库,资源有限.存储设备是microSD卡. Sqlite版本是3.7.7.1.访问sqlite的应用程序是用C语言编写的.
我想以规则的间隔知道几个表中的行数.我目前正在使用
select count(*) from TABLENAME;
获取此信息.我遇到了性能问题:当表格大小达到某一点(~200K行)时,每次检查表格大小时都会有很多系统和iowait加载.
当我写这篇文章时,我虽然查找表中的行数会很快,因为它可能存储在某个地方.但是现在我怀疑sqlite实际上是查看所有行的,当我通过数据不适合磁盘缓存的点时,我得到了很多io加载.这大致适合数据库大小和可用内存.
任何人都能告诉我sqlite是否以我怀疑的方式行事?
有没有办法获得表行数而不产生这样的负载量?
编辑:plaes询问了表格布局:
CREATE TABLE %s (timestamp INTEGER PRIMARY KEY, offset INTEGER, value NUMERIC);
解决方法:
这个表有整数索引吗?如果没有,那么添加一个.否则它必须扫描整个表来计算项目.
这是SQLite代码中的注释的摘录,它实现了COUNT()解析和执行:
/* If isSimpleCount() returns a pointer to a Table structure, then
** the SQL statement is of the form:
**
** SELECT count(*) FROM <tbl>
**
** where the Table structure returned represents table <tbl>.
**
** This statement is so common that it is optimized specially. The
** OP_Count instruction is executed either on the intkey table that
** contains the data for table <tbl> or on one of its indexes. It
** is better to execute the op on an index, as indexes are almost
** always spread across less pages than their corresponding tables.
*/
[...]
/* Search for the index that has the least amount of columns. If
** there is such an index, and it has less columns than the table
** does, then we can assume that it consumes less space on disk and
** will therefore be cheaper to scan to determine the query result.
** In this case set iRoot to the root page number of the index b-tree
** and pKeyInfo to the KeyInfo structure required to navigate the
** index.
**
** (2011-04-15) Do not do a full scan of an unordered index.
此外,您可以使用EXPLAIN QUERY PLAN来查询您的查询.
原文地址:https://codeday.me/bug/20190630/1336292.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。