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
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
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
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