SlideShare uma empresa Scribd logo
1 de 105
NoSQL Taiwan #6
   Redis Data Structure
About Me

•   Felix Lin

•   hellolucky

•   Rails developer

•   http://twitter.com/hellolucky123

•   http://blog.hellolucky.info
This talk will tell you ......


•   How to build a simple social website by using SQL

•   What's the performance issue about SQL

•   How to use the data structures of Redis

•   How to solve the performance issue of SQL by using Redis

•   Why I love Redis so much
This talk won’t tell you ......


•   How fast Redis is

•   How to Use Lua script in Redis

•   How to calculate memory usage

•   How to administrate Redis

•   The best practice of Redis
Database
User
User   UserFollowRelation
User   UserFollowRelation
Follow
User            UserFollowRelation
Follow
User              UserFollowRelation

       Follower
Follow
User              UserFollowRelation

       Follower




         Feed
Follow
User              UserFollowRelation

       Follower




         Feed
About User
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About Relation
About Relation
# Set Following User
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 2)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 3)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 4)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 5)
About Relation
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
                                UserFollowRelation Records >
                                          1000000
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
                                UserFollowRelation Records >
                                          1000000
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1


                                                Slow!
About Relation
# Find Following User Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
                              UserFollowRelation Records >
# Find Following User Count             1000000
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
                              UserFollowRelation Records >
# Find Following User Count             1000000
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1

                                            Slow!
About Relation
# Find User1's Following Users
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find User1'2 Following Users
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 2
# The Same Following Users
User1's Following Users & User2's Following Users
About Relation
# Find User1's Following Users            Slow + Slow
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find User1'2 Following Users
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 2
# The Same Following Users
User1's Following Users & User2's Following Users
About Relation
# Find User1's Following Users            Slow + Slow
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find User1'2 Following Users
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 2
# The Same Following Users
User1's Following Users & User2's Following Users


                       = Very Slow!
About Feed
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
BEGIN
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
COMMIT
About Feed
BEGIN                                     Transaction
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
COMMIT
About Feed
BEGIN                                     Transaction
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
COMMIT

                                                   Slow!
About Feed
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Following User's Feeds
SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')
About Feed
                                      A Little Data
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Following User's Feeds
SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')
About Feed
                                      A Little Data
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Following User's Feeds
SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')



                           Very Slow!
About Others
About Others

# Find Top 10 Of The Talkiest User
SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10
# Find Top 10 Of The Newest Feeds
SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
Redis?
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
                                Strings
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
 Strings are the most basic kind of Redis value. Redis Strings are binary
# Update User
UPDATE `users` SET `name` = string can contain any kind of data, for
 safe, this means that a Redis 'hellolucky123' WHERE `users`.`id` = 1
# DeleteaUser image or a serialized Ruby object.
 instance JPEG
                                         - http://redis.io/topics/data-types
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User hellolucky
 set user:1:name
 set user:1:feeds_count 0
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 set user:1:created_at 1350906890
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User hellolucky
 set user:1:name
 set user:1:feeds_count 0
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 set user:1:created_at 1350906890
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User Key
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User hellolucky
 set user:1:name
 set user:1:feeds_count 0
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 set user:1:created_at 1350906890
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User                   Value
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User hellolucky
 set user:1:name
 set user:1:feeds_count 0
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 set user:1:created_at 1350906890
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
                  Hash is better
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
                               Hashes
# Update User
 Redis Hashes are maps between string fields and string values, so they
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 are the perfect data type to represent objects.
# Delete User                         - http://redis.io/topics/data-types
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* KeyFROM `users` WHERE `users`.`id` = 1
# Update User
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
                                        Fields
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1             Values
# Update User
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERESets
                              Sorted `users`.`id` = 1
# Update User Sorted Sets are, similarly to Redis Sets, non repeating
              Redis
UPDATE `users` SET `name`difference is thatWHERE `users`.`id`a=Sorted
 collections of Strings. The = 'hellolucky123' every member of 1
    Set is associated with score, that is used in order to take the sorted
# Delete User
DELETE FROM from the smallest to the greatest score. While members
   set ordered, `users` WHERE `users`.`id` = 1
                                    are unique, scores may be repeated.
                                       - http://redis.io/topics/data-types
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User                 feeds_count
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User                  user_id
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User                   For top 10 of the talkiest user
 hmset user:1 name hellolucky feeds_count 0 created_at 1350906890
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
 zadd global:user_ids 0 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
