我使用SQL已有很长时间了,但这是我第一次看到它。
我有一个表格联系人:
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| client_id | varchar(191) | YES | MUL | NULL | |
| client_status | int(11) | YES | | NULL | |
| lastname | varchar(191) | YES | | NULL | |
| firstname | varchar(191) | YES | | NULL | |
| patronymic | varchar(191) | YES | | NULL | |
| date_of_birth | date | YES | MUL | NULL | |
| sex | int(11) | YES | | NULL | |
| state | int(11) | YES | MUL | NULL | |
| phone_mobile | varchar(191) | YES | MUL | NULL | |
| phone_home | varchar(191) | YES | | NULL | |
| adr_city_type | varchar(191) | YES | | NULL | |
| adr_city | varchar(191) | YES | | NULL | |
| adr_apt | varchar(191) | YES | | NULL | |
| adr_street_num | varchar(191) | YES | | NULL | |
| adr_street | varchar(191) | YES | | NULL | |
| adr_street_type | varchar(191) | YES | | NULL | |
| adr_district | varchar(191) | YES | | NULL | |
| adr_postcode | varchar(191) | YES | | NULL | |
| email | varchar(191) | YES | MUL | NULL | |
| comment | text | YES | | NULL | |
+------------------+--------------+------+-----+---------+-------+
我有一个简单的查询:
select client_id, lastname, firstname, phone_mobile, email
from contacts
where `client_id`="8011000000000005263"
limit 1;
执行它时,得到以下结果: 空集(0.00秒)
我知道此标识符是一个数据库,因此我决定尝试使用该查询,请参见下面的结果。
2。
mysql> select client_id, lastname, firstname, phone_mobile, email from contacts where `client_id` like "%8011000000000005263%" limit 1;
Empty set (1.74 sec)
3。
mysql>从client_id
= 8011000000000005263限制1的联系人中选择client_id,姓氏,名字,phone_mobile和电子邮件;
+-------------------+----------------------+-----------+---------------+-------+
| client_id | lastname | firstname | phone_mobile | email |
+-------------------+----------------------+-----------+---------------+-------+
| 8011000000000004655| OSTROVKAYA | YANA | +111111111111| NULL |
+-------------------+----------------------+-----------+---------------+-------+
1 row in set (0.68 sec)
4。
mysql> select client_id, lastname, firstname, phone_mobile, email from
contacts where `client_id`= 8011000000000005263;
234 rows in set (0.83 sec) - WTF ???
5。
mysql> select client_id, lastname, firstname, phone_mobile, email from
contacts use index (client_id) where `client_id`=8011000000000005263;
234 rows in set (0.90 sec) - WTF ???
6。
mysql> select client_id, lastname, firstname, phone_mobile, email from contacts use index (client_id) where client_id in (8011000000000005263) group by id_client;
234 rows in set (1.94 sec) - WTF ???
我想为上一个请求提供一个解释:
mysql> explain select client_id, lastname, firstname, phone_mobile, email from contacts use index (client_id) where client_id in (8011000000000005263) group by id_client;
+----+-------------+--------------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | contacts | NULL | index | client_id | client_id | 576 | NULL | 1183745 | 10.00 | Using where |
+----+-------------+--------------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
1 row in set, 3 warnings (0.00 sec)
我不明白为什么简单查询无法正确执行。以及如何获得正确的结果。
此外,此请求位于MySQL服务器上(((
当我们要与辅助表进行通信时,请求将挂起20-30分钟。
要完全了解正在发生的事情,我将提供表索引:
+--------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| contacts | 1 | id | 1 | id | A | 1177634 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | client_id | 1 | client_id | A | 1136884 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 1 | email | A | 194844 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 2 | phone_mobile | A | 1165372 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 3 | date_of_birth | A | 1081845 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 4 | state | A | 1148369 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | email | 1 | email | A | 197052 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | phone_mobile | 1 | phone_mobile | A | 1127414 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | state | 1 | state | A | 2 | NULL | NULL | YES | BTREE | | |
+--------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
9 rows in set (0.02 sec)
请,没有经验的开发人员不能向我解释这种语言行为的现象。
非常感谢!所有干净的代码:)