为什么这个查询在我的本地服务器上花了这么多时间而不是在生产中?

时间:2016-08-07 17:50:22

标签: mysql sql wamp

因此,这个查询需要11秒才能在我的本地服务器上执行,但在生产中几乎是inmedialy。

我不知道为什么

SELECT c.id, c.franquicia_id, f.name, CONCAT(c.name,' ',c.surname) 
       contacto, c.created, DATE_FORMAT(c.created,'%d-%m-%Y') fecha 
FROM franquicias f, contacts c where f.id = c.franquicia_id 
 and c.created = (
                   select max(c2.created) 
                   from contacts c2 
                   WHERE c2.franquicia_id = c.franquicia_id
                 ) 
 and f.contract_id=2 
ORDER BY created DESC 
limit 10

代码需要11秒,并且还尝试从phpmyadmin开始,也需要11秒

EXPLAIN

+----+--------------------+-------+------+----------------------------------+--------------------------+---------+-----------------------------+------+---------------------------------+
| id | select_type        | table | type | possible_keys                    | key                      | key_len | ref                         | rows | Extra                           |
+----+--------------------+-------+------+----------------------------------+--------------------------+---------+-----------------------------+------+---------------------------------+
| 1  | PRIMARY            | f     | ref  | PRIMARY,FK_franquicias_contracts | FK_franquicias_contracts | 5       | const                       | 94   | Using temporary; Using filesort |
+----+--------------------+-------+------+----------------------------------+--------------------------+---------+-----------------------------+------+---------------------------------+
| 1  | PRIMARY            | c     | ref  | FK_contacts_franquicias          | FK_contacts_franquicias  | 4       | admin_panel.f.id            | 73   | Using where                     |
+----+--------------------+-------+------+----------------------------------+--------------------------+---------+-----------------------------+------+---------------------------------+
| 2  | DEPENDENT SUBQUERY | c2    | ref  | FK_contacts_franquicias          | FK_contacts_franquicias  | 4       | admin_panel.c.franquicia_id | 73   | NULL                            |
+----+--------------------+-------+------+----------------------------------+--------------------------+---------+-----------------------------+------+---------------------------------+

数据库联系人:有175.000行 数据库合同:有5行 数据库franquicias:有1618

服务器:mysql Ver 15.1使用readline 5.1分发5.5.37-MariaDB,用于Linux(x86_64)

服务器:1GB RAM,AMD Quadcore

Local:mysql Ver 14.14 Distrib 5.6.17,for Win64(x86_64)

本地:16GB RAM I73820

0 个答案:

没有答案