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)

课程大纲: 第1课 数据库与关系代数 综述数据库、关系代数、查询优化技术 综述数据库调优技术 预计时间1小时 第2课 数据库查询优化技术总揽 综述查询优化技术范围,包括查询重用、查询重写规则、查询算法优化、并行查询优化等 综述逻辑查询优化,包括子查询的优化视图重写、等价谓词重写、条件化简、连接消除、非SPJ的优化等 综述逻辑物理优化,包括单表扫描算法、两表连接算法、多表连接算法、基于代价的算法等 初步理解MySQL的查询执行计划。 预计时间1小时 第3课 查询优化技术理论与MySQL实践(一)------子查询的优化(一) 第4课 查询优化技术理论与MySQL实践(二)------子查询的优化(二) 从理论看,子查询包括的内容和范围,建立清晰的概念 从实践看,MySQL的子查询优化技术的内容和范围,明确掌握子查询优化手段 预计时间2小时,每小时一个课程段(子查询是SQL查询优化的重点内容,务必掌握好) 第5课 查询优化技术理论与MySQL实践(三)------视图重写与等价谓词重写 什么是视图重写?哪些类型的视图可以被优化MySQL是怎么优化视图的?从而明白在MySQL中怎么写与视图相关的查询语句才能有好的效果? 什么是等价谓词重写?MySQL中怎么写WHERE子句有利于提高查询效率? 预计时间1小时 第6课 查询优化技术理论与MySQL实践(四)------条件化简 什么是条件化简?MySQL中对什么样的条件自动进行优化?如何写出可利用索引的条件语句? 预计时间1小时 第7课 查询优化技术理论与MySQL实践(五)------外连接消除、嵌套连接消除与连接消除 连接方式有些什么类型?不同类型的连接又是怎么优化的?外连接优化的条件是什么?MySQL中怎么写出可优化的连接语句?MySQL是否支持嵌套连接消除?MySQL是否支持连接消除?MySQL中书写SQL连接查询语句时的优化技巧。 预计时间1小时 第8课 查询优化技术理论与MySQL实践(六)------数据库的约束规则与语义优化 数据库的参照完整性(CHECKt NULL等)。什么是语义优化MySQL是否支持语义优化?怎么利用语义优化的思路人工进行SQL语句的优化? 预计时间1小时 第9课 查询优化技术理论与MySQL实践(七)------非SPJ的优化 什么是非SPJ优化? 从理论看,GROUP BY、ORDER BY、LIMIT、DISTINCT等怎么被优化MySQL中:GROUP BY是怎么优化的?ORDER BY是怎么被优化?LIMIT是怎么被优化?DISTINCT是怎么被优化? 非SPJ优化与索引的关系。 预计时间1小时 第10课 MySQL物理查询优化技术概述 从理论看,物理查询优化技术的范围。 从MySQL实践看,怎么利用物理查询优化技术对SQL查询语句调优? 本节预计会承接第9课的部分内容。 预计时间1小时 第11课 MySQL索引的利用、优化MySQL索引的角度出发,看各种SQL查询语句的优化怎么进行?(以前都是从语句的角度看怎么优化,现在站在索引的角度去总结SQL查询语句的优化) 预计时间1小时 第12课 表扫描与连接算法与MySQL多表连接优化实践 MySQL的单表扫描算法。MySQL的两表连接算法。MySQL的多表连接算法。 MySQL的多表连接的优化技巧。 预计时间1小时 第13课 查询优化的综合实例(一)------TPCH实践(一) 第14课 查询优化的综合实例(一)------TPCH实践(二) 以TPC-H国际标准的22条查询语句为实例,综合前面课程的内容,把所学的知识用于实践,进行综合的实战演练。 预计时间2小时(每个课时为1个小时) 第15课 关系代数对于数据库的查询优化的指导意义------查询优化技术总结 再次回到理论,从理论的高度总结关系代数理论与MySQL查询优化实践的关系。真正认识、掌握MySQL的查询优化技术,大步流星步入查询优化的高手之列。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值