2018.11.30
前言
某工程進行Vertica表間數據拼接,大致SQL語句如下:
insert into tableForInsert as select * from tableA union all select * from tableB ...
union all
1的表非常多,導致SQL語句過長,報錯如下:
java.sql.SQLNonTransientException: [Vertica][VJDBC](4963) ERROR: The query contains a SET operation tree that is too complex to analyze
追根溯源
從Vertica的官方文檔2得知,Vertica對於SQL語句有兩條限制:
- The first limit is based on the stack available to the expression. Vertica requires at least 100kb of free stack:第一條是關於SQL語句的大小,要求小於棧大小。這取決於Vertica的配置;
- The second limit is the number of recursions possible in an analytic expression. The limit is 2000…cannot be increased:第二條是語句可能的遞歸次數不得超過2000,而且2000是上限,不可增加。
並且根據Vertica官方某博客3可推斷,union all
是一條遞歸語句:
Other implementations include ANSI SQL’s recursive SQL syntax using WITH and UNION ALL, special graph based algorithms and enumerated path technique
恍然大悟
綜上,該語句報錯的原因就是SQL語句的union all
太多而導致超過2000的上限。最佳解決方案是修改SQL語句,棄用union all
進行拼接,根本上避免了會超過上限的可能性;次之的方法,可以考慮將SQL語句縮短。