0

嗨,我正在使用 MySQL,

SHOW PROFILES;

返回一个长列表,例如这样的行:

 |Query_ID|Duration     |Query
>| 9    |    0.00028550 |   INSERT INTO Duration(Status,DURATION,Query_ID)|

我正在尝试复制它,因此我可以将结果插入到表中并添加一个WHERE子句。

所以我尝试了这个

SELECT Query_ID, STATE, FORMAT(DURATION, 6) AS DURATION
FROM INFORMATION_SCHEMA.PROFILING
WHERE QUERY_ID = 9;

但这会返回一个较低级别的视图(我不想要):

起始 0.000053 开表 0.000088

这与以下内容相同:

SHOW PROFILE;

我将如何重新创建SHOW PROFILES?所以我可以执行这样的事情:

INSERT INTO Duration(Query_ID,Status,DURATION)
SELECT * FROM SHOW PROFILE
WHERE PROFILE.QUERY_ID = 9;

谢谢

4

1 回答 1

1

按 分组QUERY_ID。要获取状态,您需要STATE具有分组最大值 SEQ的记录;要获得持续时间,只需DURATION对组中的每条记录求和:

INSERT INTO Duration
  (Query_ID, Status, DURATION)
SELECT QUERY_ID, STATE, t.DURATION
FROM   INFORMATION_SCHEMA.PROFILING JOIN (
  SELECT   QUERY_ID, MAX(SEQ) AS SEQ, SUM(DURATION) AS DURATION
  FROM     INFORMATION_SCHEMA.PROFILING
  WHERE    QUERY_ID = 9
  GROUP BY QUERY_ID  -- superfluous in the presence of the above filter
) t USING (QUERY_ID, SEQ)
于 2013-02-19T21:21:26.673 回答