Redis Sorted sets Operation Learning Notes

Source: Internet
Author: User
Tags hash redis


Sorted set is an upgraded version of set that adds an ordered attribute to the set based on the

You can specify that when you modify an element, Zset automatically adjusts the order of the new values each time you specify it. Can be understood as having

Two-column MySQL table, a list of stored value, a list of stored order. The key in the operation is understood as Zset's name.


Like set sorted set is also a collection of string elements, and each element is associated with a double

Type of score. The implementation of the sorted set is a mixture of skip list and hash table.

When an element is added to the collection, the mapping of an element to score is added to the hash table, so given a

Element gets the score cost is O (1), another score to the element's mapping is added to the Skip list, and follows score row

Order, so you can get the elements in the collection in an orderly fashion. Add, delete operation cost is O (log (N)) and Skip list

Cost consistent, Redis's skip list implementation uses a two-way list, so that you can reverse-take elements from the tail. Sorted set Most

The regular use should be used as an index. We can store the fields to be sorted as score, and the ID of the object

When the element is stored.

Here's an example of using Sorted Sets

MySQL has a table, assuming the name is Summary_data bar, record number of 30M or so,
There is one field first_path for varchar (256), and the 10 first_path that appear most frequently need to be found.

Method one) Direct SQL statement

SQL statements are well written:

The code is as follows Copy Code
SELECT First_path, COUNT (*) as C from Summary_data GROUP by First_path Order by C DESC LIMIT 10;

The table is indexed, but the length of the index is KEY ' First_path ' (' First_path ' (255)), which may be the reason why the index cannot be used:
Id:1
Select_type:simple
Table:summary_data
Type:all
Possible_keys:null
Key:null
Key_len:null
Ref:null
rows:28136948
Extra:using temporary; Using Filesort

This SQL runs for 9 minutes.

Export First_path, generate file Input/first_path, each line a record, say this export process is really slow.

Method II) Sort and uniq

Sort Input/first_path | Uniq-c |sort-nr | Head-n 10
The state of being sorted is the state of grouping.
Uniq-c is used to count the number of each group.
SORT-NR the statistical results in descending order
1.5 done.

Method III) sorted set of Redis

In this way, just because of the sudden thought that sorted set was born to do so.

Client libary ready to use Hiredis.

Installation is simple: Make && make install
You can see that the library file is installed to the/usr/local/lib/directory
Header file installed to the/usr/local/include/hiredis/directory

Need to add/usr/local/lib/to/etc/ld.so.conf
Then Ldconfig

Compile:

The code is as follows Copy Code

Gcc-i/usr/local/include/hiredis-lhiredis./example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include
int main (int argc, char **argv) {
unsigned int J;
Rediscontext *c;
Redisreply *reply;
const char *hostname = (argc > 1)? ARGV[1]: "127.0.0.1";
int port = (argc > 2)? Atoi (argv[2]): 6379;

struct Timeval timeout = {1, 500000}; 1.5 seconds
c = redisconnectwithtimeout (hostname, port, timeout);
if (c = = NULL | | c->err) {
if (c) {
printf ("Connection Error:%SN", C-&GT;ERRSTR);
Redisfree (c);
} else {
printf ("Connection error:can ' t allocate Redis contextn");
}
Exit (1);
}

FILE * FP;


fp = fopen (Argv[3], "R");
if (!FP) exit (1);

Char line[256];

while (line, sizeof (line)-1, FP)!= NULL) {fgets
Reply = Rediscommand (c, "Zincrby Myzset_firstpath 1%s", line);
Freereplyobject (reply);
}

Fclose (FP);

Reply = Rediscommand (c, "Zrevrangebyscore myzset_firstpath +inf-inf withscores LIMIT 0 10");

if (Reply->type = = Redis_reply_array) {
for (j = 0; J < reply->elements; j+=2) {
printf ("%u)%s%sn", (unsigned int) (J/2), Reply->element[j]->str, REPLY-&GT;ELEMENT[J+1]-&GT;STR);
}
}


Freereplyobject (reply);
/* Disconnects and frees the context * *
Redisfree (c);

return 0;
}

16 minutes results, not good enough.

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.