linux – 计算sqlite数据库中的行

我在运行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来查询您的查询.

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能