查询带分布键条件 本页介绍天翼云TeleDB数据库查询带分布键条件的最佳实践。 通过explain查看执行计划,查看SQL语句是否使用到分布键,DN下发DN情况可通过Node/s: 关键字来查看。 如果使用到了分布键,那么SQL只会下发到分布键对应的某个DN节点;如果没有用到分布键,那么SQL会下发到所有DN节点;没有带分布键的SQL,因为下发到了所有DN节点,消耗了更多的连接资源、计算资源,应尽量避免。 某个表的SQL语句可能有不同的where条件,在SQL优化时,应尽量确保高频并发的SQL语句是带了分布键条件。 例如,teledb1表的分布键f2,下面的SQL where条件为f11,没有带分布键条件,那么SQL将下发到所有DN节点执行。 teledb explain select from teledb1 where f11; QUERY PLAN Remote Fast Query Execution (cost0.00..0.00 rows0 width0) Node/s: dn001, dn002 > Gather (cost1000.00..7827.20 rows1 width14) Workers Planned: 2 > Parallel Seq Scan on teledb1 (cost0.00..6827.10 rows1 width14) Filter: (f1 1) (6 rows) 下面的SQL,where条件为f21,带了分布键条件,SQL只下发到了f21所在的DN节点dn001。 teledb explain select from teledb1 where f21; QUERY PLAN Remote Fast Query Execution (cost0.00..0.00 rows0 width0) Node/s: dn001 > Gather (cost1000.00..7827.20 rows1 width14) Workers Planned: 2 > Parallel Seq Scan on teledb1 (cost0.00..6827.10 rows1 width14) Filter: (f2 1) (6 rows)