Mysql Sql查询优化与调试
我正在尝试优化mysql数据库中的sql查询。尝试了不同的重写方法,添加/删除索引,但似乎没有任何东西可以减少负载。也许我错过了什么。 查询: 持续时间:2.01秒-2.20秒 优化查询:Mysql Sql查询优化与调试,mysql,Mysql,我正在尝试优化mysql数据库中的sql查询。尝试了不同的重写方法,添加/删除索引,但似乎没有任何东西可以减少负载。也许我错过了什么。 查询: 持续时间:2.01秒-2.20秒 优化查询: select co.country_name as state, ci.city_name as city, ci.city_id, ci.country_id, count(l.id) as num from (select ci1.city_name, ci1.city_id, ci1.coun
select co.country_name as state, ci.city_name as city, ci.city_id, ci.country_id, count(l.id) as num from
(select ci1.city_name, ci1.city_id, ci1.country_id from cities ci1
where ci1.main=1) as ci
INNER JOIN countries co ON (ci.country_id = co.country_id)
INNER JOIN dancers l ON (l.city_id = ci.city_id AND l.closed = 0 AND l.approved = 1 ) GROUP BY ci.city_id ORDER BY city
持续时间:0.82秒-0.90秒
但是我觉得这个查询可以被优化得更多,但是没有得到如何优化它的想法。有三张桌子
Table 1 : countries ( country_id, country_name)
Table 2 : cities ( city_id, city_name, main, country_id)
Table 3 : dancers ( id, country_id, city_id, closed, approved)
我正在尝试获取main=1的所有城市,并为每个城市统计加入国家的城市的所有配置文件,以获得国家名称
欢迎提出任何意见,谢谢
稍后编辑:-第一个查询解释
+----+-------------+-------+-------------+---------------------------------------------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+---------------------------------------------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
| 1 | SIMPLE | l | index_merge | city_id,closed,approved,city_id_2 | closed,approved | 1,2 | NULL | 75340 | Using intersect(closed,approved); Using where; Using temporary; Using filesort |
| 1 | SIMPLE | ci | eq_ref | PRIMARY,state_id_2,state_id,city_name,lat,city_name_shorter,city_id | PRIMARY | 4 | db.l.city_id | 1 | Using where |
| 1 | SIMPLE | co | eq_ref | PRIMARY | PRIMARY | 4 | db.ci.country_id | 1 | Using where |
+----+-------------+-------+-------------+---------------------------------------------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
第二个问题解释:
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
| 1 | PRIMARY | co | ALL | PRIMARY | NULL | NULL | NULL | 51 | Using temporary; Using filesort |
| 1 | PRIMARY | <derived2> | ref | <auto_key1> | <auto_key1> | 4 | db.co.country_id | 176 | Using where |
| 1 | PRIMARY | l | ref | city_id,closed,approved,city_id_2 | city_id_2 | 4 | ci.city_id | 44 | Using index condition; Using where |
| 2 | DERIVED | ci1 | ALL | NULL | NULL | NULL | NULL | 11765 | Using where |
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
| 1 | PRIMARY | co | ALL | PRIMARY | NULL | NULL | NULL | 51 | NULL |
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 4 | db.co.country_id | 565 | Using where |
| 2 | DERIVED | l | index_merge | city_id,closed,approved,city_id_2 | closed,approved | 1,2 | NULL | 75341 | Using intersect(closed,approved); Using where; Using temporary; Using filesort |
| 2 | DERIVED | ci1 | eq_ref | PRIMARY,state_id_2,city_id | PRIMARY | 4 | db.l.city_id | 1 | Using where |
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
|id |选择|类型|类型|可能的|键|键|列|参考|行|额外|
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
|1 | PRIMARY | co | ALL | PRIMARY | NULL | NULL | NULL | 51 |使用临时命令;使用文件排序|
|1 |主要| |参考| | | | 4 | db.co.country|u id | 176 |使用where|
|1 |主要| l |参考|城市id,关闭,批准,城市id | 2 |城市id | 2 | 4 | ci.city | id | 44 |使用索引条件;在哪里使用|
|2 |导出的| ci1 | ALL | NULL | NULL | NULL | NULL | 11765 |使用where|
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
@已由\u使用\u查询解释:
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
| 1 | PRIMARY | co | ALL | PRIMARY | NULL | NULL | NULL | 51 | Using temporary; Using filesort |
| 1 | PRIMARY | <derived2> | ref | <auto_key1> | <auto_key1> | 4 | db.co.country_id | 176 | Using where |
| 1 | PRIMARY | l | ref | city_id,closed,approved,city_id_2 | city_id_2 | 4 | ci.city_id | 44 | Using index condition; Using where |
| 2 | DERIVED | ci1 | ALL | NULL | NULL | NULL | NULL | 11765 | Using where |
+----+-------------+------------+------+-----------------------------------+-------------+---------+------------------+-------+------------------------------------+
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
| 1 | PRIMARY | co | ALL | PRIMARY | NULL | NULL | NULL | 51 | NULL |
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 4 | db.co.country_id | 565 | Using where |
| 2 | DERIVED | l | index_merge | city_id,closed,approved,city_id_2 | closed,approved | 1,2 | NULL | 75341 | Using intersect(closed,approved); Using where; Using temporary; Using filesort |
| 2 | DERIVED | ci1 | eq_ref | PRIMARY,state_id_2,city_id | PRIMARY | 4 | db.l.city_id | 1 | Using where |
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
|id |选择|类型|类型|可能的|键|键|列|参考|行|额外|
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
|1 | PRIMARY | co | ALL | PRIMARY | NULL | NULL | NULL | 51 | NULL|
|1 |主要| |参考| | | | 4 | db.co.country|u id | 565 |使用where|
|2 |派生| l |索引|合并|城市| id,关闭,批准,城市| id | 2 |关闭,批准| 1,2 |空| 75341 |使用intersect(关闭,批准);在何处使用;使用临时设备;使用文件排序|
|2 |派生| ci1 |等式|参考|主要,州| id | 2,城市|主要| 4 | db.l.city | id | 1 |使用其中|
+----+-------------+------------+-------------+-----------------------------------+-----------------+---------+------------------+-------+--------------------------------------------------------------------------------+
我建议您尝试以下方法:
SELECT
co.country_name AS state
, ci.city_name AS city
, ci.city_id
, ci.country_id
, ci.num
FROM (
SELECT
ci1.city_id
, ci1.city_name
, ci1.country_id
, COUNT(l.id) AS num
FROM cities ci1
INNER JOIN dancers l ON l.city_id = ci1.city_id
AND l.closed = 0
AND l.approved = 1
WHERE ci1.main = 1
GROUP BY
ci1.city_id
, ci1.city_name
, ci1.country_id
) AS ci
INNER JOIN countries co ON ci.country_id = co.country_id
;
如果需要,您可以提供解释计划输出以供进一步分析。当优化时,知道存在哪些指标,并让他解释计划,是最基本的
此外,MySQL不允许使用非标准的GROUPBY语法(在GROUPBY子句下只包含select列表中的一列或部分列)。
在MySQL的最新版本中,GROUPBY的默认行为已更改为SQL标准语法(其中select列表中的所有“非聚合”列必须包含在GROUPBY子句下)。虽然您现有的查询使用非标准的group by语法,但此处提供的查询不符合要求。请包含解释计划输出(作为文本)我用解释“作为文本”编辑了第一篇文章,您更有可能让人们阅读材料,因为它是格式化文本…认为图片更有用,添加了格式化文本。
city\u id,closed,approved
的索引(按列顺序C,on dancers)似乎会有所帮助。您是否获得此查询变体的解释计划?在主体中也添加了查询解释