How SQL Server quickly generates 1 million random 8-bit numbers that are not duplicated

Source: Internet
Author: User
Tags rand repetition rowcount

  1. Recently in the forum to see someone asked, how to quickly generate 1 million non-repeating 8-digit number, for this issue, there are a few things to note:
  2. 1. How to generate a 8-bit random number, the more random the generated number, the more likely the repetition is, of course, the smaller
  3. 2. Control does not repeat
  4. 3. Consider performance
  5. In response to this problem, I have written the following examples to solve, hoping to provide guidance to those who have such needs
  6. */
  7. Use tempdb
  8. GO
  9. --Create a test table
  10. CREATE TABLE TB (ID char (8))
  11. --Create a unique index for automatic filtering of duplicate values
  12. CREATE UNIQUE INDEX ix_tb on TB (ID)
  13. With Ignore_dup_key
  14. GO
  15. --Test the processing time of data insertion, and record the point in time at which processing begins
  16. DECLARE @dt datetime
  17. SET @dt = GETDATE ()
  18. --Inserting random data
  19. SET NOCOUNT on
  20. DECLARE @row int
  21. Set @row = 1000000 --Sets the total number of records
  22. While @row >0
  23. BEGIN
  24. -Displays a message indicating that multiple rows of data are also required
  25. RAISERROR (' need%d rows ', ten, 1, @row) with NOWAIT
  26. --Inserting random bits encoded data
  27. SET ROWCOUNT @row
  28. INSERT TB SELECT id = right (100000000 + CONVERT (bigint, ABS (CHECKSUM (NEWID ())), 8)
  29. From syscolumns c1,syscolumns C2
  30. SET @row = @row-@ @ROWCOUNT
  31. END
  32. --Displays the time that the data was inserted for use
  33. SELECT begindate = @dt, EndDate = GETDATE (), Seconds = DATEDIFF (Second, @dt, GETDATE ())
  34. GO
  35. --Shows whether the final result record is correct
  36. SELECT COUNT (*) from TB
  37. SET ROWCOUNT 0
  38. SET NOCOUNT OFF
  39. GO
  40. --Delete test
  41. DROP TABLE TB
  42. /*
  43. Techniques used to solve the problem:
  44. 1. Control the resulting data is not duplicated, directly using the IGNORE_DUP_KEY option in a unique index, so that duplicate values in the inserted data are automatically filtered to avoid manual processing of duplicates
  45. 2. Use CHECKSUM with the NEWID () function to make the generated data as random as possible, and generally generate random numbers to consider using the RAND () function.
  46. But this function is to produce pseudo-random values, test with the following statement, you will find that the resulting data are all the same, this does not apply to bulk generation of multiple random numbers,
  47. The NEWID () function produces a GUID that is basically not duplicated, and then translates it into a number by checksum, so that the likelihood of duplication is smaller
  48. SELECT TOP ten RAND () from sysobjects
  49. 3. In efficiency control, use the Loop + batch generation method instead of the traditional one-by-one generation. In SQL Server, each INSERT statement will have an internal transaction,
  50. If you insert, the cost of the transaction is too large, the efficiency will be very low, do not consider generating 1 million data at a time, one because the generated data may be duplicated,
  51. Remove the repetition there is no 1 million, two one-time generation of 1 million data, consumption of memory and CPU resources is also very high, the general computer may not withstand
  52. */

How SQL Server quickly generates 1 million random 8-bit numbers that are not duplicated

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.