如何在android中找出Sqlite查询的执行时间

nit*_*esh 2 performance android android-sqlite

我正在尝试优化我的Sqlite查询的性能.

我想知道是否有任何方法可以找到每个Sqlite语句的执行时间,或者是否有任何工具允许在Android SDK中查看语句执行时间?

虽然我熟悉查询计时器.timer On.timer show命令.

例:

查询超时时间

任何答案都真的很感激!

Ale*_*pov 7

与其自己计算执行时间,不如让 sqlite 为您做。
这里

/**
 * Controls the printing of wall-clock time taken to execute SQL statements
 * as they are executed.
 *
 * Enable using "adb shell setprop log.tag.SQLiteTime VERBOSE".
 */
public static final boolean DEBUG_SQL_TIME =
        Log.isLoggable("SQLiteTime", Log.VERBOSE);
Run Code Online (Sandbox Code Playgroud)

因此,要启用执行时间跟踪运行:

adb shell setprop log.tag.SQLiteTime VERBOSE
Run Code Online (Sandbox Code Playgroud)

您必须重新启动应用程序才能重新加载新设置**。紧接着,您将开始在 logcat 中看到这些日志记录:

02-14 12:27:00.457 11936-12137/osom.info.dbtest I/Database: elapsedTime4Sql|/data/data/osom.info.dbtest/databases/test.db|1.000 ms|UPDATE TestTable SET key=? 哪里 _id=1

** 有时这还不够,所以运行adb shell stopadb shell start

要停止打印这些日志,请重新启动设备(重新启动之间不会保留此属性)或将该属性设置为更高的日志级别,即:

adb shell setprop log.tag.SQLiteTime ERROR
Run Code Online (Sandbox Code Playgroud)

请注意使用Jetpack 的 Room:此解决方案也适用于 Room(因为 Room 使用 Sqlite 作为底层数据库)。


Aro*_*ncz 6

你可以用Java做到这一点:

int startTime = System.currentTimeMillis();

... // Execute the query here

int executionTime = System.currentTimeMillis() - startTime; // This variable now contains the time taken by the query, in milliseconds
Run Code Online (Sandbox Code Playgroud)