Mysql 查询统计占用了99%的查询时间
我有一个SQL查询,执行大约需要1.81秒。我在phpMyAdmin中分析了它,我发现99%的时间都花在“统计”上 有趣的是,如果我再添加一个join,时间会上升到23秒左右,如果我删除一个join,时间会下降到0.26秒左右 我尝试将Mysql 查询统计占用了99%的查询时间,mysql,sql,phpmyadmin,entity-attribute-value,Mysql,Sql,Phpmyadmin,Entity Attribute Value,我有一个SQL查询,执行大约需要1.81秒。我在phpMyAdmin中分析了它,我发现99%的时间都花在“统计”上 有趣的是,如果我再添加一个join,时间会上升到23秒左右,如果我删除一个join,时间会下降到0.26秒左右 我尝试将产品\u id=放在连接中属性\u id=的位置。它不会提高性能,“统计”仍然占用99%的时间 我尝试了p.idgroupby p.id,它确实稍微提高了性能,但“统计”仍然占用了98%的时间 有没有办法提高“统计”性能,或者干脆禁用它? 注意:我对所有PK/FK
产品\u id=
放在连接中属性\u id=
的位置。它不会提高性能,“统计”仍然占用99%的时间
我尝试了p.idgroupby p.id
,它确实稍微提高了性能,但“统计”仍然占用了98%的时间
有没有办法提高“统计”性能,或者干脆禁用它?
注意:我对所有PK/FK都有索引
有用信息
id| select_type| table | type | possible_keys |key |key_len| ref | rows| Extra
1 | SIMPLE | gg | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | attribute_id | 4 | const | 143 |
1 | SIMPLE | aa | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | cc | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | bb | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | ii | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.aa.product_id | 2 | Using where
1 | SIMPLE | p | eq_ref | PRIMARY | PRIMARY | 4 | my_id.aa.product_id | 1 | Using where; Using index
1 | SIMPLE | dd | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.cc.product_id | 2 | Using where
1 | SIMPLE | ee | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.ii.product_id | 2 | Using where
1 | SIMPLE | ff | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.p.id | 2 | Using where
1 | SIMPLE | hh | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.cc.product_id | 2 | Using where
sqlfiddle
查询返回大约150行
ss_product table has 1k rows
ss_product_attributes has 30 rows
ss_product_attribute_varchars has 5000 rows
ss_product_attribute_decimals has 10000 rows
模式
配置文件
查询
解释
id| select_type| table | type | possible_keys |key |key_len| ref | rows| Extra
1 | SIMPLE | gg | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | attribute_id | 4 | const | 143 |
1 | SIMPLE | aa | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | cc | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | bb | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | ii | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.aa.product_id | 2 | Using where
1 | SIMPLE | p | eq_ref | PRIMARY | PRIMARY | 4 | my_id.aa.product_id | 1 | Using where; Using index
1 | SIMPLE | dd | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.cc.product_id | 2 | Using where
1 | SIMPLE | ee | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.ii.product_id | 2 | Using where
1 | SIMPLE | ff | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.p.id | 2 | Using where
1 | SIMPLE | hh | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.cc.product_id | 2 | Using where
完整配置文件(如Raymond Nijland所要求)
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
|状态|持续时间| CPU |用户| CPU |系统|上下文|自愿|上下文|非自愿|块|操作|输入|块|操作|输出|消息|发送|消息|接收|页面|主要故障|页面|次要|交换|源|功能|源|文件|行|
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
|开始时| 0.000151 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL|
|检查权限| 0.000005 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000003 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000003 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000005 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|检查权限| 0.000005 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql | u parse.cc | 4751|
|期初表| 0.000051 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql_base.cc | 4838|
|系统锁| 0.000016 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | lock.cc | 299|
|init | 0.000046 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql|u select.cc | 2560|
|优化| 0.000031 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql|u select.cc | 869|
|统计数据| 1.419911 | 1.404009 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | sql|
select
aa.value as attribute_a,
bb.value as attribute_b,
cc.value as attribute_c,
dd.value as attribute_d,
ee.value as attribute_e,
ff.value as attribute_f,
gg.value as attribute_g,
hh.value as attribute_h,
ii.value as attribute_i
from ss_products as p
inner join ss_product_attribute_varchars as aa
on p.id = aa.product_id
inner join ss_product_attribute_varchars as bb
on p.id = bb.product_id
inner join ss_product_attribute_varchars as cc
on p.id = cc.product_id
inner join ss_product_attribute_decimals as dd
on p.id = dd.product_id
inner join ss_product_attribute_decimals as ee
on p.id = ee.product_id
inner join ss_product_attribute_decimals as ff
on p.id = ff.product_id
inner join ss_product_attribute_varchars as gg
on p.id = gg.product_id
INNER JOIN ss_product_attribute_varchars AS hh
ON p.id = hh.product_id
INNER JOIN ss_product_attribute_varchars AS ii
ON p.id = ii.product_id
where
aa.attribute_id = 8 AND
bb.attribute_id = 6 AND
cc.attribute_id = 7 AND
dd.attribute_id = 9 and
ee.attribute_id = 10 and
ff.attribute_id = 11 AND
gg.attribute_id = 20 AND
hh.attribute_id = 2 AND
ii.attribute_id = 3
id| select_type| table | type | possible_keys |key |key_len| ref | rows| Extra
1 | SIMPLE | gg | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | attribute_id | 4 | const | 143 |
1 | SIMPLE | aa | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | cc | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | bb | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.gg.product_id | 2 | Using where
1 | SIMPLE | ii | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.aa.product_id | 2 | Using where
1 | SIMPLE | p | eq_ref | PRIMARY | PRIMARY | 4 | my_id.aa.product_id | 1 | Using where; Using index
1 | SIMPLE | dd | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.cc.product_id | 2 | Using where
1 | SIMPLE | ee | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.ii.product_id | 2 | Using where
1 | SIMPLE | ff | ref | fk_product_attribute_decimals_products1_idx,fk_pro... | fk_product_attribute_decimals_products1_idx | 4 | my_id.p.id | 2 | Using where
1 | SIMPLE | hh | ref | fk_product_attribute_varchars_products1_idx,fk_pro... | fk_product_attribute_varchars_products1_idx | 4 | my_id.cc.product_id | 2 | Using where
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
| Status | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function | Source_file | Source_line |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
| starting | 0.000151 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| checking permissions | 0.000005 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000003 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000003 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000005 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000002 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| checking permissions | 0.000005 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4751 |
| Opening tables | 0.000051 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_base.cc | 4838 |
| System lock | 0.000016 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | lock.cc | 299 |
| init | 0.000046 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_select.cc | 2560 |
| optimizing | 0.000031 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_select.cc | 869 |
| statistics | 1.419911 | 1.404009 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_select.cc | 1060 |
| preparing | 0.000050 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_select.cc | 1082 |
| executing | 0.000003 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_select.cc | 1829 |
| Sending data | 0.018508 | 0.015600 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_select.cc | 2371 |
| end | 0.000007 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_select.cc | 2596 |
| query end | 0.000004 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4440 |
| closing tables | 0.000018 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 4492 |
| freeing items | 0.000110 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 5640 |
| logging slow query | 0.000004 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 1461 |
| cleaning up | 0.000008 | 0.000000 | 0.000000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <unknown> | sql_parse.cc | 1417 |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+------------------+-------------------+-------------------+-------+-----------------+---------------+-------------+
/* Calculate how to do the join */
thd_proc_info(thd, "statistics");
if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
thd->is_fatal_error)
{
DBUG_PRINT("error",("Error: make_join_statistics() failed"));
DBUG_RETURN(1);
}
SHOW PROFILE ALL FOR QUERY n