在 SQLite 中计算表中行数的最有效方法是什么? [英] What is the most efficient way to count rows in a table in SQLite?

查看:43
本文介绍了在 SQLite 中计算表中行数的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直只使用SELECT COUNT(1) FROM X",但这可能不是最有效的.有什么想法吗?其他选项包括 SELECT COUNT(*) 或者可能获取最后插入的 id,如果它是自动递增的(并且从不删除).

I've always just used "SELECT COUNT(1) FROM X" but perhaps this is not the most efficient. Any thoughts? Other options include SELECT COUNT(*) or perhaps getting the last inserted id if it is auto-incremented (and never deleted).

如果我只想知道表中是否有任何东西呢?(例如,计数 > 0?)

How about if I just want to know if there is anything in the table at all? (e.g., count > 0?)

推荐答案

最好的方法是确保在单列上运行 SELECT COUNT (SELECT COUNT(*) 较慢) - 但 SELECT COUNT 始终是获​​取事物计数的最快方法(数据库在内部优化查询).

The best way is to make sure that you run SELECT COUNT on a single column (SELECT COUNT(*) is slower) - but SELECT COUNT will always be the fastest way to get a count of things (the database optimizes the query internally).

如果您查看下面的评论,您会看到为什么 SELECT COUNT(1) 可能是您的最佳选择.

If you check out the comments below, you can see arguments for why SELECT COUNT(1) is probably your best option.

这篇关于在 SQLite 中计算表中行数的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