SQL LIMIT vs. JDBC Statement setMaxRows. Which one is better?

后端 未结 5 1681
离开以前
离开以前 2021-02-08 20:13

I want to select the Top 10 records for a given query. So, I can use one of the following options:

  • Using the JDBC Statement.setMaxRows() method
  • Using LIMI
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 20:18

    not sure if i am right, but i remember in the past i was involved in big project to change all queries that were expected to return one row into 'TOP 1' or numrows=1. Reason was that the DB would stop searching for 'next possible matches' when this 'hint' was used. And in high volume environments this really made a difference. The remark that you can 'ignore' superfluous records in the client or in the resultset is not enough. You should avoid unnecessary reads as early as possible. But i have no idea whether the JDBC methods add those db specific hints to the query y/n. I may need to test however to see and use it ... i am not db specialist and can imagine i am not right, but "Speedwise it seems like no difference" can be a wrong assumption ... E.g. if you are asked to search in box for red balls and you only need one, it does not add value to keep searching for all where for you one is enough ... Then it matters to specify 'TOP 1' ...

提交回复
热议问题