我正在开发一个应用程序,它应该在5,760,000行中的char(26)列中找到26个字母的代码。我需要知道需要多长时间。我正在使用MS SQL Server 2012 Express。
我的数据库只有一个表myTable
:
Idcolumn integer
CodeColumn char(26)
DateAndTimeColumn datetime
我想用数据填充此表来进行一些测试,并找出从数据库中获取答案需要多长时间。我不知道如何编写正确的tsql语句来填充我的表5,760,000行。特别是第二列非常长。如何填充表格以填充表格?
假设我使用语句
时,数据应该是这样的SELECT IdColumn, CodeColumn, DateAndTimeColumn FROM myTable;
输出:
1 00000000000000000000000001 2014-11-19 15:46:50.843
2 00000000000000000000000002 2014-11-19 15:46:54.310
3 00000000000000000000000003 2014-11-19 15:46:56.060
依此类推......直到5,760,000行。
我该怎么做?
答案 0 :(得分:4)
;WITH Numbers AS
(
SELECT TOP (5760000)
IdColumn = CONVERT(INT, ROW_NUMBER() OVER (ORDER BY s1.[object_id]))
FROM sys.all_objects AS s1
CROSS JOIN sys.all_objects AS s2
CROSS JOIN sys.all_objects AS s3
)
INSERT INTO dbo.YourTable
SELECT IdColumn,
RIGHT(REPLICATE('0',26)+CONVERT(VARCHAR(26),IdColumn),26) CodeColumn,
GETDATE() DateAndTimeColumn
FROM Numbers;
答案 1 :(得分:1)
使用Lamak的优秀示例,这是另一种方法。唯一的区别是这将创建一个1000万行cte,零读取。当你使用sys.all_objects时,由于所有的I / O,它会变得非常慢。
WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b),
E4(N) AS (SELECT 1 FROM E2 a, E2 b),
E6(N) AS (SELECT 1 from E4 a, E2 b, E1 c),
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E6
)
INSERT INTO dbo.YourTable
SELECT IdColumn,
RIGHT(REPLICATE('0',26)+CONVERT(VARCHAR(26),IdColumn),26) CodeColumn,
GETDATE() DateAndTimeColumn
FROM cteTally
where cteTally.N <= 5760000
答案 2 :(得分:0)
非常类似于Lamak的答案,这不取决于您的数据库结构:
;WITH Numbers AS
(
SELECT 1 AS id UNION SELECT 2 UNION SELECT 3 UNION
SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION
SELECT 10
),
Joins AS
(
SELECT TOP (5760000)
IdColumn = CONVERT(INT, ROW_NUMBER() OVER (ORDER BY s1.[id]))
FROM Numbers AS s1 --10
CROSS JOIN Numbers AS s2 --100
CROSS JOIN Numbers AS s3 --1.000
CROSS JOIN Numbers AS s4 --10.000
CROSS JOIN Numbers AS s5 --100.000
CROSS JOIN Numbers AS s6 --1.000.000
CROSS JOIN Numbers AS s7 --10.000.000
)
INSERT INTO #YourTable
SELECT IdColumn,
RIGHT(REPLICATE('0',26)+CONVERT(VARCHAR(26),IdColumn),26) CodeColumn,
GETDATE() DateAndTimeColumn
FROM Joins;
它只生成一个从1到10的数字列表,然后它一直交叉连接到10 ^ 7。