我们正在使用PostgreSQL 8.2,当我们在多个线程中运行查询时,我们会看到一种非常奇怪的行为。
我们使用5个同步线程运行相同的查询,持续1小时。 当我们这样做时,我们看到执行查询的时间在执行之间有很大差异。
我们检查了执行计划,打破了查询,但我们仍然没有看到任何可能指向任何正确方向的内容。然而,当我们使用PostgreSQL 9.4进行相同的测试时,我们没有看到如此剧烈的偏差。
以下是我们的测试台
CentOS6,8GB 8vCPU云实例 以下是postgreSQL设置
max_connections = 150
shared_buffers = 2GB
temp_buffers = 16MB
max_prepared_transactions = 300
work_mem = 64MB
maintenance_work_mem = 512MB
effective_cache_size = 4GB
以下是查询
SELECT rateplanid,STAYDATE,MIN(PRIORITY||':'||SELLRATE||'@'||COALESCE(STDNETRATE,0)||'@0@'||COALESCE(HIGHERRATE,'N')||'@0@0@'||COALESCE(RATECODE,'-')||'@'||COALESCE(PROFITMARKUP,0)||'@'||COALESCE(BASENETRATE,0))
FROM ( SELECT rateplanid,STAYDATE, ( CASE WHEN (customerid=2791) THEN 1 WHEN (regionid=4 AND customerid=0) THEN 2 WHEN customerid=1 THEN 3 END ) AS PRIORITY, round(cast(SELLRATE as numeric),0) as SELLRATE, round(cast(COALESCE(STDNETRATE,0) as numeric),2)
AS STDNETRATE, HIGHERRATE AS HIGHERRATE, COALESCE('-') AS RATECODE, round(cast(profitmarkup as numeric),2) as PROFITMARKUP, COALESCE(FIXEDSELLRATE,0) as
BASENETRATE
FROM xxx.rate_2016m1_a2
WHERE staydate>='2016-02-20' AND staydate<='2016-02-20'
AND rateplanid in (4031,4032,4019,4020,4024,4025,4026,4027,24027,24028,24029,24033,24037,24030,24034,24038,4014,24032,
24031,4015,8131,8132,86663,86664,64855,64856,86665,86666,7916,7917,7918,25721,25722,48689,48690,48691,65456,39774,39166,
65457,65458,39792,5954,5953,6899,8333,6898,51250,88150,6366,75464,88151,6367,75465,88159,5159,75468,88160,5161,75469,88169,
5168,75473,88170,5169,75474,88152,8107,75466,88153,8108,75467,88174,8114,75483,88175,8115,75484,55086,55091,55090,55096,55095,
10770,10774,10775,10771,10772,10773,99066,732,764,99067,4393,4394,5227,5228,10397,10398,3798,3797,35043,10223,10224,4167,
4168,8537,8538,8541,8542,102361,102362,3401,3402,4493,4492,4337,4338,11352,11353)
AND ( customerid=2791 OR (regionid=4 AND customerid=0) OR customerid=1 )
AND coalesce(STDNETRATE,15)>15 AND coalesce(SELLRATE,20)>20
) AS X
GROUP BY rateplanid,STAYDATE ORDER BY rateplanid,STAYDATE
以下是执行计划
"GroupAggregate (cost=8.53..8.62 rows=1 width=53) (actual time=5303.500..5492.684 rows=27828 loops=1)"
" -> Sort (cost=8.53..8.53 rows=1 width=53) (actual time=5303.430..5317.002 rows=37531 loops=1)"
" Sort Key: rate_2016m1_a2.rateplanid, rate_2016m1_a2.staydate"
" -> Index Scan using idx_rate_2016m1_a2_staydate on rate_2016m1_a2 (cost=0.00..8.52 rows=1 width=53) (actual time=53.857..5142.824 rows=37531 loops=1)"
" Index Cond: ((staydate >= '2016-02-20'::date) AND (staydate <= '2016-02-20'::date))"
"Total runtime: 5503.289 ms"
以下是日志输出
XML - Date: 2016-02-02 rate query execution time : 2211
XML - Date: 2016-02-12 rate query execution time : 2232
XML - Date: 2016-02-10 rate query execution time : 2228
XML - Date: 2016-02-08 rate query execution time : 2224
XML - Date: 2016-02-04 rate query execution time : 2218
XML - Date: 2016-02-06 rate query execution time : 2208
XML - Date: 2016-02-07 rate query execution time : 5
XML - Date: 2016-02-13 rate query execution time : 13
XML - Date: 2016-02-05 rate query execution time : 5
XML - Date: 2016-02-09 rate query execution time : 4
XML - Date: 2016-02-11 rate query execution time : 5
XML - Date: 2016-02-03 rate query execution time : 4
XML - Date: 2016-01-25 rate query execution time : 27960
XML - Date: 2016-01-27 rate query execution time : 29843
XML - Date: 2016-01-29 rate query execution time : 33172
XML - Date: 2016-01-31 rate query execution time : 34075
XML - Date: 2016-02-01 rate query execution time : 705
XML - Date: 2016-01-26 rate query execution time : 10491
XML - Date: 2016-01-30 rate query execution time : 8584
XML - Date: 2016-01-28 rate query execution time : 12473
然而,当我们使用PostgreSQL 9.4
运行时"GroupAggregate (cost=0.58..1458.11 rows=2 width=50) (actual time=0.169..134.378 rows=98 loops=1)"
" Group Key: rate_2016m1_a2.rateplanid, rate_2016m1_a2.staydate"
" -> Index Scan using uk_key_rate_2016m1_a2 on rate_2016m1_a2 (cost=0.58..1456.43 rows=19 width=50) (actual time=0.065..133.312 rows=98 loops=1)"
" Index Cond: ((rateplanid = ANY ('{4031,4032,4019,4020,4024,4025,4026,4027,24027,24028,24029,24033,24037,24030,24034,24038,4014,24032,24031,4015,8131,8132,86663,86664,64855,64856,86665,86666,7916,7917,7918,25721,25722,48689,48690,48691,65456,39774,3 (...)"
" Filter: ((COALESCE(stdnetrate, 15::double precision) > 15::double precision) AND (COALESCE(sellrate, 20::double precision) > 20::double precision) AND ((customerid = 2791) OR ((regionid = 4) AND (customerid = 0)) OR (customerid = 1)))"
" Rows Removed by Filter: 4"
"Planning time: 1.037 ms"
"Execution time: 134.501 ms"
记录时间
XML - Date: 2016-02-06 rate query execution time : 1627
XML - Date: 2016-02-04 rate query execution time : 1622
XML - Date: 2016-02-08 rate query execution time : 1638
XML - Date: 2016-02-10 rate query execution time : 1645
XML - Date: 2016-02-12 rate query execution time : 1659
XML - Date: 2016-02-07 rate query execution time : 90
XML - Date: 2016-02-11 rate query execution time : 177
XML - Date: 2016-02-09 rate query execution time : 189
XML - Date: 2016-02-05 rate query execution time : 209
XML - Date: 2016-02-13 rate query execution time : 268
XML - Date: 2016-02-02 rate query execution time : 1967
XML - Date: 2016-02-03 rate query execution time : 40
XML - Date: 2016-01-29 rate query execution time : 3091
XML - Date: 2016-01-25 rate query execution time : 3195
XML - Date: 2016-01-31 rate query execution time : 3148
XML - Date: 2016-01-30 rate query execution time : 51
XML - Date: 2016-01-27 rate query execution time : 3147
XML - Date: 2016-01-30 rate query execution time : 51
XML - Date: 2016-01-27 rate query execution time : 3147
现在我们陷入困境,我们将非常感谢能够帮助您解决这个问题。