MySQL 视图和索引使用

MySQL Views and index use(MySQL 视图和索引使用)
本文介绍了MySQL 视图和索引使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑开始利用视图来降低我们项目中代码和查询的复杂性——其中一些有一些连接,据我所知,MySQL 视图可以让我们更容易地引用这些数据在多个地方.

I was thinking of starting to utilize views to reduce the complexity of code and queries in our project -- some of them have a few joins, and from what I understand, MySQL views would allow us to reference that data a little easier in multiple places.

有很多东西被抛出,其中MySQL 不为视图使用索引"、你不能有索引视图"、只有你使用 MERGE"......没有明确的-截断答案.

There's a lot of stuff being thrown around, where "MySQL doesn't use indexes for views", "You can't have an indexed view", "Only if you use MERGE"... There is no clear-cut answer.

所以,切入正题:MySQL 视图是否使用索引在它们构建的表上?使用视图是一个坏主意,因为性能会很糟糕,还是在进行连接时会使用基础表上的索引?如果我按在表中编入索引的列对视图进行排序,它的排序速度是否仍像往常一样快?

So, to cut to the chase: Do MySQL views use indexes on the tables they are built from? Is it a bad idea to use views at all because performance will be abysmal, or will it use the indexes on the underlying tables when doing its joins? If I sort a view by a column that is indexed in the table, does it still sort as fast as it normally would?

我的研究似乎表明视图不使用索引,但如果是这样的话,没有人会使用它们;显然人们这样做,所以......?

Doing my research seems to indicate that views don't use indexes, but if that was the case nobody would ever use them; obviously people do, so...?

对不起,如果这看起来有点荒谬.

Sorry if this seems kind of absurd.

推荐答案

如果你查询一个视图,MySQL 考虑在底层表上使用索引.

If you query a view, MySQL will consider using indexes on the underlying tables.

但是,不能向视图中的计算列添加新索引.我认为这就是 MySQL 没有索引视图的意思,而不是(例如)SQL Server 的索引视图.

However it is not possible to add a new index to a calculated column in the view. I think this is what people meant by MySQL not having indexed views, as opposed to (for example) SQL Server's indexed views.

这篇关于MySQL 视图和索引使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)