---
title: "数据库统计执行耗时"
author: "futurep_p"
date: "2010-10-20T22:06:04Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2010-10-20T22:07:43Z"
id: 789957
url: "https://touchfu.iteye.com/blog/789957"
---

前提：表字段包含：创建时间、最后修改时间。时间字段为timestamp类型。

要求：统计业务执行耗时，单位毫秒，分区间统计。

执行：

```
select stat, count(*)
  from (select case
                 when milli <= 10 then
                  'time<=10'
                 when milli > 10 and milli <= 20 then
                  '10<time<=20'
                 else
                  'time>20'
               end stat
          from (select to_number(substr((gmt_modified - gmt_create),
                                        instr((gmt_modified - gmt_create), ' ') + 4,
                                        2) * 60 +
                                 substr((gmt_modified - gmt_create),
                                        instr((gmt_modified - gmt_create), ' ') + 7,
                                        2) ||
                                 substr((gmt_modified - gmt_create),
                                        instr((gmt_modified - gmt_create), ' ') + 10,
                                        3)) milli
                  from table))
 group by stat
```

 

PS：作个记录