加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 计算sqlite数据库中的行

发布时间:2020-12-13 22:51:23 所属栏目:Linux 来源:网络整理
导读:我在运行 Linux的ARM嵌入式平台上有一个sqlite数据库,资源有限.存储设备是microSD卡. Sqlite版本是3.7.7.1.访问sqlite的应用程序是用C语言编写的. 我想以规则的间隔知道几个表中的行数.我目前正在使用 select count(*) from TABLENAME; 获取此信息.我遇到了
我在运行 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来查询您的查询.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读