Mysql没有在查询中使用索引

时间:2014-05-23 12:52:37

标签: mysql

这是我的解释结果。

+----+-------------+----------------+--------+-------------------------------------------------------+-------------+---------+-------------------------------------+------+----------------------------------------------------+
| id | select_type | table          | type   | possible_keys                                         | key         | key_len | ref                                 | rows | Extra                       |
+----+-------------+----------------+--------+-------------------------------------------------------+-------------+---------+-------------------------------------+------+----------------------------------------------------+
|  1 | SIMPLE      | client_address | ALL    | client_id                                             | NULL        | NULL    | NULL                                | 1619 | Using where; Using temporary; Using filesort       |
|  1 | SIMPLE      | clients        | eq_ref | PRIMARY,contract_id,contract_id_2                     | PRIMARY     | 3       | defenselaw.client_address.client_id |    1 | Using where                       |
|  1 | SIMPLE      | user_infos     | eq_ref | user_id                                               | user_id     | 3       | defenselaw.clients.client_rep_id    |    1 | Using where                       |
|  1 | SIMPLE      | users          | eq_ref | PRIMARY                                               | PRIMARY     | 2       | defenselaw.user_infos.user_id       |    1 | Using where; Using index                       |
|  1 | SIMPLE      | contracts      | eq_ref | PRIMARY                                               | PRIMARY     | 3       | defenselaw.clients.contract_id      |    1 | NULL                       |
|  1 | SIMPLE      | documents      | eq_ref | contract_id,contract_id_2,contract_id_3,contract_id_4 | contract_id | 3       | defenselaw.clients.contract_id      |    1 | Using where                       |
|  1 | SIMPLE      | client_info    | ref    | client_id,client_id_2,client_id_3,index5              | client_id   | 3       | defenselaw.client_address.client_id |    1 | Using where                       |
|  1 | SIMPLE      | location       | ALL    | PRIMARY                                               | NULL        | NULL    | NULL                                |    1 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+----------------+--------+-------------------------------------------------------+-------------+---------+-------------------------------------+------+----------------------------------------------------+

这是查询。

SELECT 
    contracts.contract_id,
    documents.documentKey,
    documents.document_name,
    documents.notify_accountant,
    clients.client_first_name,
    clients.client_middle_name,
    clients.client_last_name,
    clients.client_rep_id,
    client_address.client_street,
    client_address.client_apartment,
    client_address.client_city,
    client_address.client_state,
    client_address.client_zip,                    
    client_info.client_email,
    client_info.client_home_phone,
    client_info.client_work_phone,
    client_info.client_cell_phone,
    client_info.client_fax_number,
    contracts.contract_agreement_date,
    user_infos.user_first_name,
    user_infos.user_last_name,
    location.location
FROM 
    `contracts`
LEFT JOIN 
    `clients` 
        ON clients.contract_id = contracts.contract_id  
INNER JOIN 
    `client_address` 
        ON client_address.client_id = clients.client_id    
INNER JOIN 
    `client_info` 
        ON client_info.client_id = client_address.client_id
LEFT JOIN
    `documents`
        ON contracts.contract_id = documents.contract_id   
LEFT JOIN
    `user_infos`
        ON user_infos.user_id = clients.client_rep_id
                LEFT JOIN
                        `users` ON
                        users.user_id = user_infos.user_id
LEFT JOIN
        `location`
            ON location.location_id = user_infos.location_id  
 WHERE client_info.client_role = 'primary'
AND documents.archive = 0
ORDER BY
    documents.created_on DESC

有人可以向我解释为什么我的client_address上的索引没有被使用?因为我不知道该怎么做,我迷失在这里。查询的复杂性并不是很大。是的,stackoverflow,我理解帖子主要是代码。你还想要我什么?除了我的查询之外,我还能说除此之外的其他内容还有多少,而且这里的查询速度非常慢。

+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| client_address | CREATE TABLE `client_address` (
  `client_address_id` mediumint(12) NOT NULL AUTO_INCREMENT,
  `client_id` mediumint(12) NOT NULL,
  `client_street` varchar(40) COLLATE utf8_bin NOT NULL,
  `client_apartment` varchar(20) COLLATE utf8_bin NOT NULL,
  `client_city` varchar(80) COLLATE utf8_bin NOT NULL,
  `client_state` varchar(80) COLLATE utf8_bin NOT NULL,
  `client_zip` varchar(80) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`client_address_id`),
  KEY `client_id` (`client_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2167 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

0 个答案:

没有答案