mysql 视图 优化,如何优化MySQL视图

I have some querys using views, and these run a lot slower than I would expect them to given all relevant tables are indexed (and not that large anyway).

I hope I can explain this:

My main Query looks like this (grossly simplified)

select [stuff] from orders as ord

left join calc_order_status as ors on (ors.order_id = ord.id)

calc_order_status is a view, defined thusly:

create view calc_order_status as

select ord.id AS order_id,

(sum(itm.items * itm.item_price) + ord.delivery_cost) AS total_total

from orders ord

left join order_items itm on itm.order_id = ord.id

group by ord.id

Orders (ord) contain orders, order_items contain the individual items associated with each order and their prices.

All tables are properly indexed, BUT the thing runs slowly and when I do a EXPLAIN I get

# id select_type table type possible_keys key key_len ref rows Extra

1 1 PRIMARY ord ALL customer_id NULL NULL NULL 1002 Using temporary; Using filesort

2 1 PRIMARY ALL NULL NULL NULL NULL 1002

3 1 PRIMARY cus eq_ref PRIMARY PRIMARY 4 db135147_2.ord.customer_id 1 Using where

4 2 DERIVED ord ALL NULL NULL NULL NULL 1002 Using temporary; Using filesort

5 2 DERIVED itm ref order_id order_id 4 db135147_2.ord.id 2

My guess is, "derived2" refers to the view. The individual items (itm) seem to work fine, indexed by order _ id. The problem seems to be Line # 4, which indicates that the system doesn't use a key for the orders table (ord). But in the MAIN query, the order id is already defined:

left join calc_order_status as ors on (ors.order _ id = ord.id)

and ord.id (both in the main query and within the view) refer to the primary key.

I have read somewhere than MySQL simpliy does not optimize views that well and might not utilize keys under some conditions even when available. This seems to be one of those cases.

I would appreciate any suggestions. Is there a way to force MySQL to realize "it's all simpler than you think, just use the primary key and you'll be fine"? Or are views the wrong way to go about this at all?

解决方案

If it is at all possible to remove those joins remove them. Replacing them with subquerys will speed it up a lot.

you could also try running something like this to see if it has any speed difference at all.

select [stuff] from orders as ord

left join (

create view calc_order_status as

select ord.id AS order_id,

(sum(itm.items * itm.item_price) + ord.delivery_cost) AS total_total

from orders ord

left join order_items itm on itm.order_id = ord.id

group by ord.id

) as ors on (ors.order_id = ord.id)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值