面試突擊 | Redis 如何從海量數據中查詢出某一個 Key?附視頻


1 考察知識點

本題考察的知識點有以下幾個:

  1. Keys 和 Scan 的區別
  2. Keys 查詢的缺點
  3. Scan 如何使用?
  4. Scan 查詢的特點

2 解答思路

  1. Keys 查詢存在的問題
  2. Scan 的使用
  3. Scan 的特點

3 Keys 使用相關

1)Keys 用法如下

2)Keys 存在的問題

  1. 此命令沒有分頁功能,我們只能一次性查詢出所有符合條件的 key 值,如果查詢結果非常巨大,那么得到的輸出信息也會非常多;
  2. keys 命令是遍歷查詢,因此它的查詢時間復雜度是 o(n),所以數據量越大查詢時間就越長。

4 Scan 使用相關

我們先來模擬海量數據,使用 Pipeline 添加 10w 條數據,Java 代碼實現如下:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import utils.JedisUtils;

public class ScanExample {
    public static void main(String[] args) {
        // 添加 10w 條數據
        initData();
    }
    public static void initData(){
        Jedis jedis = JedisUtils.getJedis();
        Pipeline pipe = jedis.pipelined();
        for (int i = 1; i < 100001; i++) {
            pipe.set("user_token_" + i, "id" + i);
        }
        // 執行命令
        pipe.sync();
        System.out.println("數據插入完成");
    }
}

我們來查詢用戶 id 為 9999* 的數據,Scan 命令使用如下:

127.0.0.1:6379> scan 0 match user_token_9999* count 10000
1) "127064"
2) 1) "user_token_99997"
127.0.0.1:6379> scan 127064 match user_token_9999* count 10000
1) "1740"
2) 1) "user_token_9999"
127.0.0.1:6379> scan 1740 match user_token_9999* count 10000
1) "21298"
2) 1) "user_token_99996"
127.0.0.1:6379> scan 21298 match user_token_9999* count 10000
1) "65382"
2) (empty list or set)
127.0.0.1:6379> scan 65382 match user_token_9999* count 10000
1) "78081"
2) 1) "user_token_99998"
   2) "user_token_99992"
127.0.0.1:6379> scan 78081 match user_token_9999* count 10000
1) "3993"
2) 1) "user_token_99994"
   2) "user_token_99993"
127.0.0.1:6379> scan 3993 match user_token_9999* count 10000
1) "13773"
2) 1) "user_token_99995"
127.0.0.1:6379> scan 13773 match user_token_9999* count 10000
1) "47923"
2) (empty list or set)
127.0.0.1:6379> scan 47923 match user_token_9999* count 10000
1) "59751"
2) 1) "user_token_99990"
   2) "user_token_99991"
   3) "user_token_99999"
127.0.0.1:6379> scan 59751 match user_token_9999* count 10000
1) "0"
2) (empty list or set)

從以上的執行結果,我們看出兩個問題:

  1. 查詢的結果為空,但游標值不為 0,表示遍歷還沒結束;
  2. 設置的是 count 10000,但每次返回的數量都不是 10000,且不固定,這是因為 count 只是限定服務器單次遍歷的字典槽位數量 (約等於),而不是規定返回結果的 count 值。

相關語法:scan cursor [MATCH pattern] [COUNT count]

其中:

  • cursor:光標位置,整數值,從 0 開始,到 0 結束,查詢結果是空,但游標值不為 0,表示遍歷還沒結束;
  • match pattern:正則匹配字段;
  • count:限定服務器單次遍歷的字典槽位數量 (約等於),只是對增量式迭代命令的一種提示 (hint),並不是查詢結果返回的最大數量,它的默認值是 10。

5 Scan 代碼實戰

本文我們使用 Java 代碼來實現 Scan 的查詢功能,代碼如下:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import utils.JedisUtils;

public class ScanExample {
    public static void main(String[] args) {
        Jedis jedis = JedisUtils.getJedis();
        // 定義 match 和 count 參數
        ScanParams params = new ScanParams();
        params.count(10000);
        params.match("user_token_9999*");
        // 游標
        String cursor = "0";
        while (true) {
            ScanResult<String> res = jedis.scan(cursor, params);
            if (res.getCursor().equals("0")) {
                // 表示最后一條
                break;
            }
            cursor = res.getCursor(); // 設置游標
            for (String item : res.getResult()) {
                // 打印查詢結果
                System.out.println("查詢結果:" + item);
            }
        }
    }
}

以上程序執行結果如下:

查詢結果:user_token_99997

查詢結果:user_token_9999

查詢結果:user_token_99996

查詢結果:user_token_99998

查詢結果:user_token_99992

查詢結果:user_token_99994

查詢結果:user_token_99993

查詢結果:user_token_99995

查詢結果:user_token_99990

查詢結果:user_token_99991

查詢結果:user_token_99999

6 總結

通過本文我們了解到,Redis 中如果要在海量的數據數據中,查詢某個數據應該使用 Scan,Scan 具有以下特征:

  1. Scan 可以實現 keys 的匹配功能;
  2. Scan 是通過游標進行查詢的不會導致 Redis 假死;
  3. Scan 提供了 count 參數,可以規定遍歷的數量;
  4. Scan 會把游標返回給客戶端,用戶客戶端繼續遍歷查詢;
  5. Scan 返回的結果可能會有重復數據,需要客戶端去重;
  6. 單次返回空值且游標不為 0,說明遍歷還沒結束;
  7. Scan 可以保證在開始檢索之前,被刪除的元素一定不會被查詢出來;
  8. 在迭代過程中如果有元素被修改, Scan 不保證能查詢出相關的元素。

7 視頻版

視頻版:https://www.bilibili.com/video/av88076985/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM