这个查询+外键能出执行计划,不加就不能出

[复制链接]
查看11 | 回复3 | 2012-5-21 10:19:41 | 显示全部楼层 |阅读模式
mysql> explain select
->c_count,
->count(*) as custdist
-> from
->(
-> select
->c_custkey,
->count(o_orderkey)c_count
-> from
->customer left outer join orders on
-> c_custkey = o_custkey
-> and o_comment not like '%unusual%packages%'
-> group by
->c_custkey
->) c_orders
-> group by
->c_count
-> order by
->custdist desc,
->c_count desc limit10;
+----+-------------+------------+-------+---------------+------------+---------+-------------------------+---------+---------------------------------+
| id | select_type | table| type| possible_keys | key| key_len | ref
| rows| Extra
|
+----+-------------+------------+-------+---------------+------------+---------+-------------------------+---------+---------------------------------+
|1 | PRIMARY || ALL | NULL
| NULL | NULL| NULL
| 1500000 | Using temporary; Using filesort |
|2 | DERIVED | customer | index | NULL
| PRIMARY| 4 | NULL
|216720 | Using index
|
|2 | DERIVED | orders | ref | orders_fk1| orders_fk1 | 4 | tpch.customer.c_custkey | 7 |
|
+----+-------------+------------+-------+---------------+------------+---------+-------------------------+---------+---------------------------------+
3 rows in set (1 min 56.81 sec)
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
按说外键本身不带索引的
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
加前
-rw-rw---- 1 mysql mysql 8928 06-10 16:23 orders.frm
-rw-rw---- 1 mysql mysql 2000683008 06-10 16:27 orders.ibd
加后
-rw-rw---- 1 mysql mysql 8928 06-11 13:04 orders.frm
-rw-rw---- 1 mysql mysql 2315255808 06-11 13:09 orders.ibd
mysql> alter table orders add constraint orders_fk1 foreign key(o_custkey) references customer (c_custkey);
Query OK, 15000000 rows affected (4 min 58.88 sec)
Records: 15000000Duplicates: 0Warnings: 0
mysql>
mysql> explain select
->c_count,
->count(*) as custdist
-> from
->(
-> select
->c_custkey,
->count(o_orderkey)c_count
-> from
->customer left outer join orders on
-> c_custkey = o_custkey
-> and o_comment not like '%unusual%packages%'
-> group by
->c_custkey
->) c_orders
-> group by
->c_count
-> order by
->custdist desc,
->c_count desc limit10;
+----+-------------+------------+-------+---------------+------------+---------+--------------------------+---------+---------------------------------+
| id | select_type | table| type| possible_keys | key| key_len | ref
| rows| Extra
|
+----+-------------+------------+-------+---------------+------------+---------+--------------------------+---------+---------------------------------+
|1 | PRIMARY || ALL | NULL
| NULL | NULL| NULL
| 1500000 | Using temporary; Using filesort |
|2 | DERIVED | customer | index | NULL
| PRIMARY| 4 | NULL
|215781 | Using index
|
|2 | DERIVED | orders | ref | orders_fk1| orders_fk1 | 4 | tpch2.customer.c_custkey | 7 |
|
+----+-------------+------------+-------+---------------+------------+---------+--------------------------+---------+---------------------------------+
3 rows in set (1 min 29.45 sec)
mysql> select
->c_count,
->count(*) as custdist
-> from
->(
-> select
->c_custkey,
->count(o_orderkey)c_count
-> from
->customer left outer join orders on
-> c_custkey = o_custkey
-> and o_comment not like '%unusual%packages%'
-> group by
->c_custkey
->) c_orders
-> group by
->c_count
-> order by
->custdist desc,
->c_count desc limit10;
+---------+----------+
| c_count | custdist |
+---------+----------+
| 0 | 500023 |
|10 |65974 |
| 9 |65196 |
|11 |62247 |
| 8 |58373 |
|12 |55809 |
|13 |49853 |
| 7 |46762 |
|19 |46735 |
|18 |46216 |
+---------+----------+
10 rows in set (1 min 7.73 sec)
mysql>
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
17c
mysql> select
->sum(l_extendedprice) / 7.0 as avg_yearly
-> from (select l_extendedprice, l_partkey ,l_quantity
->from
->lineitem,
->part
->where
->p_partkey = l_partkey
->and p_brand = 'Brand#23'
->and p_container = 'MED CAN'
->)b
->,(select l_partkey,0.2 * avg(l_quantity) p2avg_l_quantity
->from
->(select l_partkey,l_quantity from
->lineitem
->where
->l_partkey in (select p_partkey from part where p_brand = 'Brand#23'and p_container = 'MED CAN')
->)a
->group by l_partkey
->)c
-> where b.l_partkey=c.l_partkey and b.l_quantity;
+----------------+
| avg_yearly |
+----------------+
| 3188455.648571 |
+----------------+
1 row in set (5 min 21.34 sec)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行