bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Database - 資料庫 MS SQL Server 用TSQL建立50萬筆紀錄的大資料表
查看: 7430|回復: 0
go

用TSQL建立50萬筆紀錄的大資料表 [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2006-11-2 13:08 |只看該作者 |倒序瀏覽 |打印
經常做資料庫效能測試時,需要用到資料量很大的資料表,自己動手寫一段 TSQL 語法即可。

TSQL_建立資料表結構:
  1. /**//****** 對象: 表 [dbo].[LargeTable]    腳本日期: 2006-10-26 15:40:27 ******/
  2. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[LargeTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  3. drop table [dbo].[LargeTable]
  4. GO

  5. /**//****** 對象: 表 [dbo].[LargeTable]    腳本日期: 2006-10-26 15:40:27 ******/
  6. CREATE TABLE [dbo].[LargeTable] (
  7.   [ID] [int] IDENTITY (1, 1) NOT NULL ,
  8.   [Title] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
  9.   [Content] [ntext] COLLATE Chinese_PRC_CI_AS NULL ,
  10.   [PublicTime] [datetime] NULL ,
  11.   [Author] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
  12.   [IsTop] [tinyint] NULL
  13. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  14. GO

  15. ALTER TABLE [dbo].[LargeTable] WITH NOCHECK ADD CONSTRAINT [PK_LargeTable] PRIMARY KEY  CLUSTERED ([ID])  ON [PRIMARY]
  16. GO

  17. ALTER TABLE [dbo].[LargeTable] ADD CONSTRAINT [DF_LargeTable_IsTop] DEFAULT (0) FOR [IsTop]
  18. GO

  19. exec sp_addextendedproperty N'MS_Description', N'作者', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Author'
  20. GO
  21. exec sp_addextendedproperty N'MS_Description', N'内容', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Content'
  22. GO
  23. exec sp_addextendedproperty N'MS_Description', N'文章表,包含100萬筆紀錄', N'user', N'dbo', N'table', N'LargeTable', N'column', N'ID'
  24. GO
  25. exec sp_addextendedproperty N'MS_Description', N'是否置頂 0.不置頂 1.置頂', N'user', N'dbo', N'table', N'LargeTable', N'column', N'IsTop'
  26. GO
  27. exec sp_addextendedproperty N'MS_Description', N'發佈時間', N'user', N'dbo', N'table', N'LargeTable', N'column', N'PublicTime'
  28. GO
  29. exec sp_addextendedproperty N'MS_Description', N'文章標題', N'user', N'dbo', N'table', N'LargeTable', N'column', N'Title'
  30. GO
複製代碼



TSQL_產生資料:
  1. /**//*truncate table largetable*/

  2. declare @title nvarchar(100)
  3. declare @content nvarchar(100)
  4. declare @publictime datetime
  5. declare @author nvarchar(10)
  6. declare @istop tinyint

  7. declare @randtime_month tinyint
  8. declare @randtime_day tinyint
  9. declare @randtime_hour tinyint
  10. declare @randtime_minute tinyint
  11. declare @randtime_second tinyint

  12. declare @str varchar(30)

  13. print '開始執行時間:' + cast(getdate() as varchar)
  14. declare @i int, @count int
  15. set @i = 1
  16. set @count = 500000
  17. while @i <= @count
  18. begin
  19.   set @randtime_month = rand(@i) * 12
  20.   set @randtime_day   = rand(@i) * 28
  21.   set @randtime_hour  = rand(@i) * 24
  22.   set @randtime_minute = rand(@i) * 60
  23.   set @randtime_second = rand(@i) * 60
  24.   set @str = '2006-' + cast(@randtime_month as varchar) + '-' + cast(@randtime_day as varchar) + ' ' + cast(@randtime_hour as varchar) + ':' + cast(@randtime_minute as varchar) + ':' + cast(@randtime_second as varchar)

  25.   set @title = '文章標題' + cast(@i as varchar)
  26.   set @content = '文章内容' + cast(@i as varchar)
  27.   set @publictime = convert(datetime, @str, 120)
  28.   set @author = '作者' + cast(@i as varchar)
  29.   if @i % 10000 = 0
  30.     set @istop = 1
  31.   else
  32.     set @istop = 0

  33.   insert into largetable values(@title, @content, @publictime, @author, @istop)

  34.   set @i=@i+1
  35. end
  36. print '執行完畢時間:' + cast(getdate() as varchar)
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-4-25 16:11 , Processed in 0.014428 second(s), 11 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.