hmget user:1 name feeds_count created_at
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
           HGETALL is better
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
hgetall user:1
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
hset user:1 name hellolucky123
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
# Find User
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About User
# Create User
INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES
('hellolucky', 0, 1350906890)
delFind User
# user:1
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
# Update User
UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1
# Delete User
DELETE FROM `users` WHERE `users`.`id` = 1
About Relation
# Set Following User
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 2)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 3)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 4)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 5)
About Relation
# Set Following User
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 2)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 3)                             Sets
INSERT are the most basic kind of Redis value. Redis Strings are binary
Strings INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 4) this means that a Redis string can contain any kind of data, for
safe,
instance a JPEG image or a serialized Ruby object.
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 5)                                  - http://redis.io/topics/data-types
About Relation
# Set Following User
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 2)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 3)
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
sadd user:1:follow_ids 2
(1, 4) user:2:follower_ids 1
sadd
INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES
(1, 5)
About Relation
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
smembers user:1:follow_ids
`user_follow_relations`.`follow_id` = 1
About Relation
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
 # Find Following User Ids
smembers user:1:follower_ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
# Find Following User Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
# Find Following User Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
# Find Following User Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
scard user:1:follow_ids
`user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
# Find Following User Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Follower Count
SELECT COUNT(*) FROM `users` INNER JOIN
`user_follow_relations` ON `users`.`id` =
`user_follow_relations`.`user_id` WHERE
`user_follow_relations`.`follow_id` = 1
About Relation
 # Find Following User Count
scard user:1:follower_ids `users` INNER JOIN
 SELECT COUNT(*) FROM
 `user_follow_relations` ON `users`.`id` =
 `user_follow_relations`.`follow_id` WHERE
 `user_follow_relations`.`user_id` = 1
 # Find Follower Count
 SELECT COUNT(*) FROM `users` INNER JOIN
 `user_follow_relations` ON `users`.`id` =
 `user_follow_relations`.`user_id` WHERE
 `user_follow_relations`.`follow_id` = 1
About Relation
# Find User1's Following Users
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find User1'2 Following Users
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 2
# The Same Following Users
User1's Following Users & User2's Following Users
About Relation
  # Find User1's Following Users
  SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
  ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
  `user_follow_relations`.`user_id` = 1
sinter user:1:follow_ids user:2:follow_ids
  # Find User1'2 Following Users
  SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
  ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
  `user_follow_relations`.`user_id` = 2
  # The Same Following Users
  User1's Following Users & User2's Following Users
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
                                    Store feed
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
                                     Push feed id to
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
                                        followers
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
                                     I’m lazy to list, it
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
                                     should more.....
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
                              Lists
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE Lists are simply lists of strings, sorted by insertion order. It is
   Redis `users`.`id` = 1
possible to add elements to a Redis List pushing new elements on the
              head (on the left) or on the tail (on the right) of the list.
                                      - http://redis.io/topics/data-types
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
hmset feed:1 user_id=11content description created_at 1350906890
1 WHERE `users`.`id`
sadd user:2:follow:feed_ids 1 For top 10 of the newest feeds
rpush global:feed_ids 1
About Feed
# Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
hincrby user:1 feeds_count 1
zincrby global:user_ids 1 1
 # Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
                                       Update counter
hincrby user:1 feeds_count 1
zincrby global:user_ids 1 1
 # Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
hincrby user:1 feeds_count 1
zincrby global:user_ids 1 1
 # Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
                                 For top 10 of the talkiest user
hincrby user:1 feeds_count 1
zincrby global:user_ids 1 1
 # Create Feed
INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,
'description', 1350906890)
# Update Counter
UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +
1 WHERE `users`.`id` = 1
About Feed
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Following User's Feeds
SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')
About Feed
# Find Following User Ids
SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`
ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE
`user_follow_relations`.`user_id` = 1
# Find Following User's Feeds
SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')


smembers user:1:follow:feed_ids
About Others

# Find Top 10 Of The Talkiest User
SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10
# Find Top 10 Of The Newest Feeds
SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
About Others

# Find Top 10 Of The Talkiest User
SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10
# Find Top 10 Of The Newest Feeds
SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
About Others

# Find Top 10 Of The Talkiest User
SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10
# Find Top 10 Of The Newest Feeds
SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10

zrevrange global:user_ids 0 9
About Others

# Find Top 10 Of The Talkiest User
SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10
# Find Top 10 Of The Newest Feeds
SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
About Others
lrange global:feed_ids 0 9
# Find Top 10 Of The Talkiest User
SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10
# Find Top 10 Of The Newest Feeds
SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
Conclusion
MySQL + Redis = Lego
QA

Mais conteúdo relacionado

Semelhante a NoSQL Taiwan #6 Redis Data Structure

Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Colin O'Dell
 
