mysql中的等价查询是什么?

时间:2010-08-03 02:23:19

标签: mysql sql-server

查询1:占用最长时间的前10个代码

select top 10 
  source_code,
  stats.total_elapsed_time/1000000 as seconds,
  last_execution_time from sys.dm_exec_query_stats as stats
cross apply(SELECT 
              text as source_code 
            FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
order by total_elapsed_time desc

查询2:占用最多physical_reads的前10个代码

select top 10 
  source_code,
  stats.total_elapsed_time/1000000 as seconds,
  last_execution_time from sys.dm_exec_query_stats as stats
cross apply(SELECT 
              text as source_code 
            FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
order by total_physical_reads desc

取自article

4 个答案:

答案 0 :(得分:5)

在MySQL中,您需要从日志文件中捕获此信息,而不是通过查询。有人可能会告诉你一个查询是可能的,但它们对你不公平。参见:

http://dev.mysql.com/doc/refman/5.1/en/log-tables.html “目前,记录到表会导致服务器开销大大超过记录到文件。”

..非常重要,如果你问这个问题,你不想使用它。

所以现在你的问题变成了“你如何用日志文件做到这一点?”。查询的物理读取次数未记录在stock-MySQL版本中。它虽然可以在Percona Server中使用。增强很棒(即使我有偏见,我为Percona工作):

http://www.percona.com/docs/wiki/patches:slow_extended

下一个问题是如何聚合日志,以便您可以找到这些详细信息。为此,我建议使用mk-query-digest。 http://www.maatkit.org/doc/mk-query-digest.html

答案 1 :(得分:1)

SELECT TOP 10 ...是MySQL中的SELECT ... LIMIT 10。如果您询问与INNER JOIN没有太大区别的CROSS APPLY,请参阅When should I use Cross Apply over Inner Join?

答案 2 :(得分:1)

您是否在ServerFault上看过这个Q& A?

How do I profile MySQL?

答案 3 :(得分:0)

select 
source_code,
stats.total_elapsed_time/1000000 as seconds,
last_execution_time from sys.dm_exec_query_stats as stats
inner join(SELECT 
          text as source_code 
        FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
order by total_elapsed_time desc 
limit 10  

select
source_code,
stats.total_elapsed_time/1000000 as seconds,
last_execution_time from sys.dm_exec_query_stats as stats
inner join(SELECT 
          text as source_code 
        FROM sys.dm_exec_sql_text(sql_handle)) AS query_text
order by total_physical_reads desc
limit 10