Mysql数据库优化-null值优化

1 在oracle数据库中null值是不被索引存储的,需要使用nvl函数进行优化null,

2 在mysql中null值可以被索引保存,语句可以正常使用索引。

15:16:  [ytt]> create index idx2 on t(password_last_changed);
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

15:16:  [ytt]> show index from t;
+-------+------------+----------+--------------+-----------------------+-----------+-------------+----
------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name           | Collation | Cardinality | Sub
_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------+--------------+-----------------------+-----------+-------------+----
------+--------+------+------------+---------+---------------+---------+------------+
| t     |          1 | idx1     |            1 | User                  | A         |           9 |
 NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| t     |          1 | idx2     |            1 | password_last_changed | A         |           8 |
 NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+-------+------------+----------+--------------+-----------------------+-----------+-------------+----
------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.02 sec)

15:16:  [ytt]> show index from t\G
*************************** 1. row ***************************
        Table: t
   Non_unique: 1
     Key_name: idx1
 Seq_in_index: 1
  Column_name: User
    Collation: A
  Cardinality: 9
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
*************************** 2. row ***************************
        Table: t
   Non_unique: 1
     Key_name: idx2
 Seq_in_index: 1
  Column_name: password_last_changed
    Collation: A
  Cardinality: 8
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
2 rows in set (0.00 sec)

15:17:  [ytt]> select password_last_changed from t;
+-----------------------+
| password_last_changed |
+-----------------------+
| NULL                  |
| 2021-06-22 14:09:04   |
| 2021-06-22 14:09:04   |
| 2021-06-22 14:09:04   |
| 2022-07-28 15:39:08   |
| 2022-08-24 16:50:49   |
| 2022-08-24 16:51:04   |
| 2022-08-25 10:54:30   |
| 2022-09-29 11:10:51   |
| 2023-02-07 15:48:26   |
+-----------------------+
10 rows in set (0.00 sec)

15:17:  [ytt]> explain select  * from t where password_last_changed is null;
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+------
----+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref   | rows | filte
red | Extra                 |
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+------
----+-----------------------+
|  1 | SIMPLE      | t     | NULL       | ref  | idx2          | idx2 | 5       | const |    1 |   100
.00 | Using index condition |
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+------
----+-----------------------+
1 row in set, 1 warning (0.01 sec)

15:17:  [ytt]>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值