更新行时出现奇怪的行为。我得到了父表'统计数据'和大约100个子分区表。我正在执行请求并执行150ms但是当我执行EXPLAIN ANALYZE时,我看到0.115ms。
这是我执行的代码:
UPDATE stats
SET
actions=(stats.actions + 0), offer_clicks=(stats.offer_clicks + 0), uniq=(stats.uniq + 0), raw=(stats.raw + 1),
tb_uniq=(stats.tb_uniq + 0), tb_raw=(stats.tb_raw + 0), leads=(stats.leads + 0), subs=(stats.subs + 0), unsubs=(stats.unsubs + 0),
rebills=(stats.rebills + 0), sales=(stats.sales + 0), holds=(stats.holds + 0), rejects=(stats.rejects + 0), trashes=(stats.trashes + 0),
user_lead_income=(stats.user_lead_income + 0.0), user_subs_income=(stats.user_subs_income + 0.0),
user_rebill_income=(stats.user_rebill_income + 0.0), user_sales_income=(stats.user_sales_income + 0.0),
user_hold_income=(stats.user_hold_income + 0.0), system_lead_income=(stats.system_lead_income + 0.0),
system_subs_income=(stats.system_subs_income + 0.0), system_rebill_income=(stats.system_rebill_income + 0.0),
system_sales_income=(stats.system_sales_income + 0.0), system_hold_income=(stats.system_hold_income + 0.0)
WHERE
stats.ts_spawn = 1426960800 AND stats.user_id = 1 AND stats.offer_targeting_id = 132 AND stats.offer_id = 123 AND
stats.traff_type = 4 AND stats.operator_id = 0 AND stats.country_id = 113 AND
stats.subacc_1 = '' AND stats.subacc_2 = '' AND stats.subacc_3 = '' AND stats.subacc_4 = '' AND stats.utm_source = '' AND
stats.utm_medium = '' AND stats.utm_term = '' AND stats.utm_content = '' AND stats.utm_campaign = '' AND
stats.prelanding_id = 203 AND stats.landing_id = 0 AND stats.platform_id = 6 AND stats.platform_out_id = 76 AND
stats.os_id = 9 AND stats.browser_id = 34
解释分析:
"Update on stats (cost=0.00..7.49 rows=2 width=416) (actual time=0.024..0.024 rows=0 loops=1)"
" -> Seq Scan on stats (cost=0.00..0.07 rows=1 width=670) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((ts_spawn = 1426960800) AND (user_id = 1) AND (offer_targeting_id = 132) AND (offer_id = 123) AND (traff_type = 4) AND (operator_id = 0) AND (landing_id = 0) AND (country_id = 113) AND ((subacc_1)::text = ''::text) AND ((subacc_2)::text = ''::text) AND ((subacc_3)::text = ''::text) AND ((subacc_4)::text = ''::text) AND ((utm_source)::text = ''::text) AND ((utm_medium)::text = ''::text) AND ((utm_term)::text = ''::text) AND ((utm_content)::text = ''::text) AND ((utm_campaign)::text = ''::text) AND (prelanding_id = 203) AND (platform_id = 6) AND (platform_out_id = 76) AND (os_id = 9) AND (browser_id = 34))"
" -> Index Scan using stats_y2015_m3_u1_pk on stats_y2015_m3_u1 (cost=0.28..7.42 rows=1 width=161) (actual time=0.020..0.020 rows=0 loops=1)"
" Index Cond: ((ts_spawn = 1426960800) AND (user_id = 1) AND (offer_targeting_id = 132) AND (offer_id = 123) AND (traff_type = 4) AND (operator_id = 0) AND (country_id = 113) AND ((subacc_1)::text = ''::text) AND ((subacc_2)::text = ''::text) AND ((subacc_3)::text = ''::text) AND ((subacc_4)::text = ''::text) AND ((utm_source)::text = ''::text) AND ((utm_medium)::text = ''::text) AND ((utm_term)::text = ''::text) AND ((utm_content)::text = ''::text) AND ((utm_campaign)::text = ''::text) AND (prelanding_id = 203) AND (landing_id = 0) AND (platform_id = 6) AND (platform_out_id = 76) AND (os_id = 9) AND (browser_id = 34))"
"Planning time: 143.288 ms"
"Execution time: 0.373 ms"
并从日志查询:
2015-03-26 10:16:04 UTC, LOG: duration: 150.878 ms statement:
UPDATE stats SET actions=(stats.actions + 0),
offer_clicks=(stats.offer_clicks + 0), uniq=(stats.uniq + 1), raw=(stats.raw + 0), tb_uniq=(stats.tb_uniq +0),
tb_raw=(stats.tb_raw + 0), leads=(stats.leads + 0), subs=(stats.subs + 0), unsubs=(stats.unsubs + 0),
rebills=(stats.rebills + 0), sales=(stats.sales + 0),
holds=(stats.holds + 0), rejects=(stats.rejects + 0), trashes=(stats.trashes + 0), user_lead_income=(stats.user_lead_income + 0.0),
user_subs_income=(stats.user_subs_income + 0.0), user_rebill_income=(stats.user_rebill_income + 0.0),
user_sales_income=(stats.user_sales_income + 0.0), user_hold_income=(stats.user_hold_income + 0.0),
system_lead_income=(stats.system_lead_income + 0.0), system_subs_income=(stats.system_subs_income + 0.0),
system_rebill_income=(stats.system_rebill_income + 0.0), system_sales_income=(stats.system_sales_income + 0.0),
system_hold_income=(stats.system_hold_income + 0.0)
WHERE
stats.ts_spawn = 1427364000 AND stats.user_id = 1 AND stats.offer_targeting_id = 124 AND stats.offer_id = 135 AND stats.traffic_manager_id = 0 AND
stats.traff_type = 2 AND stats.operator_id = 0 AND stats.country_id = 192 AND
stats.subacc_1 = '' AND stats.subacc_2 = '' AND stats.subacc_3 = '' AND
stats.subacc_4 = '' AND stats.utm_source = '' AND stats.utm_medium = '' AND stats.utm_term = '' AND
stats.utm_content = '' AND stats.utm_campaign = '' AND stats.prelanding_id = 223 AND stats.landing_id = 345 AND
stats.platform_id = 10 AND stats.platform_out_id = 15 AND stats.os_id = 5 AND stats.browser_id = 34
P.S。所有查询都是一样的。使用PostgreSQL 9.4。