Development of Twitter Application #5 - Users
Development of Twitter Application #5 - UsersDevelopment of Twitter Application #5 - Users
Development of Twitter Application #5 - UsersMyungjin Lee
 
Using OpenFire With OpenLDAP
Using OpenFire With OpenLDAPUsing OpenFire With OpenLDAP
Using OpenFire With OpenLDAPDashamir Hoxha
 
Connecting your Python App to OpenERP through OOOP
Connecting your Python App to OpenERP through OOOPConnecting your Python App to OpenERP through OOOP
Connecting your Python App to OpenERP through OOOPraimonesteve
 
CVJ531: Intro to MySQL
CVJ531: Intro to MySQLCVJ531: Intro to MySQL
CVJ531: Intro to MySQLClay Ewing
 
Hacking Your Way To Better Security
Hacking Your Way To Better SecurityHacking Your Way To Better Security
Hacking Your Way To Better SecurityColin O'Dell
 
Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016Colin O'Dell
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Colin O'Dell
 
Findability Day 2014 Neo4j how graph data boost your insights
Findability Day 2014 Neo4j how graph data boost your insightsFindability Day 2014 Neo4j how graph data boost your insights
Findability Day 2014 Neo4j how graph data boost your insightsFindwise
 
Proposed redesign for Facebook privacy settings
Proposed redesign for Facebook privacy settingsProposed redesign for Facebook privacy settings
Proposed redesign for Facebook privacy settingsCatriona Cornett
 
Build restful ap is with python and flask
Build restful ap is with python and flaskBuild restful ap is with python and flask
Build restful ap is with python and flaskJeetendra singh
 
Using Trust in Recommender Systems: an experimental analysis
Using Trust in Recommender Systems:  an experimental analysisUsing Trust in Recommender Systems:  an experimental analysis
Using Trust in Recommender Systems: an experimental analysisPaolo Massa
 
[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)NAVER D2
 

Semelhante a NoSQL Taiwan #6 Redis Data Structure (20)

BDD de fuera a dentro
BDD de fuera a dentroBDD de fuera a dentro
BDD de fuera a dentro
 
Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016
 
Development of Twitter Application #5 - Users
Development of Twitter Application #5 - UsersDevelopment of Twitter Application #5 - Users
Development of Twitter Application #5 - Users
 
Using OpenFire With OpenLDAP
Using OpenFire With OpenLDAPUsing OpenFire With OpenLDAP
Using OpenFire With OpenLDAP
 
Sql injection
Sql injectionSql injection
Sql injection
 
Connecting your Python App to OpenERP through OOOP
Connecting your Python App to OpenERP through OOOPConnecting your Python App to OpenERP through OOOP
Connecting your Python App to OpenERP through OOOP
 
Keynote @iSWAG2015
Keynote @iSWAG2015Keynote @iSWAG2015
Keynote @iSWAG2015
 
CVJ531: Intro to MySQL
CVJ531: Intro to MySQLCVJ531: Intro to MySQL
CVJ531: Intro to MySQL
 
SOLID Ruby, SOLID Rails
SOLID Ruby, SOLID RailsSOLID Ruby, SOLID Rails
SOLID Ruby, SOLID Rails
 
Hacking Your Way To Better Security
Hacking Your Way To Better SecurityHacking Your Way To Better Security
Hacking Your Way To Better Security
 
Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016Hacking Your Way to Better Security - PHP South Africa 2016
Hacking Your Way to Better Security - PHP South Africa 2016
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
 
Findability Day 2014 Neo4j how graph data boost your insights
Findability Day 2014 Neo4j how graph data boost your insightsFindability Day 2014 Neo4j how graph data boost your insights
Findability Day 2014 Neo4j how graph data boost your insights
 
Proposed redesign for Facebook privacy settings
Proposed redesign for Facebook privacy settingsProposed redesign for Facebook privacy settings
Proposed redesign for Facebook privacy settings
 
Twitter
TwitterTwitter
Twitter
 
Build restful ap is with python and flask
Build restful ap is with python and flaskBuild restful ap is with python and flask
Build restful ap is with python and flask
 
Using Trust in Recommender Systems: an experimental analysis
Using Trust in Recommender Systems:  an experimental analysisUsing Trust in Recommender Systems:  an experimental analysis
Using Trust in Recommender Systems: an experimental analysis
 
Elixir + Neo4j
Elixir + Neo4jElixir + Neo4j
Elixir + Neo4j
 
[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)
 
SQL Injection
SQL InjectionSQL Injection
SQL Injection
 

