简体   繁体   English

在MySQL中避免全表扫描

[英]Avoiding a Full Table Scan in MySQL

如何避免在mysql上进行全表扫描?

通常,通过确保在WHEREJOINORDER BY子句中出现的字段上有可用的索引。

Index your data. 索引您的数据。

Write queries that use those indexes. 编写使用这些索引的查询。

Anything more than that we need specifics. 除此之外我们还需要具体细节。

另请注意,有时您无法摆脱全表扫描,即当您需要表中的所有行...或者扫描索引的成本是扫描整个表的成本时。

Use a LIMIT clause when you know how many rows you are expecting to return, for example if you are looking for a record with a known ID field that is unique, limit your select to 1, that way mysql will stop searching after it finds the first record. 当您知道要返回多少行时使用LIMIT子句,例如,如果您要查找具有唯一的已知ID字段的记录,请将select限制为1,这样mysql将在找到后停止搜索第一记录。 The same goes for updates and deletes. 更新和删除也是如此。

SELECT * FROM `yourTable` WHERE `idField` = 123 LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM