【问题标题】:Extremely Slow Select Query on MySQLMySQL上非常慢的选择查询
【发布时间】:2015-03-18 09:46:43
【问题描述】:

我试图了解为什么选择查询在我的笔记本电脑上运行得非常快,而在服务器上却非常慢。查询需要1.388 秒在笔记本电脑上运行,而49.670 秒在服务器上运行。两种模式都是相同的,因为我已经从笔记本电脑导出了方案并将其导入服务器。两者都在 WAMP 2.0 上运行 MySQL 5.1.36。

SQL 转储

https://db.tt/4TvuOWbD

查询

SELECT form.id                                                                                     AS 'Form ID',
       DATE_FORMAT(form.created_on, '%d %b %Y')                                                    AS 'Created On - Date',
       DATE_FORMAT(form.created_on, '%h:%i %p')                                                    AS 'Created On - Time',
       department.name                                                                             AS 'Department',
       section.name                                                                                AS 'Section',
       reporting_to_1.id                                                                           AS 'Reporting To 1 - System ID',
       reporting_to_1.real_name                                                                    AS 'Reporting To 1 - Name',
       reporting_to_1_department.name                                                              AS 'Reporting To 1 - Department',
       reporting_to_1_section.name                                                                 AS 'Reporting To 1 - Section',
       CONVERT(IFNULL(reporting_to_2.id, '') USING utf8)                                           AS 'Reporting To 2 - System ID',
       IFNULL(reporting_to_2.real_name, '')                                                        AS 'Reporting To 2 - Name',
       IFNULL(reporting_to_2_department.name, '')                                                  AS 'Reporting To 2 - Department',
       IFNULL(reporting_to_2_section.name, '')                                                     AS 'Reporting To 2 - Section',
       form_type.type                                                                              AS 'Form Type',
       CONVERT(IF(form.customer_number = 0, '-', form.customer_number) USING utf8)                 AS 'Customer Number', 
       form.customer_name                                                                          AS 'Customer Name',
       form.customer_contract                                                                      AS 'Customer Contract No.',
       DATE_FORMAT(form.action_date, '%d %b %Y')                                                   AS 'Action - On Date',
       CONCAT('http://cns', attachment_1.path, '/', attachment_1.filename_generated)               AS 'Attachment - 1',
       CONCAT('http://cns', attachment_2.path, '/', attachment_2.filename_generated)               AS 'Attachment - 2',
       agent.name                                                                                  AS 'Agent - Name',
       agent.tag                                                                                   AS 'Agent - Tag',
       agent.type                                                                                  AS 'Agent - Type',
       CONVERT(IFNULL(agent_teamleader.real_name, '') USING utf8)                                  AS 'Agent - Team Leader - Name',
       creator.id                                                                                  AS `creator id`, 
       creator.real_name                                                                           AS `creator full name`, 
       CONVERT(IFNULL(authorizing_teamleader_user.id, '') USING utf8)                              AS `processed by - team leader - system id`, 
       IFNULL(authorizing_teamleader_user.real_name, '')                                           AS `processed by - team leader - name`, 
       CONVERT(IFNULL(authorizing_teamleader_user.employee_id, '') USING utf8)                     AS `processed by - team leader - employee id`, 
       CONVERT(IFNULL(DATE_FORMAT(authorizing_teamleader.action_date, '%d %b %Y'), '') USING utf8) AS `processed on - team leader - date`, 
       CONVERT(IFNULL(DATE_FORMAT(authorizing_teamleader.action_date, '%h:%i %p'), '') USING utf8) AS `processed on - team leader - time`, 
       CONVERT(IFNULL(authorizing_manager_user.id, '') USING utf8)                                 AS `processed by - manager - system id`, 
       IFNULL(authorizing_manager_user.real_name, '')                                              AS `processed by - manager - name`, 
       CONVERT(IFNULL(authorizing_manager_user.employee_id, '') USING utf8)                        AS `processed by - manager - employee id`, 
       CONVERT(IFNULL(DATE_FORMAT(authorizing_manager.action_date, '%d %b %Y'), '') USING utf8)    AS `processed on - manager - date`, 
       CONVERT(IFNULL(DATE_FORMAT(authorizing_manager.action_date, '%h:%i %p'), '') USING utf8)    AS `processed on - manager - time`, 
       CONVERT(IFNULL(authorizing_director_user.id, '') USING utf8)                                AS `processed by - director - system id`, 
       IFNULL(authorizing_director_user.real_name, '')                                             AS `processed by - director - name`, 
       CONVERT(IFNULL(authorizing_director_user.employee_id, '') USING utf8)                       AS `processed by - director - employee id`, 
       CONVERT(IFNULL(DATE_FORMAT(authorizing_director.action_date, '%d %b %Y'), '') USING utf8)   AS `processed on - director - date`, 
       CONVERT(IFNULL(DATE_FORMAT(authorizing_director.action_date, '%h:%i %p'), '') USING utf8)   AS `processed on - director - time`, 
       status.name                                                                                 AS `status`,
       CONVERT(IF(status.name = 'Pending', '', user_status_by.id) USING utf8)                      AS `status by - system id`, 
       IFNULL(user_status_by.real_name, '')                                                        AS `status by - name`, 
       CONVERT(IFNULL(user_status_by.employee_id, '') USING utf8)                                  AS `status by - employee id`, 
       IFNULL(user_status_by_role.name, '')                                                        AS `status by - position`, 
       CONVERT(IFNULL(DATE_FORMAT(form.status_on, '%d %b %Y'), '') USING utf8)                     AS `status on - date`, 
       CONVERT(IFNULL(DATE_FORMAT(form.status_on, '%h:%i %p'), '') USING utf8)                     AS `status on - time`, 
       CONCAT('http://cns/pdf/', form.pdf)                                                         AS `pdf`
FROM   forms AS form
       JOIN (sections AS section, 
            departments AS department) 
         ON form.section_id = section.id 
             AND section.department_id = department.id 
       JOIN (users AS reporting_to_1, 
            sections AS reporting_to_1_section, 
            departments AS reporting_to_1_department)
         ON reporting_to_1.id = form.reporting_to_1 
             AND reporting_to_1.section_id = reporting_to_1_section.id 
             AND reporting_to_1_section.department_id = reporting_to_1_department.id 
       LEFT JOIN (users AS reporting_to_2, sections AS reporting_to_2_section, 
                 departments AS reporting_to_2_department)
         ON reporting_to_2.id = form.reporting_to_2 
             AND reporting_to_2.section_id = reporting_to_2_section.id 
             AND reporting_to_2_section.department_id = reporting_to_2_department.id 
       JOIN form_type 
         ON form.type = form_type.id 
       LEFT JOIN attachments AS attachment_1 
         ON form.id = attachment_1.form 
             AND attachment_1.id = ( SELECT min(id) 
                                     FROM   attachments 
                                     WHERE  form = form.id) 
       LEFT JOIN attachments AS attachment_2 
         ON form.id = attachment_2.form 
             AND attachment_2.id = ( SELECT max(id) 
                                     FROM   attachments 
                                     WHERE  form = form.id) 
       LEFT JOIN (agents AS agent,
                 users AS agent_teamleader,
                 branches AS branch) 
         ON form.id = agent.form_id 
             AND agent_teamleader.id = agent.teamleader_id 
             AND branch.id = agent.branch_id 
       JOIN users AS creator 
          ON form.user_id = creator.id 
       LEFT JOIN (authorizers AS authorizing_teamleader,
                 users AS authorizing_teamleader_user) 
          ON authorizing_teamleader.form_id = form.id 
             AND authorizing_teamleader_user.id = authorizing_teamleader.`from` 
             AND authorizing_teamleader_user.role = 't' 
       LEFT JOIN (authorizers AS authorizing_manager,
                 users AS authorizing_manager_user) 
          ON authorizing_manager.form_id = form.id 
             AND authorizing_manager_user.id = authorizing_manager.`from` 
             AND authorizing_manager_user.role = 'm' 
       LEFT JOIN (authorizers AS authorizing_director,
                 users AS authorizing_director_user) 
          ON authorizing_director.form_id = form.id 
             AND authorizing_director_user.id = authorizing_director.`from` 
             AND authorizing_director_user.role = 'd' 
       JOIN status 
          ON form.status = status.id 
       LEFT JOIN (users AS user_status_by,
                 roles AS user_status_by_role) 
          ON user_status_by.id = form.status_by_user_id 
             AND user_status_by_role.id = user_status_by.role 
GROUP  BY form.id 
ORDER  BY form.id DESC 
LIMIT 0, 100

解释扩展 - 服务器

+----+--------------------+-----------------------------+--------+----------------------------------------------------------------+-------------+---------+------------------------------------------+------+----------+---------------------------------+
| id |    select_type     |            table            |  type  |                         possible_keys                          |     key     | key_len |                   ref                    | rows | filtered |              Extra              |
+----+--------------------+-----------------------------+--------+----------------------------------------------------------------+-------------+---------+------------------------------------------+------+----------+---------------------------------+
|  1 | PRIMARY            | section                     | ALL    | PRIMARY,IDX_DEPARTMENT                                         |             |         |                                          |   18 |      100 | Using temporary; Using filesort |
|  1 | PRIMARY            | department                  | eq_ref | PRIMARY                                                        | PRIMARY     |       4 | cns.section.department_id                |    1 |      100 |                                 |
|  1 | PRIMARY            | form                        | ref    | IDX_USER_ID,IDX_REPORTING_TO_1,IDX_SECTION,IDX_TYPE,IDX_STATUS | IDX_SECTION |       4 | cns.section.id                           |  528 |      100 |                                 |
|  1 | PRIMARY            | status                      | eq_ref | PRIMARY                                                        | PRIMARY     |       3 | cns.form.status                          |    1 |      100 |                                 |
|  1 | PRIMARY            | form_type                   | eq_ref | PRIMARY                                                        | PRIMARY     |       4 | cns.form.type                            |    1 |      100 |                                 |
|  1 | PRIMARY            | authorizing_teamleader      | ref    | IDX_FORM_ID,IDX_FROM_USER_ID                                   | IDX_FORM_ID |       4 | cns.form.id                              |    1 |      100 |                                 |
|  1 | PRIMARY            | authorizing_teamleader_user | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY     |       4 | cns.authorizing_teamleader.from          |    1 |      100 |                                 |
|  1 | PRIMARY            | authorizing_manager         | ref    | IDX_FORM_ID,IDX_FROM_USER_ID                                   | IDX_FORM_ID |       4 | cns.form.id                              |    1 |      100 |                                 |
|  1 | PRIMARY            | authorizing_manager_user    | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY     |       4 | cns.authorizing_manager.from             |    1 |      100 |                                 |
|  1 | PRIMARY            | authorizing_director        | ref    | IDX_FORM_ID,IDX_FROM_USER_ID                                   | IDX_FORM_ID |       4 | cns.form.id                              |    1 |      100 |                                 |
|  1 | PRIMARY            | authorizing_director_user   | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY     |       4 | cns.authorizing_director.from            |    1 |      100 |                                 |
|  1 | PRIMARY            | attachment_1                | eq_ref | PRIMARY,IDX_FORM_ID                                            | PRIMARY     |       4 | func                                     |    1 |      100 |                                 |
|  1 | PRIMARY            | attachment_2                | eq_ref | PRIMARY,IDX_FORM_ID                                            | PRIMARY     |       4 | func                                     |    1 |      100 |                                 |
|  1 | PRIMARY            | agent                       | ref    | IDX_FORM_ID,IDX_BRANCH_ID,IDX_TEAMLEADER_ID                    | IDX_FORM_ID |       4 | cns.form.id                              |    1 |      100 |                                 |
|  1 | PRIMARY            | agent_teamleader            | eq_ref | PRIMARY                                                        | PRIMARY     |       4 | cns.agent.teamleader_id                  |    1 |      100 |                                 |
|  1 | PRIMARY            | branch                      | eq_ref | PRIMARY                                                        | PRIMARY     |       4 | cns.agent.branch_id                      |    1 |      100 | Using index                     |
|  1 | PRIMARY            | reporting_to_1              | eq_ref | PRIMARY,IDX_SECTION                                            | PRIMARY     |       4 | cns.form.reporting_to_1                  |    1 |      100 |                                 |
|  1 | PRIMARY            | reporting_to_2              | eq_ref | PRIMARY,IDX_SECTION                                            | PRIMARY     |       4 | cns.form.reporting_to_2                  |    1 |      100 |                                 |
|  1 | PRIMARY            | reporting_to_2_section      | eq_ref | PRIMARY,IDX_DEPARTMENT                                         | PRIMARY     |       4 | cns.reporting_to_2.section_id            |    1 |      100 |                                 |
|  1 | PRIMARY            | reporting_to_2_department   | eq_ref | PRIMARY                                                        | PRIMARY     |       4 | cns.reporting_to_2_section.department_id |    1 |      100 |                                 |
|  1 | PRIMARY            | creator                     | eq_ref | PRIMARY                                                        | PRIMARY     |       4 | cns.form.user_id                         |    1 |      100 |                                 |
|  1 | PRIMARY            | reporting_to_1_section      | eq_ref | PRIMARY,IDX_DEPARTMENT                                         | PRIMARY     |       4 | cns.reporting_to_1.section_id            |    1 |      100 |                                 |
|  1 | PRIMARY            | reporting_to_1_department   | eq_ref | PRIMARY                                                        | PRIMARY     |       4 | cns.reporting_to_1_section.department_id |    1 |      100 |                                 |
|  1 | PRIMARY            | user_status_by              | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY     |       4 | cns.form.status_by_user_id               |    1 |      100 |                                 |
|  1 | PRIMARY            | user_status_by_role         | eq_ref | PRIMARY                                                        | PRIMARY     |       3 | cns.user_status_by.role                  |    1 |      100 |                                 |
|  3 | DEPENDENT SUBQUERY | attachments                 | ref    | IDX_FORM_ID                                                    | IDX_FORM_ID |       4 | cns.form.id                              |    1 |      100 | Using index                     |
|  2 | DEPENDENT SUBQUERY | attachments                 | ref    | IDX_FORM_ID                                                    | IDX_FORM_ID |       4 | cns.form.id                              |    1 |      100 | Using index                     |
+----+--------------------+-----------------------------+--------+----------------------------------------------------------------+-------------+---------+------------------------------------------+------+----------+---------------------------------+

解释扩展 - 笔记本电脑

+----+--------------------+-----------------------------+--------+----------------------------------------------------------------+---------------+---------+------------------------------------------+------+----------+----------------------------------------------+
| id |    select_type     |            table            |  type  |                         possible_keys                          |      key      | key_len |                   ref                    | rows | filtered |                    Extra                     |
+----+--------------------+-----------------------------+--------+----------------------------------------------------------------+---------------+---------+------------------------------------------+------+----------+----------------------------------------------+
|  1 | PRIMARY            | form_type                   | index  | PRIMARY                                                        | IDX_FORM_TYPE |     137 |                                          |    2 |      100 | Using index; Using temporary; Using filesort |
|  1 | PRIMARY            | form                        | ref    | IDX_USER_ID,IDX_REPORTING_TO_1,IDX_SECTION,IDX_TYPE,IDX_STATUS | IDX_TYPE      |       4 | cns.form_type.id                         | 1443 |      100 |                                              |
|  1 | PRIMARY            | status                      | eq_ref | PRIMARY                                                        | PRIMARY       |       3 | cns.form.status                          |    1 |      100 |                                              |
|  1 | PRIMARY            | section                     | eq_ref | PRIMARY,IDX_DEPARTMENT                                         | PRIMARY       |       4 | cns.form.section_id                      |    1 |      100 |                                              |
|  1 | PRIMARY            | department                  | eq_ref | PRIMARY                                                        | PRIMARY       |       4 | cns.section.department_id                |    1 |      100 |                                              |
|  1 | PRIMARY            | authorizing_teamleader      | ref    | IDX_FORM_ID,IDX_FROM_USER_ID                                   | IDX_FORM_ID   |       4 | cns.form.id                              |    1 |      100 |                                              |
|  1 | PRIMARY            | authorizing_teamleader_user | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY       |       4 | cns.authorizing_teamleader.from          |    1 |      100 |                                              |
|  1 | PRIMARY            | authorizing_manager         | ref    | IDX_FORM_ID,IDX_FROM_USER_ID                                   | IDX_FORM_ID   |       4 | cns.form.id                              |    1 |      100 |                                              |
|  1 | PRIMARY            | authorizing_manager_user    | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY       |       4 | cns.authorizing_manager.from             |    1 |      100 |                                              |
|  1 | PRIMARY            | authorizing_director        | ref    | IDX_FORM_ID,IDX_FROM_USER_ID                                   | IDX_FORM_ID   |       4 | cns.form.id                              |    1 |      100 |                                              |
|  1 | PRIMARY            | authorizing_director_user   | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY       |       4 | cns.authorizing_director.from            |    1 |      100 |                                              |
|  1 | PRIMARY            | attachment_1                | eq_ref | PRIMARY,IDX_FORM_ID                                            | PRIMARY       |       4 | func                                     |    1 |      100 |                                              |
|  1 | PRIMARY            | attachment_2                | eq_ref | PRIMARY,IDX_FORM_ID                                            | PRIMARY       |       4 | func                                     |    1 |      100 |                                              |
|  1 | PRIMARY            | agent                       | ref    | IDX_FORM_ID,IDX_BRANCH_ID,IDX_TEAMLEADER_ID                    | IDX_FORM_ID   |       4 | cns.form.id                              |    1 |      100 |                                              |
|  1 | PRIMARY            | agent_teamleader            | eq_ref | PRIMARY                                                        | PRIMARY       |       4 | cns.agent.teamleader_id                  |    1 |      100 |                                              |
|  1 | PRIMARY            | branch                      | eq_ref | PRIMARY                                                        | PRIMARY       |       4 | cns.agent.branch_id                      |    1 |      100 | Using index                                  |
|  1 | PRIMARY            | reporting_to_1              | eq_ref | PRIMARY,IDX_SECTION                                            | PRIMARY       |       4 | cns.form.reporting_to_1                  |    1 |      100 |                                              |
|  1 | PRIMARY            | reporting_to_2              | eq_ref | PRIMARY,IDX_SECTION                                            | PRIMARY       |       4 | cns.form.reporting_to_2                  |    1 |      100 |                                              |
|  1 | PRIMARY            | reporting_to_2_section      | eq_ref | PRIMARY,IDX_DEPARTMENT                                         | PRIMARY       |       4 | cns.reporting_to_2.section_id            |    1 |      100 |                                              |
|  1 | PRIMARY            | reporting_to_2_department   | eq_ref | PRIMARY                                                        | PRIMARY       |       4 | cns.reporting_to_2_section.department_id |    1 |      100 |                                              |
|  1 | PRIMARY            | creator                     | eq_ref | PRIMARY                                                        | PRIMARY       |       4 | cns.form.user_id                         |    1 |      100 |                                              |
|  1 | PRIMARY            | reporting_to_1_section      | eq_ref | PRIMARY,IDX_DEPARTMENT                                         | PRIMARY       |       4 | cns.reporting_to_1.section_id            |    1 |      100 |                                              |
|  1 | PRIMARY            | reporting_to_1_department   | eq_ref | PRIMARY                                                        | PRIMARY       |       4 | cns.reporting_to_1_section.department_id |    1 |      100 |                                              |
|  1 | PRIMARY            | user_status_by              | eq_ref | PRIMARY,IDX_ROLE                                               | PRIMARY       |       4 | cns.form.status_by_user_id               |    1 |      100 |                                              |
|  1 | PRIMARY            | user_status_by_role         | eq_ref | PRIMARY                                                        | PRIMARY       |       3 | cns.user_status_by.role                  |    1 |      100 |                                              |
|  3 | DEPENDENT SUBQUERY | attachments                 | ref    | IDX_FORM_ID                                                    | IDX_FORM_ID   |       4 | cns.form.id                              |    1 |      100 | Using index                                  |
|  2 | DEPENDENT SUBQUERY | attachments                 | ref    | IDX_FORM_ID                                                    | IDX_FORM_ID   |       4 | cns.form.id                              |    1 |      100 | Using index                                  |
+----+--------------------+-----------------------------+--------+----------------------------------------------------------------+---------------+---------+------------------------------------------+------+----------+----------------------------------------------+

型号

笔记本电脑规格

操作系统:Microsoft Windows 7 Professional,处理器:Intel® Core™ i7-4600M 处理器(4M 缓存,最高 3.60 GHz),内存:8GB

服务器规格

操作系统:Microsoft Windows 2008 Standard SP2,处理器:Intel® Xeon® Processor X5570(8M 高速缓存,2.93 GHz,6.40 GT/s Intel® QPI),内存:4GB

疑难解答

1. 将两个数据库中所有表的引擎从 InnoDB 更改为 MyISAM,并进行了优化。在服务器上运行需要89.435 秒,在笔记本电脑上运行需要57.252 秒。与使用 InnoDB 引擎的 1.388 秒查询时间相比,笔记本电脑仍然更快,非常慢。

【问题讨论】:

  • 服务器解释和笔记本解释有很大的不同:在笔记本上它的部分表上是Using Index,而在服务器上不是。所以你的问题在于那个缺失的索引。
  • @JorgeCampos 我在笔记本电脑和服务器上的所有桌子上运行SHOW INDEX。除了 Cardinality 之外,所有数据看起来都相同。 Screenshots
  • 假设你还在使用 InnDB,运行show variables like 'innodb_%';。innodb_buffer_pool_size 的值是多少?两者都一样吗?
  • @Mihai 两者都一样 :(
  • 在服务器上优化你的表mysqlcheck -u youruser -p -o cns

标签: mysql sql mysql-5.1


【解决方案1】:

笔记本电脑和服务器之间的数据(不是架构)不同?

解释表明section 表选择服务器上的所有行,而不是像笔记本电脑上那样只选择一个。

此外,它在服务器中显示Using temporary; Using filesort:这可能是问题的根源。

【讨论】:

  • 我不确定为什么数据不匹配,因为我刚刚导出/导入了数据。以下是使用 MySQL Workbench 的表信息截图:Laptop vs Server
  • 差别太大了!!您至少有一半的用户,50 个代理行和 300 个表单行丢失!
  • 你是对的,但这真的会在查询时间上有那么大的差异吗?
  • 对于 InnoDB 表,除了计算它们之外,服务器无法告知表中的确切行数。它估计数字(使用它拥有的各种信息),这就是数字在每次请求时发生变化的原因。这是正常行为。
  • 我们能看到笔记本电脑和服务器的 my.cnf 文件吗?如果两者之间的 innodb_buffer_pool_size 或 tmp_table_size 不同,则可能会触发使用临时文件而不是将内容保存在内存中。
【解决方案2】:

也许在将数据库从一台机器转移到另一台机器后,您的表中的索引没有得到重建(我以前经历过这种情况)。您必须手动告诉 MySQL 重建索引。如果我没记错的话,您可以使用 OPTIMIZE 查询来实现

优化表your_table

不存在索引会显着减慢您的查询速度,尽管您遇到的差异可能太大而无法用这个问题来解释。 和之前的 cmets 一样,您能发布您的表格/查询吗?

【讨论】:

  • 您能否验证(例如在 PhpMyAdmin 中)您的索引的基数? (即大小)如果您的服务器机器上的那些是 0,那就是问题所在。
  • 正如我所怀疑的,您正在执行大量的 JOIN,这很容易在您的任何表上没有良好的索引。
  • 我几乎没有使用过 MySQL Workbench,但我发现了这个:dev.mysql.com/doc/refman/5.0/en/show-index.html
  • 那肯定会更多,你做了优化查询吗?
  • 啊,你得到了相当大的基数(我以为你的意思是它们都等于 1)。如果我没记错的话,在 MySQL 工作台中,您可以实时查看服务器正在做什么。您已经可以看到它是哪个活动了吗?我曾经有很长的查询时间,结果发现只是将结果发送回我的客户需要很长时间。
【解决方案3】:

事物在数据库中绝对不会线性扩展。我曾经通过在 where 部分中预先选择 7% 的原始表和附加子句来更改包含大量计算的查询。结果不是速度提升了15倍左右,而是3000多倍!

(我怀疑 DBMS 突然能够将表完全存储在内存中)

无论如何;您可以尝试使用分析器来查看在两个系统上执行的实际查询以及不同步骤所花费的时间。如果没记错的话,可以使用 MySQL 的分析器。

【讨论】:

  • 两个查询配置文件:LaptopServer。你能帮忙分析一下数据吗?
  • 在服务器配置文件数据中“处理到 tmp 表”要慢得多。
  • 为什么会出现这个问题,我该如何解决?
  • 很难说。也许是杀毒软件(尝试禁用实时保护)?
  • 服务器上没有运行防病毒软件。此外,这里是 Windows Task Manager Performance Tab 的屏幕截图。此活动会影响查询时间吗?
【解决方案4】:

您查询的主要问题是您使用了太多无用的convert using utf-8(用于 id 和日期时间???),只需删除所有转换,我可以在不到 1 秒的时间内在我的笔记本电脑上运行您的查询(在删除之前,需要30多s,我没耐心再等了,就停下吧)

SELECT form.id                                                                                     AS 'Form ID',
   DATE_FORMAT(form.created_on, '%d %b %Y')                                                    AS 'Created On - Date',
   DATE_FORMAT(form.created_on, '%h:%i %p')                                                    AS 'Created On - Time',
   department.name                                                                             AS 'Department',
   section.name                                                                                AS 'Section',
   reporting_to_1.id                                                                           AS 'Reporting To 1 - System ID',
   reporting_to_1.real_name                                                                    AS 'Reporting To 1 - Name',
   reporting_to_1_department.name                                                              AS 'Reporting To 1 - Department',
   reporting_to_1_section.name                                                                 AS 'Reporting To 1 - Section',
   IFNULL(reporting_to_2.id, '')                                           AS 'Reporting To 2 - System ID',
   IFNULL(reporting_to_2.real_name, '')                                                        AS 'Reporting To 2 - Name',
   IFNULL(reporting_to_2_department.name, '')                                                  AS 'Reporting To 2 - Department',
   IFNULL(reporting_to_2_section.name, '')                                                     AS 'Reporting To 2 - Section',
   form_type.type                                                                              AS 'Form Type',
   IF(form.customer_number = 0, '-', form.customer_number)                 AS 'Customer Number', 
   form.customer_name                                                                          AS 'Customer Name',
   form.customer_contract                                                                      AS 'Customer Contract No.',
   DATE_FORMAT(form.action_date, '%d %b %Y')                                                   AS 'Action - On Date',
   CONCAT('http://cns', attachment_1.path, '/', attachment_1.filename_generated)               AS 'Attachment - 1',
   CONCAT('http://cns', attachment_2.path, '/', attachment_2.filename_generated)               AS 'Attachment - 2',
   agent.name                                                                                  AS 'Agent - Name',
   agent.tag                                                                                   AS 'Agent - Tag',
   agent.type                                                                                  AS 'Agent - Type',
   IFNULL(agent_teamleader.real_name, '')                                  AS 'Agent - Team Leader - Name',
   creator.id                                                                                  AS `creator id`, 
   creator.real_name                                                                           AS `creator full name`, 
   IFNULL(authorizing_teamleader_user.id, '')                               AS `processed by - team leader - system id`, 
   IFNULL(authorizing_teamleader_user.real_name, '')                                           AS `processed by - team leader - name`, 
   IFNULL(authorizing_teamleader_user.employee_id, '')                      AS `processed by - team leader - employee id`, 
   IFNULL(DATE_FORMAT(authorizing_teamleader.action_date, '%d %b %Y'), '')  AS `processed on - team leader - date`, 
   IFNULL(DATE_FORMAT(authorizing_teamleader.action_date, '%h:%i %p'), '')  AS `processed on - team leader - time`, 
   IFNULL(authorizing_manager_user.id, '')                                  AS `processed by - manager - system id`, 
   IFNULL(authorizing_manager_user.real_name, '')                                              AS `processed by - manager - name`, 
   IFNULL(authorizing_manager_user.employee_id, '')                        AS `processed by - manager - employee id`, 
   IFNULL(DATE_FORMAT(authorizing_manager.action_date, '%d %b %Y'), '')    AS `processed on - manager - date`, 
   IFNULL(DATE_FORMAT(authorizing_manager.action_date, '%h:%i %p'), '')    AS `processed on - manager - time`, 
   IFNULL(authorizing_director_user.id, '')                                AS `processed by - director - system id`, 
   IFNULL(authorizing_director_user.real_name, '')                                             AS `processed by - director - name`, 
   IFNULL(authorizing_director_user.employee_id, '')                       AS `processed by - director - employee id`, 
   IFNULL(DATE_FORMAT(authorizing_director.action_date, '%d %b %Y'), '')   AS `processed on - director - date`, 
   IFNULL(DATE_FORMAT(authorizing_director.action_date, '%h:%i %p'), '')   AS `processed on - director - time`, 
   status.name                                                                                 AS `status`,
   IF(status.name = 'Pending', '', user_status_by.id)                      AS `status by - system id`, 
   IFNULL(user_status_by.real_name, '')                                                        AS `status by - name`, 
   IFNULL(user_status_by.employee_id, '')                                  AS `status by - employee id`, 
   IFNULL(user_status_by_role.name, '')                                                        AS `status by - position`, 
   IFNULL(DATE_FORMAT(form.status_on, '%d %b %Y'), '')                     AS `status on - date`, 
   IFNULL(DATE_FORMAT(form.status_on, '%h:%i %p'), '')                     AS `status on - time`, 
   CONCAT('http://cns/pdf/', form.pdf)                                                         AS `pdf`
FROM   forms AS form
   JOIN (sections AS section, 
        departments AS department) 
     ON form.section_id = section.id 
         AND section.department_id = department.id 
   JOIN (users AS reporting_to_1, 
        sections AS reporting_to_1_section, 
        departments AS reporting_to_1_department)
     ON reporting_to_1.id = form.reporting_to_1 
         AND reporting_to_1.section_id = reporting_to_1_section.id 
         AND reporting_to_1_section.department_id = reporting_to_1_department.id 
   LEFT JOIN (users AS reporting_to_2, sections AS reporting_to_2_section, 
             departments AS reporting_to_2_department)
     ON reporting_to_2.id = form.reporting_to_2 
         AND reporting_to_2.section_id = reporting_to_2_section.id 
         AND reporting_to_2_section.department_id = reporting_to_2_department.id 
   JOIN form_type 
     ON form.type = form_type.id 
   LEFT JOIN attachments AS attachment_1 
     ON form.id = attachment_1.form 
         AND attachment_1.id = ( SELECT min(id) 
                                 FROM   attachments 
                                 WHERE  form = form.id) 
   LEFT JOIN attachments AS attachment_2 
     ON form.id = attachment_2.form 
         AND attachment_2.id = ( SELECT max(id) 
                                 FROM   attachments 
                                 WHERE  form = form.id) 
   LEFT JOIN (agents AS agent,
             users AS agent_teamleader,
             branches AS branch) 
     ON form.id = agent.form_id 
         AND agent_teamleader.id = agent.teamleader_id 
         AND branch.id = agent.branch_id 
   JOIN users AS creator 
      ON form.user_id = creator.id 
   LEFT JOIN (authorizers AS authorizing_teamleader,
             users AS authorizing_teamleader_user) 
      ON authorizing_teamleader.form_id = form.id 
         AND authorizing_teamleader_user.id = authorizing_teamleader.`from` 
         AND authorizing_teamleader_user.role = 't' 
   LEFT JOIN (authorizers AS authorizing_manager,
             users AS authorizing_manager_user) 
      ON authorizing_manager.form_id = form.id 
         AND authorizing_manager_user.id = authorizing_manager.`from` 
         AND authorizing_manager_user.role = 'm' 
   LEFT JOIN (authorizers AS authorizing_director,
             users AS authorizing_director_user) 
      ON authorizing_director.form_id = form.id 
         AND authorizing_director_user.id = authorizing_director.`from` 
         AND authorizing_director_user.role = 'd' 
   JOIN status 
      ON form.status = status.id 
   LEFT JOIN (users AS user_status_by,
             roles AS user_status_by_role) 
      ON user_status_by.id = form.status_by_user_id 
         AND user_status_by_role.id = user_status_by.role 
GROUP  BY form.id 
ORDER  BY form.id DESC 
LIMIT 0, 100

【讨论】:

  • 我刚刚运行了上面的查询;笔记本电脑5.491 秒与服务器120.729
  • 如果我不使用CONVERT,我会将该列中的所有值作为BLOB
  • 只是想知道,为什么不将mysql升级到5.5?它现在是标准的,不是吗?
  • @HeiuVo 还有其他应用程序在 WAMP 2.0 上运行。如果没有任何效果,那么我会更新它。我仍在试图弄清楚为什么会这样!
  • 您可以尝试将您的数据库导出到笔记本电脑中,完全删除您的服务器中的数据库并重新导入吗?
【解决方案5】:

我无法添加 cmets,所以我将投入两分钱作为答案。 这仅适用于您不拥有服务器并与其他服务共享它的情况。 笔记本电脑,单盘旋转,盘片上的所有记录都很好,整洁。数据库是唯一使用资源的东西。

服务器、共享服务、SAN 或 RAID 磁盘、文件不在一个盘片上,您正在争夺资源。如果您有权访问,请在运行查询时检查 CPU 利用率等资源。如果您与其他人共享此服务器,那么您可能只被分配了这么多的处理器资源和内存来执行您的查询。虽然您的服务器规格令人印象深刻,但共享环境会为每一方分配少量资源。

在这种情况下,您的笔记本电脑是更强大的设备。向您的系统管理员咨询更多资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2021-12-09
    相关资源
    最近更新 更多