Último

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Último (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

NoSQL Taiwan #6 Redis Data Structure

  • 1. NoSQL Taiwan #6 Redis Data Structure
  • 2. About Me • Felix Lin • hellolucky • Rails developer • http://twitter.com/hellolucky123 • http://blog.hellolucky.info
  • 3. This talk will tell you ...... • How to build a simple social website by using SQL • What's the performance issue about SQL • How to use the data structures of Redis • How to solve the performance issue of SQL by using Redis • Why I love Redis so much
  • 4. This talk won’t tell you ...... • How fast Redis is • How to Use Lua script in Redis • How to calculate memory usage • How to administrate Redis • The best practice of Redis
  • 7. User UserFollowRelation
  • 8. User UserFollowRelation
  • 9. Follow User UserFollowRelation
  • 10. Follow User UserFollowRelation Follower
  • 11. Follow User UserFollowRelation Follower Feed
  • 12. Follow User UserFollowRelation Follower Feed
  • 14. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 16. About Relation # Set Following User INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 2) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 3) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 4) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 5)
  • 17. About Relation # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 18. About Relation UserFollowRelation Records > 1000000 # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 19. About Relation UserFollowRelation Records > 1000000 # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1 Slow!
  • 20. About Relation # Find Following User Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 21. About Relation UserFollowRelation Records > # Find Following User Count 1000000 SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 22. About Relation UserFollowRelation Records > # Find Following User Count 1000000 SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1 Slow!
  • 23. About Relation # Find User1's Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find User1'2 Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 2 # The Same Following Users User1's Following Users & User2's Following Users
  • 24. About Relation # Find User1's Following Users Slow + Slow SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find User1'2 Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 2 # The Same Following Users User1's Following Users & User2's Following Users
  • 25. About Relation # Find User1's Following Users Slow + Slow SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find User1'2 Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 2 # The Same Following Users User1's Following Users & User2's Following Users = Very Slow!
  • 27. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 28. About Feed BEGIN # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1 COMMIT
  • 29. About Feed BEGIN Transaction # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1 COMMIT
  • 30. About Feed BEGIN Transaction # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1 COMMIT Slow!
  • 31. About Feed # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Following User's Feeds SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')
  • 32. About Feed A Little Data # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Following User's Feeds SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')
  • 33. About Feed A Little Data # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Following User's Feeds SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5') Very Slow!
  • 35. About Others # Find Top 10 Of The Talkiest User SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10 # Find Top 10 Of The Newest Feeds SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 37. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 38. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 39. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User Strings SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 Strings are the most basic kind of Redis value. Redis Strings are binary # Update User UPDATE `users` SET `name` = string can contain any kind of data, for safe, this means that a Redis 'hellolucky123' WHERE `users`.`id` = 1 # DeleteaUser image or a serialized Ruby object. instance JPEG - http://redis.io/topics/data-types DELETE FROM `users` WHERE `users`.`id` = 1
  • 40. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hellolucky set user:1:name set user:1:feeds_count 0 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 set user:1:created_at 1350906890 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 41. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hellolucky set user:1:name set user:1:feeds_count 0 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 set user:1:created_at 1350906890 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 42. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User Key SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hellolucky set user:1:name set user:1:feeds_count 0 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 set user:1:created_at 1350906890 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 43. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User Value SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hellolucky set user:1:name set user:1:feeds_count 0 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 set user:1:created_at 1350906890 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 44. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User Hash is better UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 45. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 Hashes # Update User Redis Hashes are maps between string fields and string values, so they UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 are the perfect data type to represent objects. # Delete User - http://redis.io/topics/data-types DELETE FROM `users` WHERE `users`.`id` = 1
  • 46. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 47. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 48. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* KeyFROM `users` WHERE `users`.`id` = 1 # Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 49. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User Fields SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 50. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 Values # Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 51. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 52. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERESets Sorted `users`.`id` = 1 # Update User Sorted Sets are, similarly to Redis Sets, non repeating Redis UPDATE `users` SET `name`difference is thatWHERE `users`.`id`a=Sorted collections of Strings. The = 'hellolucky123' every member of 1 Set is associated with score, that is used in order to take the sorted # Delete User DELETE FROM from the smallest to the greatest score. While members set ordered, `users` WHERE `users`.`id` = 1 are unique, scores may be repeated. - http://redis.io/topics/data-types
  • 53. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 54. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User feeds_count hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 55. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User user_id hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 56. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User For top 10 of the talkiest user hmset user:1 name hellolucky feeds_count 0 created_at 1350906890 UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 zadd global:user_ids 0 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 57. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 58. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User hmget user:1 name feeds_count created_at DELETE FROM `users` WHERE `users`.`id` = 1
  • 59. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User HGETALL is better DELETE FROM `users` WHERE `users`.`id` = 1
  • 60. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User hgetall user:1 DELETE FROM `users` WHERE `users`.`id` = 1
  • 61. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 62. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES hset user:1 name hellolucky123 ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 63. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) # Find User SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 64. About User # Create User INSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES ('hellolucky', 0, 1350906890) delFind User # user:1 SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 # Update User UPDATE `users` SET `name` = 'hellolucky123' WHERE `users`.`id` = 1 # Delete User DELETE FROM `users` WHERE `users`.`id` = 1
  • 65. About Relation # Set Following User INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 2) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 3) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 4) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 5)
  • 66. About Relation # Set Following User INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 2) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 3) Sets INSERT are the most basic kind of Redis value. Redis Strings are binary Strings INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 4) this means that a Redis string can contain any kind of data, for safe, instance a JPEG image or a serialized Ruby object. INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 5) - http://redis.io/topics/data-types
  • 67. About Relation # Set Following User INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 2) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 3) INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES sadd user:1:follow_ids 2 (1, 4) user:2:follower_ids 1 sadd INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES (1, 5)
  • 68. About Relation # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 69. About Relation # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 70. About Relation # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE smembers user:1:follow_ids `user_follow_relations`.`follow_id` = 1
  • 71. About Relation # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 72. About Relation # Find Following User Ids smembers user:1:follower_ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 73. About Relation # Find Following User Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 74. About Relation # Find Following User Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 75. About Relation # Find Following User Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = scard user:1:follow_ids `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 76. About Relation # Find Following User Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 77. About Relation # Find Following User Count scard user:1:follower_ids `users` INNER JOIN SELECT COUNT(*) FROM `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 78. About Relation # Find User1's Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find User1'2 Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 2 # The Same Following Users User1's Following Users & User2's Following Users
  • 79. About Relation # Find User1's Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 sinter user:1:follow_ids user:2:follow_ids # Find User1'2 Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 2 # The Same Following Users User1's Following Users & User2's Following Users
  • 80. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 81. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 82. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 rpush global:feed_ids 1
  • 83. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 rpush global:feed_ids 1
  • 84. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) Store feed # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 rpush global:feed_ids 1
  • 85. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 rpush global:feed_ids 1
  • 86. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter Push feed id to UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + followers hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 rpush global:feed_ids 1
  • 87. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter I’m lazy to list, it UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + should more..... hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 rpush global:feed_ids 1
  • 88. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 rpush global:feed_ids 1
  • 89. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter Lists UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE Lists are simply lists of strings, sorted by insertion order. It is Redis `users`.`id` = 1 possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list. - http://redis.io/topics/data-types
  • 90. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + hmset feed:1 user_id=11content description created_at 1350906890 1 WHERE `users`.`id` sadd user:2:follow:feed_ids 1 For top 10 of the newest feeds rpush global:feed_ids 1
  • 91. About Feed # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 92. About Feed hincrby user:1 feeds_count 1 zincrby global:user_ids 1 1 # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 93. About Feed Update counter hincrby user:1 feeds_count 1 zincrby global:user_ids 1 1 # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 94. About Feed hincrby user:1 feeds_count 1 zincrby global:user_ids 1 1 # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 95. About Feed For top 10 of the talkiest user hincrby user:1 feeds_count 1 zincrby global:user_ids 1 1 # Create Feed INSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1, 'description', 1350906890) # Update Counter UPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + 1 WHERE `users`.`id` = 1
  • 96. About Feed # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Following User's Feeds SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5')
  • 97. About Feed # Find Following User Ids SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Following User's Feeds SELECT `feeds`.* FROM `feeds` WHERE user_id in ('2,3,4,5') smembers user:1:follow:feed_ids
  • 98. About Others # Find Top 10 Of The Talkiest User SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10 # Find Top 10 Of The Newest Feeds SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 99. About Others # Find Top 10 Of The Talkiest User SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10 # Find Top 10 Of The Newest Feeds SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 100. About Others # Find Top 10 Of The Talkiest User SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10 # Find Top 10 Of The Newest Feeds SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10 zrevrange global:user_ids 0 9
  • 101. About Others # Find Top 10 Of The Talkiest User SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10 # Find Top 10 Of The Newest Feeds SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 102. About Others lrange global:feed_ids 0 9 # Find Top 10 Of The Talkiest User SELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10 # Find Top 10 Of The Newest Feeds SELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 104. MySQL + Redis = Lego
  • 105. QA

Notas do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n