选择速度非常慢但是uniq键,有没有办法改进?

时间:2017-01-28 06:57:34

标签: mysql mariadb

根据我的授权,我通过他的社交ID查找用户:

select * from users where yandex_id = 65250508;

结果非常糟糕:1排(11.25秒)

此表的计数:

select count(id) from users;
+-----------+
| count(id) |
+-----------+
|   1852446 |
+-----------+

还有我的疑问解释:

explain select * from users where yandex_id = 65250508;
+------+-------------+-------+------+---------------------------------+------+---------+------+---------+-------------+
| id   | select_type | table | type | possible_keys                   | key  | key_len | ref  | rows    | Extra       |
+------+-------------+-------+------+---------------------------------+------+---------+------+---------+-------------+
|    1 | SIMPLE      | users | ALL  | UNIQ_1483A5E988FDD79D,yandex_id | NULL | NULL    | NULL | 1820017 | Using where |
+------+-------------+-------+------+---------------------------------+------+---------+------+---------+-------------+

并描述表:

describe users;
+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| ts          | int(10) unsigned    | NO   | MUL | NULL    |                |
| last_ts     | int(10) unsigned    | NO   | MUL | NULL    |                |
| last_mail   | int(10) unsigned    | NO   |     | NULL    |                |
| photo       | varchar(32)         | YES  |     | NULL    |                |
| name        | varchar(48)         | YES  |     | NULL    |                |
| email       | varchar(48)         | YES  | UNI | NULL    |                |
| state       | smallint(6)         | NO   | MUL | NULL    |                |
| ip          | bigint(20) unsigned | NO   |     | NULL    |                |
| gender      | varchar(1)          | NO   |     | NULL    |                |
| facebook_id | varchar(64)         | YES  | UNI | NULL    |                |
| mailru_id   | varchar(64)         | YES  | UNI | NULL    |                |
| vk_id       | varchar(64)         | YES  | UNI | NULL    |                |
| yandex_id   | varchar(64)         | YES  | UNI | NULL    |                |
| google_id   | varchar(64)         | YES  | UNI | NULL    |                |
| roles       | longtext            | YES  |     | NULL    |                |
| is_active   | tinyint(1)          | NO   | MUL | 1       |                |
+-------------+---------------------+------+-----+---------+----------------+
17 rows in set (0.00 sec)

2 个答案:

答案 0 :(得分:0)

明确地在查询中尝试使用USE INDEX(update_index)。

有时,优化器在选择索引时做出了错误的选择,因为查询变慢了。

答案 1 :(得分:0)

我已经解决了我的问题。所以,问题在于我在使用整数值的地方,但我的字段输入定义为varchar,所以当我将搜索ID更改为字符串时,它开始工作完美

MariaDB [hrabr]> select * from users force index(yandex_id) where yandex_id = 65250508;
1 row in set (13.81 sec)

并使用字符串:

MariaDB [hrabr]> select * from users force index(yandex_id) where yandex_id = '65250508';
1 row in set (0.00 sec)

我希望它会帮助别人!