找到一个表中但不存在于其他表中的记录数

时间:2018-02-23 20:27:28

标签: mysql

我有两张桌子,其中一张桌子有一个小柱子。当我离开加入这张桌子时,我得到的结果非常快。 但反过来说没有用。

 CREATE TABLE `userprofile_dtl` (
  `user_id` varchar(100) NOT NULL,
  `bankpassbook_doc` varchar(4) DEFAULT NULL,
  `aadhaar_no` tinyblob,
  PRIMARY KEY (`user_id`),
  KEY `idx_aadhaar_no_tinyblob` (`aadhaar_no`(16)) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 CREATE TABLE `commonformfields_dtl` (
  `app_id` varchar(25) NOT NULL,
  `user_id` varchar(100) DEFAULT NULL,
  `isParentsSeparated` varchar(3) DEFAULT NULL,
  PRIMARY KEY (`app_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 
  1. mysql如何使用tinyblob列上构建的完全不相关的密钥来比较user_id?

  2. 为什么它比使用primary的逻辑索引更快?

  3. 快速:

    mysql>  explain 
    SELECT count(*) 
    FROM `userprofile_dtl` as a 
    left join `commonformfields_dtl` as b 
      on a.user_id = b.user_id 
    where b.user_id is null;
    
    +----+-------------+-------+------------+-------+---------------+-------------------------+---------+---------------------+--------+----------+--------------------------+
    | id | select_type | table | partitions | type  | possible_keys | key                     | key_len | ref                 | rows   | filtered | Extra                    |
    +----+-------------+-------+------------+-------+---------------+-------------------------+---------+---------------------+--------+----------+--------------------------+
    |  1 | SIMPLE      | a     | NULL       | index | NULL          | idx_aadhaar_no_tinyblob | 19      | NULL                | 392608 |   100.00 | Using index              |
    |  1 | SIMPLE      | b     | NULL       | ref   | user_id       | user_id                 | 303     | portal.a.user_id |      1 |   100.00 | Using where; Using index |
    +----+-------------+-------+------------+-------+---------------+-------------------------+---------+---------------------+--------+----------+--------------------------+
    

    慢速:

    mysql> explain   
    SELECT count(*) 
    FROM  `commonformfields_dtl` as a 
    left join `userprofile_dtl` as b 
      on a.user_id = b.user_id 
    where b.user_id is null;
    
    +----+-------------+-------+------------+--------+---------------+---------+---------+---------------------+---------+----------+--------------------------------------+
    | id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref                 | rows    | filtered | Extra                                |
    +----+-------------+-------+------------+--------+---------------+---------+---------+---------------------+---------+----------+--------------------------------------+
    |  1 | SIMPLE      | a     | NULL       | index  | NULL          | user_id | 303     | NULL                | 2004994 |   100.00 | Using index                          |
    |  1 | SIMPLE      | b     | NULL       | eq_ref | PRIMARY       | PRIMARY | 302     |  portal.a.user_id |       1 |   100.00 | Using where; Not exists; Using index |
    +----+-------------+-------+------------+--------+---------------+---------+---------+---------------------+---------+----------+--------------------------------------+
    
    mysql> explain   
    SELECT count(*) 
    FROM  `commonformfields_dtl` as a 
    left join `userprofile_dtl` as b ignore index (primary) 
      on a.user_id = b.user_id 
    where b.user_id is null;
    
    +----+-------------+-------+------------+-------+---------------+-------------------------+---------+------+---------+----------+-----------------------------------------------------------------------------+
    | id | select_type | table | partitions | type  | possible_keys | key                     | key_len | ref  | rows    | filtered | Extra                                                                       |
    +----+-------------+-------+------------+-------+---------------+-------------------------+---------+------+---------+----------+-----------------------------------------------------------------------------+
    |  1 | SIMPLE      | a     | NULL       | index | NULL          | user_id                 | 303     | NULL | 2004994 |   100.00 | Using index                                                                 |
    |  1 | SIMPLE      | b     | NULL       | index | NULL          | idx_aadhaar_no_tinyblob | 19      | NULL |  392608 |    10.00 | Using where; Not exists; Using index; Using join buffer (Block Nested Loop) |
    +----+-------------+-------+------------+-------+---------------+-------------------------+---------+------+---------+----------+-----------------------------------------------------------------------------+
    

0 个答案:

没有答案