SQL oracle中如何衡量查询的性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1312663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to measure performance of query in oracle
提问by Cshah
I'm new to Oracle db. I have 2 queries which return the same result set. I want to measure the performance of each of them and choose the better one. How do I do that using Oracle SQL developer? I remember reading that certain tools provide stats. Any pointers on how to read these stats?
我是 Oracle 数据库的新手。我有 2 个查询返回相同的结果集。我想衡量他们每个人的表现并选择更好的一个。我如何使用 Oracle SQL 开发人员做到这一点?我记得读过某些工具提供统计数据。关于如何阅读这些统计数据的任何指示?
Update: As suggested by Rob Van, I used the tkprof utility to find the performance of my queries. A few parameters I could understand (count,rows,elapsed time,execution time), but most I couldn't. Can anybody help me out with the significance of those parameters? Below are the results.
更新:正如 Rob Van 所建议的,我使用了 tkprof 实用程序来查找查询的性能。一些我能理解的参数(计数、行数、经过时间、执行时间),但大多数我不能。任何人都可以帮助我了解这些参数的重要性吗?以下是结果。
Query 1:
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.01 0.01 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 49 0.26 0.32 45 494 0 23959
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 51 0.28 0.33 45 494 0 23959
Query2:
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 33 0.25 0.24 0 904 0 15992
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 35 0.25 0.24 0 904 0 15992
I can decide that query 2 is better than query 1. Any help on what disk, query and current params mean??
我可以决定查询 2 比查询 1 更好。有关磁盘、查询和当前参数的含义有什么帮助吗?
回答by Jay
There is button above the SQL editor called "Explain Plan". This tool will tell you what each route costs, and how the statement will use indexes and partitions. Note, you may get an error, your DBA will need to turn on a feature for your user account, I believe it is "trace", but could be wrong on that point. Reading the execute statement output can be challenging at first, but it is a good tool to helping write good SQL.
SQL 编辑器上方有一个名为“解释计划”的按钮。这个工具会告诉你每条路由的成本,以及语句将如何使用索引和分区。请注意,您可能会收到错误消息,您的 DBA 将需要为您的用户帐户启用一项功能,我相信这是“跟踪”,但在这一点上可能是错误的。一开始读取 execute 语句输出可能具有挑战性,但它是帮助编写好的 SQL 的好工具。
回答by Rob van Wijk
On OTN, Randolf Geist and me have written two posts about how to measure performance. If you do as indicated in those threads, you will have gathered information to be able to choose the better one.
在 OTN 上,Randolf Geist 和我写了两篇关于如何衡量性能的文章。如果您按照这些线程中的指示进行操作,您将收集信息以便能够选择更好的一个。
If you want the full version, visit Randolf's.
如果您想要完整版,请访问Randolf 的.
If you want a short version, visit mine:
如果你想要一个简短的版本,请访问我的:
Both threads explain how to use explain plan and tracing to see where time is spent. Then it is up to you to decide what you exactly qualify as "better". This can be the shortest elapsed time, the least amount of used resources, or the smallest number of latches, or maybe something else.
两个线程都解释了如何使用解释计划和跟踪来查看时间花在哪里。然后由您来决定什么才是“更好”的。这可以是最短的运行时间、最少的资源使用量、最少的锁存器数量,或者其他一些东西。
Hope this helps.
希望这可以帮助。
Regards, Rob.
问候,罗布。
回答by Chris Gill
I'm afraid EXPLAIN PLAN is the only way. Start off by assuming that a lower cost (if you look at the explain plan there should be a column called COST) is better, but you need to read up about it so that you learn more and more. Maybe you have a DBA there that you could chat stuff through with? Without having your data and the queries it is difficult to advise further
恐怕 EXPLAIN PLAN 是唯一的方法。首先假设较低的成本(如果您查看解释计划,应该有一个名为 COST 的列)更好,但是您需要阅读它,以便您了解更多信息。也许你在那里有一个 DBA,你可以与之聊天?如果没有您的数据和查询,就很难提供进一步的建议
For anything to do with developing Oracle, a good place to start is anything by Tom Kyte (Google him) or search through the Ask Tom website. If you really want to get involved
对于任何与开发 Oracle 相关的事情,一个好的起点是 Tom Kyte(谷歌他)或通过Ask Tom 网站搜索。如果你真的想参与
Only running the query a few times is a pretty bad idea - equivalent to just accepting that the cost of the explain plan tells you the best query. You really need to take into account what resources your query is taking up and therefore how it could affect you production system.
只运行几次查询是一个非常糟糕的主意 - 相当于只是接受解释计划的成本会告诉您最好的查询。您确实需要考虑您的查询占用了哪些资源,以及它如何影响您的生产系统。
Depending on how often the query gets called affects how well you need to track a query's performance (sorry to purists, but it does). If the query only runs once a week and takes a minute to run without affecting anything elsethen do you need to optimise that query? Is it easier for maintenance to use a query that is more easy to logically follow?
根据查询被调用的频率影响您需要跟踪查询性能的程度(抱歉纯粹主义者,但确实如此)。如果查询每周只运行一次并且运行需要一分钟而不影响其他任何东西,那么您是否需要优化该查询?使用逻辑上更容易遵循的查询是否更容易维护?
If the query is being called multiple times a second then you need to completely understand the explain plan, and have a further understanding on how you can optimise the query to its best performance
如果查询在一秒钟内被多次调用,那么您需要完全理解解释计划,并进一步了解如何优化查询以使其最佳性能
回答by jva
Basic answer - execute each query a couple of times and see which one is faster. Best part - you can do this without learning about Oracle performance. The only thing you need to know is that you can not trust the first try because most of the time it will read data from disk while second try will use cached data from RAM. That is why you try each query a couple of times.
基本答案 - 执行每个查询几次,看看哪个更快。最好的部分 - 您可以在不了解 Oracle 性能的情况下执行此操作。您唯一需要知道的是,您不能相信第一次尝试,因为大多数情况下它会从磁盘读取数据,而第二次尝试将使用来自 RAM 的缓存数据。这就是为什么您要多次尝试每个查询。