Does limiting a query to one record improve performance

前端 未结 6 994
清酒与你
清酒与你 2020-12-06 09:30

Will limiting a query to one result record, improve performance in a large(ish) MySQL table if the table only has one matching result?

for example

 s         


        
6条回答
  •  眼角桃花
    2020-12-06 09:49

    If you have a slightly more complicated query, with one or more joins, the LIMIT clause gives the optimizer extra information. If it expects to match two tables and return all rows, a hash join is typically optimal. A hash join is a type of join optimized for large amounts of matching.

    Now if the optimizer knows you've passed LIMIT 1, it knows that it won't be processing large amounts of data. It can revert to a loop join.

    Based on the database (and even database version) this can have a huge impact on performance.

提交回复
热议问题