Redis Series (vi)-sortedsets design tips

Source: Internet
Author: User
Tags ranges

Read the catalogue:

    1. Introduced
    2. Score-occupied
    3. More bit information
    4. Summarize
Introduced

Redis Sorted Sets is a string collection that resembles a redis sets data structure and does not allow duplicates. The difference is that each member in the sorted sets is assigned a fractional value (score), which is used to sort the members in sorted sets, from the minimum to the maximum value. All members of Sorted sets are unique, and their fractions (score) can be duplicated, meaning that a score may correspond to multiple values.

With sorted sets you can add, delete, or update members very quickly, with the complexity of O (M*log (n)) and m being the number of members added or queried. Because members are added in order, they can be queried very quickly by score or by index. Accessing the elements in the middle of the sorted sets is also very fast, so you can use sort sets as a small, non-repeating sequence of tables. With sorted sets you can quickly manipulate anything you want to do: sort the members, determine whether members are in the collection, and quickly access the members in the middle of the collection.

If you add the same value to sorted sets more than once, Redis will be based on the last value score.

Overall, in other databases more difficult tasks to complete, with sorted sets can be faster and better performance of the completion.

For more sorted sets usage, you can view official documents.

Score-occupied

The score in Sorted sets is a 64-bit integer type that ranges from 9007199254740992 to 9007199254740992, which is a very useful key point.

We can usually use it for simple range queries, such as using age as a fraction and querying all users of a certain age by Zrangebyscore.
For users who are searching for 18-35 years old:

 -  *

If there are more complex requirements, such as by date, user type, etc. to query, it is more difficult. If you use other data structures, you want to query multiple round-trip operation, there is performance loss. Here you can use the length attribute of the score integer type to store the query's conditional information.

For example, some data needs to be queried by date range, which is a very common scenario.

Use sorted sets when storing the data structure, add key when the date is simply transcoded.

There are 365 days in a year, three bits are reserved in score to store the days, and the number of days of the year is calculated by DayOfYear:

var time1=datetime.now.dayofyear

Because the data before the 100th day is less than three, you need to use 0 completion, write a method:

stringAutocompletion (intLengthintnum) {        stringstr =Num.        ToString (); if(str. Length >length)Throw NewException ("Max length"); varReal = length-Str.        Length; varSB =NewStringBuilder (length);  for(intj =0; J < Real; J + +) {sb. Append ((Char) -); } sb.        Append (str); returnsb.    ToString (); }

Because the date of the data may span years, 2 bits are reserved before the day, and the year is stored. The length is now 5 bits, such as 15200 for 15 19th (No. 200 day).

Calculates the value of the current data item score by year + days.

var long. Parse (DateTime.Year.ToString (). Substring (22) + autocompletion (3, datetime.dayofyear));

Store the actual and fractional values in Redis:

Zadd Test Socre value

When querying, the date range that needs to be queried is converted to fractions, where the first 10 days of data are queried:

        int time1 = DateTime.Now.DayOfYear;         var 2 3 );         int time2 = DateTime.Now.DayOfYear;         var 2) + autocompletion (DateTime.Now.AddDays (-3);

Query in Redis:

Zrangebyscore User Score1 Score2

Check the information on the day (score):

More bit information

The R64 bit integer type has 16 locations, meaning that more dimension information can also be stored. This is a date-based, in addition to the type dimension, the date position shifted left 4 bits, reserved 4 bits to store the type dimension.

such as 152003456, its 34,564 bits is the type of storage, can be based on the type of how much can be increased or reduced bit occupancy.

If you simply query by date, the type does not pass, and the next 4 bits complement the minimum and maximum values at query time.

Data for the first 10 days of the query:

151900000 152009999

Query 15 year 19th number 3578 type of data:

152003578 152003578

Query 15 year 19th number 1000-4000 type of data:

152001000 152004000

It's easy to make a simple mapping table about types that are often not simple data types.

1001, etc...   

Note the conversion when adding or querying.

Summarize

It is important to note that score is an integer type stored from small to large, as the above design does not look for date ranges in the case of a specified type, because we define the date as the primary dimension.

Tables that have many hierarchical relationships in the database, such as large classifications, sub-classifications, and small categories, are usually reduced at the first level. Using sorted sets We can simulate this relationship to design the score value, the primary dimension, the sub-dimension, the lower dimension, so that the scope of the information can be more quickly query, there is a premise that 64-bit integer position enough.

If you do not have a similar requirement, you can use the timestamp as the fractional value of the value so that no additional conversions are required.

Simple sharing of sorted sets storage design experience, we hope to help.

Redis Series (vi)-sortedsets design tips

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.