EXPLAIN ANALYZE QUERY 如何用于测量查询性能?
How is EXPLAIN ANALYZE QUERY useful for measuring query performance?
今天我尝试使用 explain analyze 来提取查询的执行时间。我希望在不受查询延迟影响的情况下正确计算它们的执行时间,但是在 documentation 中它指出存在分析开销:
In order to measure the run-time cost of each node in the execution plan, the current implementation of EXPLAIN ANALYZE adds profiling overhead to query execution. As a result, running EXPLAIN ANALYZE on a query can sometimes take significantly longer than executing the query normally. The amount of overhead depends on the nature of the query, as well as the platform being used. The worst case occurs for plan nodes that in themselves require very little time per execution, and on machines that have relatively slow operating system calls for obtaining the time of day.
因此,我质疑 explain analyze 的有用性,因为您似乎只是用分析开销来交换延迟。更令人惊讶的是,在代码中对我的数据库查询进行计时(Python 在我使用 psycopg2 库的情况下)导致查询执行时间更短。
EXPLAIN (ANALYZE)
很有用,不是因为它测量执行时间的准确性,而是因为它告诉您大部分执行时间花在了哪里以及 PostgreSQL 的估计值在哪里。
EXPLAIN (ANALYZE)
是一种优化工具,绝不是优化器。这意味着它增加了实际的开销,永远不应在生产应用程序中使用,而只能在调整时使用。
它是数据库开发人员或管理员准确了解数据库引擎将如何执行特定查询以及大部分时间花在何处的宝贵工具。例如,它可以帮助确定添加或删除索引的影响,以及对查询时间和访问的详细影响。
因为一旦使用数据库,就不能依赖简单的时间测量,因为它们可以取决于数据库是否加载了其他查询,某些页面是否已经缓存在内存中,更不用说外部了在客户端或服务器机器上加载。
今天我尝试使用 explain analyze 来提取查询的执行时间。我希望在不受查询延迟影响的情况下正确计算它们的执行时间,但是在 documentation 中它指出存在分析开销:
In order to measure the run-time cost of each node in the execution plan, the current implementation of EXPLAIN ANALYZE adds profiling overhead to query execution. As a result, running EXPLAIN ANALYZE on a query can sometimes take significantly longer than executing the query normally. The amount of overhead depends on the nature of the query, as well as the platform being used. The worst case occurs for plan nodes that in themselves require very little time per execution, and on machines that have relatively slow operating system calls for obtaining the time of day.
因此,我质疑 explain analyze 的有用性,因为您似乎只是用分析开销来交换延迟。更令人惊讶的是,在代码中对我的数据库查询进行计时(Python 在我使用 psycopg2 库的情况下)导致查询执行时间更短。
EXPLAIN (ANALYZE)
很有用,不是因为它测量执行时间的准确性,而是因为它告诉您大部分执行时间花在了哪里以及 PostgreSQL 的估计值在哪里。
EXPLAIN (ANALYZE)
是一种优化工具,绝不是优化器。这意味着它增加了实际的开销,永远不应在生产应用程序中使用,而只能在调整时使用。
它是数据库开发人员或管理员准确了解数据库引擎将如何执行特定查询以及大部分时间花在何处的宝贵工具。例如,它可以帮助确定添加或删除索引的影响,以及对查询时间和访问的详细影响。
因为一旦使用数据库,就不能依赖简单的时间测量,因为它们可以取决于数据库是否加载了其他查询,某些页面是否已经缓存在内存中,更不用说外部了在客户端或服务器机器上加载。