0

我正在尝试提高应用程序的性能。我可能需要创建在 cron 上运行的汇总表,这样应用程序的加载时间就不会太长(5-10 秒)。这是最好的主意吗?

给定下表:

mysql> describe school_data_sets_numeric_data;
+--------------+---------------+------+-----+---------+----------------+
| Field        | Type          | Null | Key | Default | Extra          |
+--------------+---------------+------+-----+---------+----------------+
| id           | int(11)       | NO   | PRI | NULL    | auto_increment |
| data_set_nid | int(11)       | NO   | MUL | NULL    |                |
| school_nid   | int(11)       | NO   | MUL | NULL    |                |
| year         | int(11)       | NO   | MUL | NULL    |                |
| description  | varchar(255)  | NO   |     | NULL    |                |
| value        | decimal(18,5) | NO   |     | NULL    |                |
+--------------+---------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

以及以下查询(为学校的每个 data_set_nid 运行一次)

此查询运行速度很快(0 秒):

SELECT year, description, CONCAT(FORMAT((value/(SELECT SUM(value) 
FROM `school_data_sets_numeric_data` as numeric_data_inner 
WHERE year = numeric_data_outer.year and data_set_nid = numeric_data_outer.data_set_nid and school_nid = numeric_data_outer.school_nid)) * 100, 2), '%') as value 
FROM `school_data_sets_numeric_data` as numeric_data_outer 
WHERE data_set_nid = 38251 and school_nid = 32805 ORDER BY id DESC;

解释:

+----+--------------------+--------------------+------+---------------------------------------------+--------------+---------+-----------------------------------------------------------------------------------------------------------+------+-----------------------------+
| id | select_type        | table              | type | possible_keys                               | key          | key_len | ref                                                                                                       | rows | Extra                       |
+----+--------------------+--------------------+------+---------------------------------------------+--------------+---------+-----------------------------------------------------------------------------------------------------------+------+-----------------------------+
|  1 | PRIMARY            | numeric_data_outer | ref  | data_set_nid,data_set_nid_2,school_nid      | data_set_nid | 8       | const,const                                                                                               |   17 | Using where; Using filesort |
|  2 | DEPENDENT SUBQUERY | numeric_data_inner | ref  | year,data_set_nid,data_set_nid_2,school_nid | data_set_nid | 8       | rocdocs_main_drupal_7.numeric_data_outer.data_set_nid,rocdocs_main_drupal_7.numeric_data_outer.school_nid |    9 | Using where                 |
+----+--------------------+--------------------+------+---------------------------------------------+--------------+---------+-----------------------------------------------------------------------------------------------------------+------+-----------------------------+

此查询运行缓慢(1.43 秒):

SELECT year, description, CONCAT(FORMAT((SUM(value)/(SELECT SUM(value) 
FROM `school_data_sets_numeric_data` as numeric_data_inner 
WHERE year = numeric_data_outer.year and data_set_nid = numeric_data_outer.data_set_nid)) * 100, 2), '%') as value 
FROM `school_data_sets_numeric_data` as numeric_data_outer 
WHERE data_set_nid = 38251 GROUP BY year,description ORDER BY id DESC;

解释:

+----+--------------------+--------------------+------+----------------------------------+----------------+---------+-------+-------+----------------------------------------------+
| id | select_type        | table              | type | possible_keys                    | key            | key_len | ref   | rows  | Extra                                        |
+----+--------------------+--------------------+------+----------------------------------+----------------+---------+-------+-------+----------------------------------------------+
|  1 | PRIMARY            | numeric_data_outer | ref  | data_set_nid,data_set_nid_2      | data_set_nid_2 | 4       | const | 90640 | Using where; Using temporary; Using filesort |
|  2 | DEPENDENT SUBQUERY | numeric_data_inner | ref  | year,data_set_nid,data_set_nid_2 | year           | 4       | func  | 38871 | Using where                                  |
+----+--------------------+--------------------+------+----------------------------------+----------------+---------+-------+-------+----------------------------------------------+
4

1 回答 1

-1

相关的子查询/子选择通常是瓶颈——部分原因是 MySql 只有一个嵌套循环连接算法,没有散列连接/合并连接。

我会尝试将您的主选择加入到包含您需要的所有 SUM 值的派生表中。

于 2012-08-15T12:33:48.497 回答