I have been busy recently. It should have been three months since I did not write a blog.
Blog directory: Index & writing plan
This requirement was met in the use of Mongo, but I think it is easier to put this requirement into traditional RDBMS. The requirement is as follows: assume that your database uses sqlserver and there is a table with data records. You need to select a random data entry in the table.
Suppose the data structure discussed in this article (clustered index on PK, username on non clustered index ):
Your first response is probably: Oh, Mom, it's a coincidence that the primary key uses int auto-incrementing. I only need to generate a random number and then find the primary key corresponding to the random number.
The implementation steps are roughly as follows: ① return the maximum idmax value of the ID in the database ② generate a random number int random = new random (). Next (1, idmax) between 1 and idmax );
③ Use userid = random as the condition Query
④ If no data is found, a random number is generated again and then searched again (because the data of a userid may be deleted)
This method is simple and violent, but there is a fatal problem: When I create a table, to illustrate this method, the primary key uses int, however, in most production environments I know, GUID is used. This fatal problem will directly cause the method above to be unavailable.
As to why most of the production environments I know use guids instead of int, I will make a comparison in the next article.
Since int cannot use guid as the primary key, we should use row_number. Sqlserver must support row_number. It seems that Oracle and MySQL have similar concepts. (If you are not sure, ask your colleagues for a positive answer. I didn't go into it)
The implementation steps are roughly as follows: ① return the total number of data records in the database count ② generate a random number from 1 to count int random = new random (). Next (1, count );
③ Query the data of row_number = random.
However, row_number has an extremely bad condition, that is, the slower the data that comes after the query, the more resources the query consumes. However, databases that store data in sequence have this problem. For example, the following two statements:
select * from(SELECT UserID,UserName,Password,Sex,City,ROW_NUMBER()OVER(ORDER BY CURRENT_TIMESTAMP) as Number FROM [User_db].[dbo].[Users] ) as query where query.Number = 20 select * from(SELECT UserID,UserName,Password,Sex,City,ROW_NUMBER()OVER(ORDER BY CURRENT_TIMESTAMP) as Number FROM [User_db].[dbo].[Users] ) as query where query.Number = 5000000
The first row to query row_number = 20, logical reads 5. elapsed time = 58 Ms.
The second row queries data with row_number = 5000000. Logical reads 90208. elapsed time = 900 ms.
It can be seen that the latter has too many logical reads, and the running speed is much slower. If this function is frequently used, for example, it is recommended to users randomly, this will become a performance bottleneck.
Some netizens say this sentence:
SELECT TOP 1 * FROM Users ORDER BY NEWID()
The running results are correct, but the efficiency is greatly reduced. For example, I found 1,336,793rd pieces of data, logical reads 90208, elapsed time = 3026 MS
View the execution plan and find that sort occupies 98% of the total usage:
Is there a better way than row_number?
The answer is to add a random column to the table to change the data structure to the following:
When adding data, a random number is generated and inserted. In this example, a random number between 0 and 0.1 billion can be generated at the beginning. Note: you must add an index to the random.
The procedure is roughly as follows: ① Add a random value when inserting data
② Generate a random number and query the select top (1) * from users where random> Random Number
③ There may be multiple results (but not many) for this query, and then randomly select one of the multiple data records (this can be easily implemented using LINQ, not to be repeated)
Now, I have finished talking about it. Please allow me to sell cute words at the end: Smart readers, start your brains. Do you have any better solutions? If yes, leave a message
PS: I'm leaving now. I'm going to go home and have a rest for 2 months. Can someone recommend something fun?