How does 'LIMIT' parameter work in sql?

后端 未结 6 1256
余生分开走
余生分开走 2020-12-14 17:52

I have 4000 rows for example, and I define X limit.

The query stops after it finds X rows? or the query finds all the rows and then takes X rows from the found rows?

6条回答
  •  既然无缘
    2020-12-14 18:21

    I'm assuming you're thinking about MySQL, in which according to the documentation, the answer is it depends. If you're using a LIMIT (without a HAVING), then:

    • If you are selecting only a few rows with LIMIT, MySQL uses indexes in some cases when normally it would prefer to do a full table scan.
    • As soon as MySQL has sent the required number of rows to the client, it aborts the query unless you are using SQL_CALC_FOUND_ROWS.

    There are a few other cases which you should read about in the documentation.

提交回复
热议问题