首页 > 数据库 > 数据库开发技术 > SQL面试题:第二个优化案例
CREATE TABLE `customer` (
`C_CUSTKEY` int NOT NULL,
`C_NAME` varchar(25) NOT NULL,
`C_ADDRESS` varchar(40) NOT NULL,
`C_NATIONKEY` int NOT NULL,
`C_PHONE` char(15) NOT NULL,
`C_ACCTBAL` decimal(15,2) NOT NULL,
`C_MKTSEGMENT` char(10) NOT NULL,
`C_COMMENT` varchar(117) NOT NULL,
PRIMARY KEY (`C_CUSTKEY`)
) ENGINE=InnoDB;
CREATE TABLE `orders` (
`O_ORDERKEY` int NOT NULL,
`O_CUSTKEY` int NOT NULL,
`O_ORDERSTATUS` char(1) NOT NULL,
`O_TOTALPRICE` decimal(15,2) NOT NULL,
`O_ORDERDATE` date NOT NULL,
`O_ORDERPRIORITY` char(15) NOT NULL,
`O_CLERK` char(15) NOT NULL,
`O_SHIPPRIORITY` int NOT NULL,
`O_COMMENT` varchar(79) NOT NULL
PRIMARY KEY (`C_ORDERKEY`)
) ENGINE=InnoDB;
SELECT o_custkey, c_name, sum(o.o_totalprice)
FROM customer c, orders o
WHERE o_custkey = c_custkey
GROUP BY o_custkey, c_name
ORDER BY c_name;
EXPLAIN
select c_custkey, c_name, sum(o.o_totalprice)
from customer c, orders o
where o_custkey = c_custkey
group by c_name, c_custkey
order by c_name;
id|select_type|table|partitions|type |possible_keys|key |key_len|ref |rows|filtered|Extra |
--+-----------+-----+----------+------+-------------+-------+-------+--------------+----+--------+-------------------------------+
1|SIMPLE |o | |ALL | | | | | 1| 100.0|Using temporary; Using filesort|
1|SIMPLE |c | |eq_ref|PRIMARY |PRIMARY|4 |hr.o.O_CUSTKEY| 1| 100.0| |
-- 创建索引
CREATE INDEX idx_orders_custkey_totalprice ON orders(o_custkey, o_totalprice);
EXPLAIN
select o.o_custkey, c.c_name, o.sumprice
from
(select o_custkey, sum(o_totalprice) sumprice
from orders
group by o_custkey) o, customer c
where o_custkey = c_custkey
order by c_name;
id|select_type|table |partitions|type |possible_keys |key |key_len|ref |rows|filtered|Extra |
--+-----------+----------+----------+-----+-----------------------------+-----------------------------+-------+--------------+----+--------+--------------+
1|PRIMARY |c | |ALL |PRIMARY | | | | 1| 100.0|Using filesort|
1|PRIMARY |<derived2>| |ref |<auto_key0> |<auto_key0> |4 |hr.c.C_CUSTKEY| 2| 100.0| |
2|DERIVED |orders | |index|idx_orders_custkey_totalprice|idx_orders_custkey_totalprice|11 | | 1| 100.0|Using index |
-- 创建索引
CREATE INDEX idx_customer_name ON customer(c_name);
EXPLAIN
select c.c_custkey, c.c_name, o.sumprice
from
(select o_custkey, sum(o_totalprice) sumprice
from orders
group by o_custkey) o, customer c
where o_custkey = c_custkey
order by c_name;
id|select_type|table |partitions|type |possible_keys |key |key_len|ref |rows|filtered|Extra |
--+-----------+----------+----------+-----+-----------------------------+-----------------------------+-------+--------------+----+--------+-----------+
1|PRIMARY |c | |index|PRIMARY |idx_customer_name |102 | | 1| 100.0|Using index|
1|PRIMARY |<derived2>| |ref |<auto_key0> |<auto_key0> |4 |hr.c.C_CUSTKEY| 2| 100.0| |
2|DERIVED |orders | |index|idx_orders_custkey_totalprice|idx_orders_custkey_totalprice|11 | | 1| 100.0|Using index|
EXPLAIN
select o_custkey, c_name, sum(o.o_totalprice)
from customer c, orders o
where o_custkey = c_custkey
group by c_name, o_custkey
order by c_name;
id|select_type|table|partitions|type |possible_keys |key |key_len|ref |rows|filtered|Extra |
--+-----------+-----+----------+-----+-----------------------------+-----------------------------+-------+--------------+----+--------+-----------+
1|SIMPLE |c | |index|PRIMARY,idx_customer_name |idx_customer_name |102 | | 1| 100.0|Using index|
1|SIMPLE |o | |ref |idx_orders_custkey_totalprice|idx_orders_custkey_totalprice|4 |hr.c.C_CUSTKEY| 1| 100.0|Using index|
原文作者:SQL编程思想;如有侵权,请联系管理员删除。