【问题标题】:Is there a way to measure query time in sqlite at runtime?有没有办法在运行时测量 sqlite 中的查询时间?
【发布时间】:2012-11-16 22:32:33
【问题描述】:

我想测量sqlite中每条sql语句的执行时间。

我知道在 sqlite shell 中你可以只做 .timer,但我想知道如何以可编程的方式来做,这样我就可以知道当应用程序实时访问数据库时 sql 语句需要多少时间,不只是事后在 shell 中重播该语句?

我可以为 sqlite_profile 提供一个插件功能吗?

非常感谢

【问题讨论】:

  • 你使用什么语言?

标签: sqlite profiling query-optimization


【解决方案1】:

看了shell.c的代码后,发现文件顶部有以下相关信息:

/* Saved resource information for the beginning of an operation */
static struct rusage sBegin;

/*
** Begin timing an operation
*/
static void beginTimer(void){
  if( enableTimer ){
    getrusage(RUSAGE_SELF, &sBegin);
  }
}

/* Return the difference of two time_structs in seconds */
static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + 
         (double)(pEnd->tv_sec - pStart->tv_sec);
}

/*
** Print the timing results.
*/
static void endTimer(void){
  if( enableTimer ){
    struct rusage sEnd;
    getrusage(RUSAGE_SELF, &sEnd);
    printf("CPU Time: user %f sys %f\n",
       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
  }
}

这意味着,SQLite 本身不提供某种分析,因此您必须使用您的语言功能(例如 endtime-starttime)来完成这项工作。

【讨论】:

    【解决方案2】:

    sqliteman 提供了一些基本的分析功能。它显示查询执行时间。 好吧,尽管查询执行是在不同的处理器上(即 X86 与 ARM 相比),但它仍然可以帮助您在 sqlite 上编写优化的原始查询。 例如。我刚刚测试了一个没有索引的 where 子句的选择查询,它花了大约 0.019 秒,在创建索引后它只需要 0.008。

    这里是 sqlite man http://sourceforge.net/projects/sqliteman/的链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2021-04-02
      • 2011-07-05
      • 1970-01-01
      • 2012-11-15
      • 2010-10-23
      • 2012-11-22
      相关资源
      最近更新 更多