测试版本:--03134283938-20221019-172201-20018
达梦的排序机制由四个dm.ini参数控制:
SORT_BUF_SIZE = 100 #maximum sort buffer size in MegabytesSORT_BLK_SIZE = 1 #maximum sort blk size in MegabytesSORT_BUF_GLOBAL_SIZE = 1000 #maximum global sort buffer size in MegabytesSORT_FLAG = 0 #choose method of sort
SORT_FLAG,0代表传统排序机制,1代表新排序机制。
传统排序机制时,系统均为每一个进行排序操作的会话分配SORT_BUF_SIZE大小的本地排序区(RT_MEMOBJ_VPOOL)。如果排序的尺寸大于SORT_BUF_SIZE,则使用临时表空间协助完成排序,这时可以通过v$mtab_used_history查询。
-
下面我们做一个实验
会话1:
提前准备包含30000000行数据的d表,读取d表数据排序后插入e表。
insert into e select * from d order by 2;
会话2:
查询内存池的使用情况
#查询内存池的使用情况
SQL> select a.name pool,total_size/1024/1024.0 Mbytes,FILE_NAME
from v$mem_pool a
where a.name in ('VDTA POOL','RT_MEMOBJ_VPOOL')
order by 2 desc limit 2#会话1执行中的查询结果:
LINEID POOL MBYTES FILE_NAME
---------- --------------- ------ ------------------------------------------------
1 RT_MEMOBJ_VPOOL 115 /home/dmops/build/svns/1666183702277/op/xsort3.c
2 RT_MEMOBJ_VPOOL 10 /home/dmops/build/svns/1666183702277/op/xsort3.c#查询临时表空间的使用情况
SQL> select MTAB_TYPE,MTAB_USED_BY_M,SQL_TEXT from v$mtab_used_history;LINEID MTAB_TYPE MTAB_USED_BY_M SQL_TEXT
---------- --------- -------------- -----------------------------------------
1 FLUSH 24 insert into e select * from d order by 2;
2 NSORT 289 insert into e select * from d order by 2;
下面是dmserver在OS中的内存占用
-
调整参数不变,5个会话同时执行
查询会话:
#查询内存池的使用情况
SQL> select a.name pool,total_size/1024/1024.0 Mbytes,FILE_NAME
from v$mem_pool a
where a.name in ('VDTA POOL','RT_MEMOBJ_VPOOL')
order by 2 desc limit 5LINEID POOL MBYTES FILE_NAME
---------- --------------- ------ ------------------------------------------------
1 RT_MEMOBJ_VPOOL 115 /home/dmops/build/svns/1666183702277/op/xsort3.c
2 RT_MEMOBJ_VPOOL 115 /home/dmops/build/svns/1666183702277/op/xsort3.c
3 RT_MEMOBJ_VPOOL 115 /home/dmops/build/svns/1666183702277/op/xsort3.c
4 RT_MEMOBJ_VPOOL 115 /home/dmops/build/svns/1666183702277/op/xsort3.c
5 RT_MEMOBJ_VPOOL 115 /home/dmops/build/svns/1666183702277/op/xsort3.c#查询临时表空间的使用情况
SQL> select MTAB_TYPE,MTAB_USED_BY_M,SQL_TEXT from v$mtab_used_history;LINEID MTAB_TYPE MTAB_USED_BY_M SQL_TEXT
---------- --------- -------------- -----------------------------------------
1 FLUSH 24 insert into e select * from d order by 2;
2 FLUSH 24 insert into e select * from d order by 2;
3 FLUSH 24 insert into e select * from d order by 2;
4 FLUSH 24 insert into e select * from d order by 2;
5 FLUSH 24 insert into e select * from d order by 2;
6 NSORT 289 insert into e select * from d order by 2;
7 NSORT 289 insert into e select * from d order by 2;
8 NSORT 289 insert into e select * from d order by 2;
9 NSORT 289 insert into e select * from d order by 2;
10 NSORT 289 insert into e select * from d order by 2;
下面是dmserver在OS中的内存占用
-
调整参数SORT_BUF_SIZE=512
5个会话同时执行
查询会话:
#查询内存池的使用情况
SQL> select a.name pool,total_size/1024/1024.0 Mbytes,FILE_NAME
from v$mem_pool a
where a.name in ('VDTA POOL','RT_MEMOBJ_VPOOL')
order by 2 desc limit 5#会话1执行中的查询结果:
select MTAB_TYPE,MTAB_USED_BY_M,SQL_TEXT from v$mtab_used_history;
LINEID POOL MBYTES FILE_NAME
---------- --------------- ------------- ------------------------------------------------
1 RT_MEMOBJ_VPOOL 714.509765625 /home/dmops/build/svns/1666183702277/op/xsort3.c
2 RT_MEMOBJ_VPOOL 712.509765625 /home/dmops/build/svns/1666183702277/op/xsort3.c
3 RT_MEMOBJ_VPOOL 712.509765625 /home/dmops/build/svns/1666183702277/op/xsort3.c
4 RT_MEMOBJ_VPOOL 712.509765625 /home/dmops/build/svns/1666183702277/op/xsort3.c
5 RT_MEMOBJ_VPOOL 712.509765625 /home/dmops/build/svns/1666183702277/op/xsort3.c#查询临时表空间的使用情况
SQL> select MTAB_TYPE,MTAB_USED_BY_M,SQL_TEXT from v$mtab_used_history;
no rows
由上可见,当排序区足够时将不启用临时表空间排序。
下面是dmserver在OS中的内存占用
-
调整参数SORT_BUF_SIZE=1024
5个会话同时执行
查询会话:
#查询内存池的使用情况
SQL> select a.name pool,total_size/1024/1024.0 Mbytes,FILE_NAME
from v$mem_pool a
where a.name in ('VDTA POOL','RT_MEMOBJ_VPOOL')
order by 2 desc limit 5#会话1执行中的查询结果:
LINEID POOL MBYTES FILE_NAME
---------- --------------- -------------- ------------------------------------------------
1 RT_MEMOBJ_VPOOL 1121.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
2 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
3 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
4 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
5 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c#查询临时表空间的使用情况
SQL> select MTAB_TYPE,MTAB_USED_BY_M,SQL_TEXT from v$mtab_used_history;
no rows
下面是dmserver在OS中的内存占用
从本次测试中可见,当增加参数值后,每个会话占用的内存增加,dmserver占用的总内存也相应增加。
-
调整参数SORT_BUF_SIZE=2048
5个会话同时执行
查询会话:
#查询内存池的使用情况
SQL> select a.name pool,total_size/1024/1024.0 Mbytes,FILE_NAME
from v$mem_pool a
where a.name in ('VDTA POOL','RT_MEMOBJ_VPOOL')
order by 2 desc limit 5#会话1执行中的查询结果:
LINEID POOL MBYTES FILE_NAME
---------- --------------- -------------- ------------------------------------------------
1 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
2 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
3 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
4 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c
5 RT_MEMOBJ_VPOOL 1117.841796875 /home/dmops/build/svns/1666183702277/op/xsort3.c#查询临时表空间的使用情况
SQL> select MTAB_TYPE,MTAB_USED_BY_M,SQL_TEXT from v$mtab_used_history;
no rows
下面是dmserver在OS中的内存占用
从本次测试中可见,参数再增加,占用的内存不再同步增长。
结论
SORT_BUF_SIZE参数是系统为每一个会话分配的排序内存上限。参数设置过大时可能会造成一些内存浪费,但并不总跟随参数上涨,系统试图优化内存的使用。当会话实际需要超过SORT_BUF_SIZE时,系统使用临时表空间协助完成排序工作,可以通过v$mtab_used_history视图查询。
每一个会话的排序区相互独立。其中一个会话超出限额不会影响后续会话的执行。
新排序机制则有所不同,全局排序区耗光后,后续排序会话将报-544错误。以后的文章将详细介绍。