Sybase ASE 按等效级别连接
Sybase ASE connect by level equivalent
我想生成从 0 到 9000000 的数字。在 Oracle 中,我可以使用下面的代码。我如何在 Sybase ASE 中执行此操作?
这是在 Oracle 中:
SELECT level Num
FROM DUAL
CONNECT BY LEVEL <= 9000000
ORDER BY Num;
如何在 Sybase ASE 中执行此操作?
我无法创建 table 并添加身份,因为我需要从 1 到 9000000 的数字,所以 table 会很复杂。是否有执行此操作的查询?
这可能对您有用,但需要一点时间(但您可以分部分进行)。抱歉,但我不知道最好的方法,只是下面的:
select * from
(select (t6.i*1000000 + t5.i*100000 + t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_value from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t5,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t6) v
where selected_value between 1 and 200 --here you can change the interval ex.: between 201 and 1000
order by selected_value
例如,如果您需要 99999 之前的数字,则不需要 t6 和 t5(只是为了更好地解释)。
我运行这里,用了1分多钟才500万。
您能否详细说明 what/how 您打算使用那 900 万 (+1) 个号码?
如果唯一的目的是将数字流发送回客户端,在我看来,让客户端应用程序生成 900 万个数字会更有效率。
有很多方法可以使用基于少量 table 的笛卡尔积的单个查询生成数字序列(参见 RMH 的 base-10 示例),但所有这些解决方案都需要:
- 首先,要在 tempdb 中构建整个笛卡尔积
- 然后,要排序的整个笛卡尔积(在内存和 tempdb 中)
- 然后才将所需号码发送回客户端
无论您是要生成一小组数字(1 到 10)还是大量数字(1 到 9,000,000),相同的 'generator' 查询都需要这种开销。
显然需要使用一种更好、更高效、轻量级的方法来避免占用数据服务器资源(例如,想象几个 users/applications 试图生成 9,000,000 个数字......所有这些都使用大量的 tempdb space 以及 cpu 资源 ... egad ... 期待来自 DBA 的愤怒 email/phone-call!)。
另一个想法是使用循环构造来生成所需的数字...
declare @counter bigint, @max bigint
select @counter=0, @max=9000000
while @counter <= @max
begin
select @counter
select @counter=@counter+1
end
...虽然这将消除 tempdb 开销,但您仍然会耗尽适量的 cpu 资源循环 900 万次。额外的性能损失可能来自生成 900 万个 1 行结果集的开销,特别是您可能会看到在数据服务器和客户端应用程序之间流动的网络数据包数量过多。
虽然我们可以通过使最终结果看起来像单个结果集来减少网络数据包的数量,但这需要将上述循环结构转换为存储过程,确保您定义了环回服务器,然后创建一个代理 table 来引用所述过程;然后,您将查询代理 table 以获得包含所需数字集的单个结果集。
'当然,在这一点上,我们现在不得不跳过几个环节,只是为了让数据服务器生成(以某种有效的方法)一系列数字作为单个结果集。
Sybase/ASE 可以通过其他方式生成一系列数字(例如,create/populate 带有标识列的 table),但所有这些都需要一个) 大量的数据服务器资源或 b) 一些复杂的代码(例如,应用程序上下文函数、插件 java 代码)...做一些可能由 client/front-end 应用程序更有效地处理的事情.
在 Sybase IQ 中,有一个可用于生成数字的系统过程:sa_rowgenerator
你本来可以做到的:
SELECT row_num FROM sa_rowgenerator( 1, 9000000);
我根本不知道 Sybase ASE,所以我用谷歌搜索了一下,发现 ASE 中没有这个过程,但有一个替代方法:
The SQL Anywhere system procedures sa_rowgenerator, sa_split_list, and
sa_conn_info are not supported by ASE. An ASE master database contains
a table, spt_values, that can be used to SELECT integer values in a
way similar to that of the sa_rowgenerator procedure, or SQL
Anywhere’s dbo.row_generator system table.
来源:Migrating SQL Anywhere database applications to ASE
这个 table spt_values
包含整数令人难以置信地没有记录。就像鬼一样table。
我建议你试一试:
select number
FROM master.dbo.spt_values
WHERE number BETWEEN 0 AND 9000000
但是如果你的数据库系统爆炸我概不负责;-)
我想生成从 0 到 9000000 的数字。在 Oracle 中,我可以使用下面的代码。我如何在 Sybase ASE 中执行此操作?
这是在 Oracle 中:
SELECT level Num
FROM DUAL
CONNECT BY LEVEL <= 9000000
ORDER BY Num;
如何在 Sybase ASE 中执行此操作?
我无法创建 table 并添加身份,因为我需要从 1 到 9000000 的数字,所以 table 会很复杂。是否有执行此操作的查询?
这可能对您有用,但需要一点时间(但您可以分部分进行)。抱歉,但我不知道最好的方法,只是下面的:
select * from
(select (t6.i*1000000 + t5.i*100000 + t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_value from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t5,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t6) v
where selected_value between 1 and 200 --here you can change the interval ex.: between 201 and 1000
order by selected_value
例如,如果您需要 99999 之前的数字,则不需要 t6 和 t5(只是为了更好地解释)。 我运行这里,用了1分多钟才500万。
您能否详细说明 what/how 您打算使用那 900 万 (+1) 个号码?
如果唯一的目的是将数字流发送回客户端,在我看来,让客户端应用程序生成 900 万个数字会更有效率。
有很多方法可以使用基于少量 table 的笛卡尔积的单个查询生成数字序列(参见 RMH 的 base-10 示例),但所有这些解决方案都需要:
- 首先,要在 tempdb 中构建整个笛卡尔积
- 然后,要排序的整个笛卡尔积(在内存和 tempdb 中)
- 然后才将所需号码发送回客户端
无论您是要生成一小组数字(1 到 10)还是大量数字(1 到 9,000,000),相同的 'generator' 查询都需要这种开销。
显然需要使用一种更好、更高效、轻量级的方法来避免占用数据服务器资源(例如,想象几个 users/applications 试图生成 9,000,000 个数字......所有这些都使用大量的 tempdb space 以及 cpu 资源 ... egad ... 期待来自 DBA 的愤怒 email/phone-call!)。
另一个想法是使用循环构造来生成所需的数字...
declare @counter bigint, @max bigint
select @counter=0, @max=9000000
while @counter <= @max
begin
select @counter
select @counter=@counter+1
end
...虽然这将消除 tempdb 开销,但您仍然会耗尽适量的 cpu 资源循环 900 万次。额外的性能损失可能来自生成 900 万个 1 行结果集的开销,特别是您可能会看到在数据服务器和客户端应用程序之间流动的网络数据包数量过多。
虽然我们可以通过使最终结果看起来像单个结果集来减少网络数据包的数量,但这需要将上述循环结构转换为存储过程,确保您定义了环回服务器,然后创建一个代理 table 来引用所述过程;然后,您将查询代理 table 以获得包含所需数字集的单个结果集。
'当然,在这一点上,我们现在不得不跳过几个环节,只是为了让数据服务器生成(以某种有效的方法)一系列数字作为单个结果集。
Sybase/ASE 可以通过其他方式生成一系列数字(例如,create/populate 带有标识列的 table),但所有这些都需要一个) 大量的数据服务器资源或 b) 一些复杂的代码(例如,应用程序上下文函数、插件 java 代码)...做一些可能由 client/front-end 应用程序更有效地处理的事情.
在 Sybase IQ 中,有一个可用于生成数字的系统过程:sa_rowgenerator
你本来可以做到的:
SELECT row_num FROM sa_rowgenerator( 1, 9000000);
我根本不知道 Sybase ASE,所以我用谷歌搜索了一下,发现 ASE 中没有这个过程,但有一个替代方法:
The SQL Anywhere system procedures sa_rowgenerator, sa_split_list, and sa_conn_info are not supported by ASE. An ASE master database contains a table, spt_values, that can be used to SELECT integer values in a way similar to that of the sa_rowgenerator procedure, or SQL Anywhere’s dbo.row_generator system table.
来源:Migrating SQL Anywhere database applications to ASE
这个 table spt_values
包含整数令人难以置信地没有记录。就像鬼一样table。
我建议你试一试:
select number
FROM master.dbo.spt_values
WHERE number BETWEEN 0 AND 9000000
但是如果你的数据库系统爆炸我概不负责;-)