tag:blogger.com,1999:blog-8540221137227899134.post2059409112455521614..comments2024-03-17T00:36:18.345-07:00Comments on A Dash of Technology: Multi-column (SQL-like) sorting in RedisJosiah Carlsonhttp://www.blogger.com/profile/16662314724540946069noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-8540221137227899134.post-64433342715684370352016-04-07T23:06:09.129-07:002016-04-07T23:06:09.129-07:00Strings are not usable as scores in sorted sets, g...Strings are not usable as scores in sorted sets, generally, so that isn't a concern.<br /><br />If you used something like this: https://github.com/josiahcarlson/rom/blob/master/rom/util.py#L310 ... which generates scores from strings, then you are likely limited to a single-column sort, not a multi-column tuple sort like most relational databases offer.Josiah Carlsonhttps://www.blogger.com/profile/16662314724540946069noreply@blogger.comtag:blogger.com,1999:blog-8540221137227899134.post-4431405739068147202016-04-03T23:20:24.221-07:002016-04-03T23:20:24.221-07:00Hi, awesome article.
But what if one of the column...Hi, awesome article.<br />But what if one of the columns were a string? How would the approach change then?Anonymoushttps://www.blogger.com/profile/03390689869040116017noreply@blogger.comtag:blogger.com,1999:blog-8540221137227899134.post-3429985287307510672015-08-10T23:57:55.451-07:002015-08-10T23:57:55.451-07:00It makes it clear. Thank you. It makes it clear. Thank you. Sokolhttps://www.blogger.com/profile/01822879038221900341noreply@blogger.comtag:blogger.com,1999:blog-8540221137227899134.post-19376027150055035312015-08-09T08:29:51.970-07:002015-08-09T08:29:51.970-07:00When ordering your sort columns, you must pick the...When ordering your sort columns, you must pick the *highest* priority sort column first. The order you are getting is saying that *weight* is more important than *age*, so you are saying sort=('weight', 'age'). If you pass sort=('age', 'weight'), then you will get:<br />ZINTERSTORE res 2 weights ages 1 80<br /><br />Which will result in:<br />Nick 2150<br />Dough 2160<br />Kate 2210<br />John 2235<br /><br />Which is the order you are asking for.<br /><br />As for what it's doing... Let's pretend for a moment that the underlying "score" inside a ZSET was a 64 bit integer (it's actually an IEEE 754 floating point double, but what happens is easier to explain with an integer). We pack the integer based on segments. The lowest-bits are packed with scores from the least-important data, and the highest-bits are packed with scores from the most-important data:<br />[high bits ... low bits] -> [important scores ... less important scores]<br /><br />This gives us the equivalent of a tuple inside an integer (or double), which is how SQL sorts its data when saying "ORDER BY age, weight".<br /><br />Note: I just made 2 small fixes to the code in the gist and post, which fixes a couple issues in practice.Josiah Carlsonhttps://www.blogger.com/profile/16662314724540946069noreply@blogger.comtag:blogger.com,1999:blog-8540221137227899134.post-92103215685687114492015-08-09T07:30:14.869-07:002015-08-09T07:30:14.869-07:00Can you please explain why should it work? Lets sa...Can you please explain why should it work? Lets say I have users:<br /><br />id name age weight<br />1 Nick 26 70<br />2 John 27 75<br />3 Dough 26 80<br />4 Kate 27 50<br /><br />Ages set is: 26 26 27 27<br />Weights set is: 50 70 75 80<br /><br />And I want to sort by age and weight, so I do (based on the code):<br />ZINTERSCORE res 2 ages weights 1 27<br /><br />That will result in:<br />Kate 1377<br />Nick 1916<br />John 2052<br />Dough 2186<br /><br />While expected is:<br />Nick<br />Dough<br />Kate<br />JohnSokolhttps://www.blogger.com/profile/01822879038221900341noreply@blogger.comtag:blogger.com,1999:blog-8540221137227899134.post-28079192092573324722013-10-24T21:53:04.486-07:002013-10-24T21:53:04.486-07:00This comment has been removed by a blog administrator.Anonymoushttps://www.blogger.com/profile/02612621968899506746noreply@blogger.com