教你巧用mysql位运算解决多选值存储的问题

目录
  • 一.问题场景
  • 二. 场景分析
    • 1.多字段存储
    • 2.单字段拼接
  • 三.巧用位运算
    • 1.概述
    • 2.sql查询
    • 3.java解析与计算
    • 4.总结
  • 附mysql的支持6种位运算
    • 总结

      一.问题场景

      工作中经常遇到多选值存储问题,例如:用户有多种认证方式,密码认证、短信认证、扫码认证等,一个用户可能只开启了其中某几种认证方式。

      二. 场景分析

      比较容易理解的两种实现方式,多字段存储、单个字段拼接存储。

      1.多字段存储

      每种认证方式用一个字段存储,0表示未开启,1表示已开启。

      缺点:每增加一种认证方式都需要添加一个表字段,扩展性差。

      2.单字段拼接

      单字段存储,已开启的认证方式用逗号(或其他分割符)拼接。例如:开始了密码认证和短信认证,则存储为:密码认证,短信认证。

      缺点:不利于查询,需要使用模糊查询,搞不好会影响性能。

      三.巧用位运算

      1.概述

      参考linux权限控制思路,将每种认证方式对应到二进制位中,例如:密码认证–10000000,短信认证–01000000,扫码认证–00100000,然后将其转换成10进制,密码认证–1, 短信认证–2,扫码认证–4。mysql存储时使用单字段(auth_method)int类型存储,如果开启了多种认证方式将多种认证方式对应的枚举数值相加后存储,例如开启了密码认证和短信认证,则存储为3(1+2)。

      2.sql查询

      ## 例1:判断用户是否开启了密码认证--1 (满足条件时返回查询结果,没有满足条件时返回为空)
      select * from user where auth_method & 1;
      
      ## 例2:判断用户是否开启了密码认证 + 短信认证 (1+2)
      select * from user where auth_method & 3;
      
      ## 例2:判断用户是否开启了密码认证 + 短信认证 + 扫码认证 (1+2+4)
      select * from user where auth_method & 7;
      

      3.java解析与计算

      import com.google.common.collect.lists;
      import lombok.getter;
      import org.springframework.util.collectionutils;
      
      import java.util.arrays;
      import java.util.list;
      
      @getter
      public enum authmethodenum {
      
          password(1, "密码认证"),
          sms(2, "短信认证"),
          qr_code(4, "扫码认证");
      
          private integer method;
      
          private string name;
      
          authmethodenum(integer method, string name) {
              this.method = method;
              this.name = name;
          }
      
          /**
           * 将mysql存储值解析成多种认证方式
           * @param method
           * @return
           */
          public static list<integer> parseauthmethod(integer method) {
              list<integer> list = lists.newarraylist();
              if (null == method) {
                  return list;
              }
              authmethodenum[] arr = authmethodenum.values();
              // 需要先将method从大到小排序
              arrays.sort(arr, (o1, o2) -> {
                  if (o1.method > o2.method) {
                      return -1;
                  } else {
                      return 0;
                  }
              });
              for (authmethodenum e : arr) {
                  if (method >= e.method) {
                      list.add(e.method);
                      method = method - e.method;
                  }
              }
              return list;
          }
      
          /**
           * 将任意种认证方式计算后得到存储值
           * @param methods
           * @return
           */
          public static integer calculateauthmethod(list<integer> methods) {
              if (collectionutils.isempty(methods)) {
                  return 0;
              }
              return methods.stream().maptoint(p -> p).sum();
          }
      
          public static void main(string[] args) {
              system.out.println(parseauthmethod(8));
          }
      }
      

      4.总结

      通过位运算的转换,实现了单个字段存储不同的认证状态,增加一个新的认证方式时只需要添加一个枚举值。不仅可以节省存储空间,大大增加了可扩展性,对性能几乎没有影响。

      附mysql的支持6种位运算

      符号 含义
      a|b 位或
      a&b 位与
      a^b 位异或
      ~a 位取反
      a<<b 位左移
      a>>b 位右移

      总结

      到此这篇关于教你巧用mysql位运算解决多选值存储问题的文章就介绍到这了,更多相关mysql位运算解决多选值存储内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

      (0)
      上一篇 2022年3月21日
      下一篇 2022年3月21日

      相关推荐