我很难理解以下查询计划(匿名(。查询的实际部分所花费的时间和缺少的部分之间似乎存在差距。
相关部分:
-> Sort (cost=306952.44..307710.96 rows=303409 width=40) (actual time=4222.122..4400.534 rows=582081 loops=1)
Sort Key: table1.column3, table1.column2
Sort Method: quicksort Memory: 71708kB
Buffers: shared hit=38058
-> Index Scan using myindex on table1 (cost=0.56..279325.68 rows=303409 width=40) (actual time=0.056..339.565 rows=582081 loops=1)
Index Cond: (((column1)::text = 'xxx1'::text) AND ((column2)::text > 'xxx2'::text) AND ((column2)::text < 'xxx3'::text))
Buffers: shared hit=38058
索引扫描(直到339.565
(和开始对结果进行排序(4222.122
(之间发生了什么?
完整的计划(以防万一(:
GroupAggregate (cost=344290.95..348368.01 rows=30341 width=65) (actual time=4933.739..4933.806 rows=11 loops=1)
Group Key: t.column1, t.current_value, t.previous_value
Buffers: shared hit=38058
-> Sort (cost=344290.95..345045.68 rows=301892 width=72) (actual time=4933.712..4933.719 rows=58 loops=1)
Sort Key: t.column1, t.current_value, t.previous_value
Sort Method: quicksort Memory: 32kB
Buffers: shared hit=38058
-> Subquery Scan on t (cost=306952.44..316813.23 rows=301892 width=72) (actual time=4573.523..4933.607 rows=58 loops=1)
Filter: ((t.current_value)::text <> (t.previous_value)::text)
Rows Removed by Filter: 582023
Buffers: shared hit=38058
-> WindowAgg (cost=306952.44..313020.62 rows=303409 width=72) (actual time=4222.144..4859.579 rows=582081 loops=1)
Buffers: shared hit=38058
-> Sort (cost=306952.44..307710.96 rows=303409 width=40) (actual time=4222.122..4400.534 rows=582081 loops=1)
Sort Key: table1.column3, table1.column2
Sort Method: quicksort Memory: 71708kB
Buffers: shared hit=38058
-> Index Scan using myindex on table1 (cost=0.56..279325.68 rows=303409 width=40) (actual time=0.056..339.565 rows=582081 loops=1)
Index Cond: (((column1)::text = 'xxx1'::text) AND ((column2)::text > 'xxx2'::text) AND ((column2)::text < 'xxx3'::text))
Buffers: shared hit=38058
Planning Time: 0.405 ms
Execution Time: 4941.003 ms
cost
和actual time
数据在这些计划中都显示为两个数字。第一个是设置时间。粗略地描述,现在是查询步骤传递第一行的时间。看看这个。第二个是完成时间,或到最后一行的时间。
actual time=setup...completion
排序步骤上的设置包括检索需要排序的结果集和实际执行排序所需的时间:在RAM中来回移动行,或者希望不是在磁盘上。(因为快速排序的复杂性通常为O(n-log(n((,所以对于大的结果集来说,这可能很长。你早就知道。(
在您的计划中,内部排序处理来自索引扫描的近600K行。这是一个需要时间的步骤。它使用了71708千字节的RAM。
据我所知,你的计划没有什么异常。如何加快这种速度?您可以尝试更短或固定长度的column2
和column3
数据类型,但这都是猜测,而看不到查询和表定义。