ORIGINAL QUERY 4.5 Performance
SELECT DISTINCT p.pid FROM performance
p JOIN performance
ps ON KEYS “performanceStats/pid:” || p.pid JOIN user
u ON KEYS “user/userId:” || p.userId WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7” OR p.shareTarget = “public” OR (p.shareTarget = “userIds” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN p.shareUserIds) OR (p.shareTarget = “friend” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT friendIds FROM user
USE KEYS “friend/userId:” || p.userId)) OR (p.shareTarget = “follower” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT followerIds FROM user
USE KEYS “follow/userId:” || p.userId))) ORDER BY p.createdAt DESC LIMIT 50;
1.04ms (50 results)
Notes:
- query is significantly faster without any modifications
Separate Indexes
CREATE INDEX index_performance_pid ON performance
(pid) WHERE meta().id LIKE “performance/%”;
CREATE INDEX index_performance_userId ON performance
(userId) WHERE meta().id LIKE “performance/%”;
CREATE INDEX index_performance_isEnabled ON performance
(isEnabled) WHERE meta().id LIKE “performance/%”;
CREATE INDEX index_performance_isDeleted ON performance
(isDeleted) WHERE meta().id LIKE “performance/%”;
CREATE INDEX index_performance_expiredAt ON performance
(expiredAt) WHERE meta().id LIKE “performance/%”;
CREATE INDEX index_performance_createdAt ON performance
(createdAt) WHERE meta().id LIKE “performance/%”;
CREATE INDEX index_performance_shareTarget ON performance
(shareTarget) WHERE meta().id LIKE “performance/%”;
CREATE INDEX index_performance_shareUserIds ON performance
(shareUserIds) WHERE meta().id LIKE “performance/%”;
SELECT DISTINCT p.pid FROM performance
p JOIN performance
ps ON KEYS “performanceStats/pid:” || p.pid JOIN user
u ON KEYS “user/userId:” || p.userId WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7” OR p.shareTarget = “public” OR (p.shareTarget = “userIds” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN p.shareUserIds) OR (p.shareTarget = “friend” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT friendIds FROM user
USE KEYS “friend/userId:” || p.userId)) OR (p.shareTarget = “follower” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT followerIds FROM user
USE KEYS “follow/userId:” || p.userId))) ORDER BY p.createdAt DESC LIMIT 50;
1.07s (50 results)
Notes:
- not much of an impact with separate indexes on this one
Without Share Targets
SELECT DISTINCT p.pid FROM performance
p WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 LIMIT 50;
135ms (50 results)
With userId check
SELECT DISTINCT p.pid FROM performance
p WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7”) LIMIT 50;
130ms (50 results)
With public share target
SELECT DISTINCT p.pid FROM performance
p WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7” OR p.shareTarget = “public”) LIMIT 50;
173ms (50 results)
Notes:
- each string comparison adds quite a performance hit
With shared userIds share target
SELECT DISTINCT p.pid FROM performance
p WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7” OR p.shareTarget = “public” OR (p.shareTarget = “userIds” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN p.shareUserIds)) LIMIT 50;
180ms (50 results)
With friendIds share target
SELECT DISTINCT p.pid FROM performance
p WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7” OR p.shareTarget = “public” OR (p.shareTarget = “userIds” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN p.shareUserIds) OR (p.shareTarget = “friend” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT friendIds FROM user
USE KEYS “friend/userId:” || p.userId))) LIMIT 50;
600ms (50 results)
Notes:
- though significantly faster, sub-queries still add quite a performance hit, roughly 500ms even though they yield empty results
With All Share Targets (followerIds)
SELECT DISTINCT p.pid FROM performance
p WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7” OR p.shareTarget = “public” OR (p.shareTarget = “userIds” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN p.shareUserIds) OR (p.shareTarget = “friend” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT friendIds FROM user
USE KEYS “friend/userId:” || p.userId)) OR (p.shareTarget = “follower” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT followerIds FROM user
USE KEYS “follow/userId:” || p.userId))) LIMIT 50;
1.06s (50 results)
Notes:
- though significantly faster, sub-queries still add quite a performance hit, roughly 500ms even though they yield empty results
With Joins
SELECT DISTINCT p.pid FROM performance
p JOIN performance
ps ON KEYS “performanceStats/pid:” || p.pid JOIN user
u ON KEYS “user/userId:” || p.userId WHERE meta§.id LIKE “performance/%” AND p.pid IS NOT MISSING AND p.isEnabled = FALSE AND p.isDeleted = FALSE AND p.expiredAt > 1461345193451 AND p.createdAt >= 1460913193450 AND (p.userId = “04a43a75-65c3-4840-b250-d0da7bc13ff7” OR p.shareTarget = “public” OR (p.shareTarget = “userIds” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN p.shareUserIds) OR (p.shareTarget = “friend” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT friendIds FROM user
USE KEYS “friend/userId:” || p.userId)) OR (p.shareTarget = “follower” AND “04a43a75-65c3-4840-b250-d0da7bc13ff7” WITHIN (SELECT followerIds FROM user
USE KEYS “follow/userId:” || p.userId))) LIMIT 50;
1.04s (50 results)
Notes:
- joins have no impact on the performance
Share Target Sub Queries Only
SELECT followerIds FROM user
USE KEYS “follow/userId:04a43a75-65c3-4840-b250-d0da7bc13ff7”;
1ms (0 results)
SELECT friendIds FROM user
USE KEYS “friend/userId:04a43a75-65c3-4840-b250-d0da7bc13ff7”;
1ms (0 results)