MySQL是否因查询列数过多而变慢?

huangapple go评论66阅读模式
英文:

Does mysql slow down because there are too many columns to query?

问题

数据库集群索引是否存储整行数据?如果有许多列,索引仍然查看整行数据。查询中有更多列是否会导致 MySQL 查询变慢?

select * from tableName

英文:

I have a question, does the database cluster index store the whole row of data? So if you have a lot of columns, the index is still looking at the whole row of data. So does having more columns in a query cause mysql queries to slow down?

select * from tableName

答案1

得分: 0

是的,只有在查询中存在一些条件时才会使用索引。
例如,在您的查询中存在WHERE type = 'admin',并且在您的表中存在type列的索引,且该列具有有限的不同数值时,它将使用索引。

参考链接:https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html

如果您的查询是SELECT * FROM tableName,那么它无论如何都必须遍历整个表。即使存在索引,也没有使用的必要。

只有当它必须搜索某些记录的子集时,使用条件进行筛选,它才会决定使用哪个索引以便遍历最少数量的记录以找到您要查找的内容。

英文:

Yes, an index will only be used when there is some criteria in your query.
E.g. WHERE type = 'admin' is there in your query,
And there is an index on type column in your table, and it has limited distinct values, then it will use index.

Refer: https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html

If your query is SELECT * FROM tableName, then it has to go through the whole table anyway. There is no point in using any index, even if those are there.

Only when it has to search for some subset of records, using where criteria, it will decide which index might help in going through the least amount of records to find what you are looking for.

答案2

得分: 0

假设您正在使用InnoDB作为存储引擎。这是MySQL的默认设置。

  • 是的,所有列都存储在聚簇索引中。整个行存储在同一个InnoDB页面中,除了BLOB/TEXT/JSON列,它们可能会溢出到额外的页面。

  • 由于InnoDB以页面为单位读取数据,一旦从行中读取一个列,剩下的行也包括在那次读取中。与最初的页面读取相比,读取所有列的额外成本可以忽略不计。

    类比一下,如果您正在阅读一本书,找到了包含您要查找的一个句子的页面,那么在那里的同时阅读页面的其余部分需要非常少的努力。

  • 由于BLOB/TEXT/JSON列可能足够大以溢出到额外的页面,当您不需要这些列时执行SELECT *确实会增加成本,因为InnoDB需要读取额外的页面。这是避免使用SELECT *并更具体地仅命名每个查询所需列的一个很好的理由。

英文:

Assuming you are using InnoDB as the storage engine. This is the default in MySQL.

  • Yes, all the columns are stored together in the clustered index. The whole row is stored in the same InnoDB page. Except for BLOB/TEXT/JSON, which may overflow to additional pages.

  • Since InnoDB reads data in units of pages, once you read one column from the row, the rest of the row is also included in that read. The extra cost of reading all the columns is negligible compared to initially reading the page.

    By analogy, if you are reading a book and you find a page that has the one sentence you are looking for, it's very little effort to read the rest of that page while you're there.

  • Since BLOB/TEXT/JSON columns may be large enough to overflow to additional pages, doing SELECT * when you don't need those columns does cost more, because InnoDB needs to read the additional pages. This can be a good reason to avoid using SELECT *, and be more specific to name only the columns you need per query.

huangapple
  • 本文由 发表于 2023年4月11日 11:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定