postgresql查询无法完成
发布时间:2020-12-13 16:02:34 所属栏目:百科 来源:网络整理
导读:在postgresl 9.0上我们有一个SQL查询: SELECT count(*) FROM lane WHERE not exists (SELECT 1 FROM id_map WHERE id_map.new_id=lane.lane_id and id_map.column_name='lane_id' and id_map.table_name='lane') and lane.lane_id is not null; 这通常需要
在postgresl 9.0上我们有一个SQL查询:
SELECT count(*) FROM lane WHERE not exists (SELECT 1 FROM id_map WHERE id_map.new_id=lane.lane_id and id_map.column_name='lane_id' and id_map.table_name='lane') and lane.lane_id is not null; 这通常需要大约1.5秒才能完成. 有时虽然此查询挂起但无法完成.它甚至可以运行11个小时但没有成功. 此查询所采用的唯一锁定是“AccessShareLock”,它们都被授予. SELECT a.datname,c.relname,l.transactionid,l.mode,l.granted,a.usename,a.current_query,a.query_start,age(now(),a.query_start) AS "age",a.procpid FROM pg_stat_activity a JOIN pg_locks l ON l.pid = a.procpid JOIN pg_class c ON c.oid = l.relation ORDER BY a.query_start; 该查询作为使用连接池连接到数据库的java进程的一部分运行,并执行此格式的顺序类似的选择查询: SELECT count(*) FROM {} WHERE not exists (SELECT 1 FROM id_map WHERE id_map.new_id={}.{} and id_map.column_name='{}' and id_map.table_name='{}') and {}.{} is not null 没有更新或删除与此过程并行发生,所以我不认为吸尘可能是这里的问题. postgres日志不显示长时间运行的查询的任何条目,因为它们永远不会完成,因此永远不会被记录. 知道什么可能导致这种行为以及如何防止它发生? 没有分析的解释计划: Aggregate (cost=874337.91..874337.92 rows=1 width=0) -> Nested Loop Anti Join (cost=0.00..870424.70 rows=1565283 width=0) Join Filter: (id_map.new_id = lane.lane_id) -> Seq Scan on lane (cost=0.00..30281.84 rows=1565284 width=8) Filter: (lane_id IS NOT NULL) -> Materialize (cost=0.00..816663.60 rows=1 width=8) -> Seq Scan on id_map (cost=0.00..816663.60 rows=1 width=8) Filter: (((column_name)::text = 'lane_id'::text) AND ((table_name)::text = 'lane'::text)) 解决方法VACUUM ANALYZE VERBOSE; 刷新统计数据应该有助于db选择最佳计划 – 而不是嵌套循环,我认为这需要100%的CPU (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |