简体   繁体   English

如何将巨大的虚拟数据插入到Sql server

[英]How to insert Huge dummy data to Sql server

Currently development team is done their application, and as a tester needs to insert 1000000 records into the 20 tables, for performance testing.目前开发团队已经完成了他们的应用,作为测试人员需要在20张表中插入1000000条记录,进行性能测试。

I gone through the tables and there is relationship between all the tables actually.我浏览了表格,实际上所有表格之间都有关系。

To insert that much dummy data into the tables, I need to understand the application completely in very short span so that I don't have the dummy data also by this time.要将那么多虚拟数据插入表中,我需要在很短的时间内完全了解应用程序,以便此时我也没有虚拟数据。

In SQL server is there any way to insert this much data insertion possibility.SQL server中有什么方法可以插入这么多数据插入的可能性。

please share the approaches.请分享方法。

  1. Currently I am planning with the possibilities to create dummy data in excel , but here I am not sure the relationships between the tables.目前我正在计划在excel 中创建虚拟数据的可能性,但在这里我不确定表之间的关系。
  2. Found in Google that SQL profiler will provide the order of execution, but waiting for the access to analyze this.在Google中发现SQL profiler会提供执行顺序,但是等待访问来分析这个。
  3. One more thing I found in Google is red-gate tool can be used.我在 Google 中发现的另一件事是可以使用red-gate工具。

Is there any script or any other solution to perform this tasks in simple way.是否有任何脚本或任何其他解决方案可以以简单的方式执行此任务。

I am very sorry if this is a common question, I am working first time in SQL real time scenario.如果这是一个常见问题,我很抱歉,我是第一次在 SQL 实时场景中工作。 but I have the knowledge on SQL.但我对 SQL 有一定的了解。

Why You don't generate those records in SQL Server.为什么不在 SQL Server 中生成这些记录。 Here is a script to generate table with 1000000 rows:这是一个生成 1000000 行表的脚本:

DECLARE @values TABLE (DataValue int, RandValue INT)

;WITH mycte AS
(
SELECT 1 DataValue
UNION all
SELECT DataValue + 1
FROM    mycte   
WHERE   DataValue + 1 <= 1000000
)
INSERT INTO @values(DataValue,RandValue)
SELECT 
        DataValue,
        convert(int, convert (varbinary(4), NEWID(), 1)) AS RandValue
FROM mycte m 
OPTION (MAXRECURSION 0)


SELECT 
        v.DataValue,
        v.RandValue,
        (SELECT TOP 1 [User_ID] FROM tblUsers ORDER BY NEWID())
FROM    @values v

In table @values You will have some random int value(column RandValue) which can be used to generate values for other columns.在表@values 中,您将有一些随机整数值(列 RandValue),可用于为其他列生成值。 Also You have example of getting random foreign key.你也有获取随机外键的例子。

Below is a simple procedure I wrote to insert millions of dummy records into the table, I know its not the most efficient one but serves the purpose for a million records it takes around 5 minutes.下面是我编写的将数百万条虚拟记录插入表中的简单程序,我知道它不是最有效的方法,但可以达到一百万条记录的目的,大约需要 5 分钟。 You need to pass the no of records you need to generate while executing the procedure.您需要传递执行过程时需要生成的记录数。

IF  EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DUMMY_INSERT]') AND type in (N'P', N'PC'))
BEGIN
    DROP PROCEDURE  DUMMY_INSERT
END
GO
CREATE PROCEDURE DUMMY_INSERT (
@noOfRecords INT
)
AS
BEGIN

DECLARE @count int
SET @count = 1;

WHILE (@count < @noOfRecords)
BEGIN
    INSERT INTO [dbo].[LogTable] ([UserId],[UserName],[Priority],[CmdName],[Message],[Success],[StartTime],[EndTime],[RemoteAddress],[TId])
     VALUES(1,'user_'+CAST(@count AS VARCHAR(256)),1,'dummy command','dummy message.',0,convert(varchar(50),dateadd(D,Round(RAND() * 1000,1),getdate()),121),convert(varchar(50),dateadd(D,Round(RAND() * 1000,1),getdate()),121),'160.200.45.1',1);

     SET @count = @count + 1;
END
END

you can use the cursor for repeat data:您可以将光标用于重复数据:

for example this simple code:例如这个简单的代码:

Declare @SYMBOL nchar(255), --sample V
         @SY_ID     int         --sample V
Declare R2 Cursor
    For SELECT  [ColumnsName]
        FROM    [TableName]
    For Read Only;
Open R2
Fetch  Next From R2 INTO @SYMBOL,@SY_ID
    While (@@FETCH_STATUS <>-1 )
        Begin
            Insert INTO [TableName] ([ColumnsName])
                Values              (@SYMBOL,@SY_ID)
            Fetch  Next From R2 INTO @SYMBOL,@SY_ID
        End
Close R2
Deallocate R2
/*wait a ... moment*/
SELECT COUNT(*)                  --check result
        FROM [TableName]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM