sql自学笔记(十八)——MySQL8.0版本的新特性(八)

2019-05-06  本文已影响0人  itczt

递归限制

递归表达式的查询中需要包含一个终止递归的调减

cte - max - recursion - depth
max - execytion -time

这两个参数对递归CTE的影响

先写一个死循环的递归调用

with recursive cte(n) as
(
   select 1
   union all
   select n+1 from cte
)
select * from cte;
![](https://img.haomeiwen.com/i16612509/7a6f18f91e8c1e5c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

系统会执行一定次数的次数的递归循环之后出现错误默认是1000次
可以先看一下这个默认值

set variables like cte - max%;  

我们可以修改这个默认值

set session cte - max - recursion - depth = 10;

在执行一次,可以看到第11次出错,我们可以根据自己的需要来调整这个默认值。

show variables like 'max - execution%';

由图可知,他是以毫秒为单位来限制的,0表示没有限制,
我们再来设置一下默认值

set variables like max - execution-time=1000;

我们可以看到它没有这最大次数,但是到达了最大时间。
由此可知我们有两种方式来限制递归,一种是次数cte - max - recursion,二是时间max - execption -time ,这两种都可以防止递归陷入死循环。

CTE小结

上一篇下一篇

猜你喜欢

热点阅读