简体   繁体   English

为什么这个MySQL查询没有索引会更快?

[英]Why this MySQL query is faster without index?

I am having trouble understanding why my MySQL query runs faster when I change it to use no indexes. 我无法理解为什么我将MySQL查询更改为不使用索引时运行得更快。

My first query takes 0.236s to run: 我的第一个查询需要0.236s才能运行:

SELECT 
    u.id, 
    u.email, 
    CONCAT(u.first_name, ' ', u.last_name) AS u_name
FROM
    tbl_user AS u
WHERE
    u.site_id=1
    AND u.role_id=5
    AND u.removed_date IS NULL
ORDER BY 
    u_name ASC 
LIMIT 0, 20

My second query takes 0.147s to run: 我的第二个查询需要0.147s才能运行:

SELECT 
    u.id, 
    u.email, 
    CONCAT(u.first_name, ' ', u.last_name) AS u_name
FROM
    tbl_user AS u USE INDEX () 
WHERE
    u.site_id=1
    AND u.role_id=5
    AND u.removed_date IS NULL
ORDER BY 
    u_name ASC 
LIMIT 0, 20

I have a unique index named idx_1 on columns site_id, role_id and email. 我在site_id,role_id和email列上有一个名为idx_1的唯一索引。

The EXPLAIN statement tells that it will use idx_1. EXPLAIN语句告知它将使用idx_1。

+----+-------------+-------+------+-------------------------------------+-------+---------+-------------+-------+----------------------------------------------------+
| id | select_type | table | type | possible_keys                       | key   | key_len | ref         | rows  | Extra                                              |
+----+-------------+-------+------+-------------------------------------+-------+---------+-------------+-------+----------------------------------------------------+
|  1 | SIMPLE      | u     | ref  | idx_1,idx_import,tbl_user_ibfk_2    | idx_1 | 8       | const,const | 55006 | Using index condition; Using where; Using filesort |
+----+-------------+-------+------+-------------------------------------+-------+---------+-------------+-------+----------------------------------------------------+

The table has about 110000 records. 该表有大约110000条记录。

Thanks 谢谢

UPDATE 1: 更新1:

Below is the list of my table indexes: 以下是我的表索引列表:

Name                Fields                      Type    Method
---------------------------------------------------------------
idx_1               site_id, role_id, email     Unique  BTREE
idx_import          site_id, external_id        Unique  BTREE
tbl_user_ibfk_2     role_id                     Normal  BTREE
tbl_user_ibfk_3     country_id                  Normal  BTREE
tbl_user_ibfk_4     preferred_country_id        Normal  BTREE
---------------------------------------------------------------

You haven't specified which mysql you are using. 您尚未指定要使用的mysql。 Does this explain it 这说明了吗

Prior to MySQL 5.1.17, USE INDEX, IGNORE INDEX, and FORCE INDEX affect only which indexes are used when MySQL decides how to find rows in the table and how to process joins. 在MySQL 5.1.17之前,USE INDEX,IGNORE INDEX和FORCE INDEX仅影响在MySQL决定如何在表中查找行以及如何处理联接时使用哪些索引。 They do not affect whether an index is used when resolving an ORDER BY or GROUP BY clause. 它们不影响解析ORDER BY或GROUP BY子句时是否使用索引。

from https://dev.mysql.com/doc/refman/5.1/en/index-hints.html 来自https://dev.mysql.com/doc/refman/5.1/en/index-hints.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM