What's the most efficient way to select the last n rows in a table without changing the table's structure?

后端 未结 8 1850
天命终不由人
天命终不由人 2020-12-10 02:25

What\'s the most efficient way to select the last n number of rows in a table using mySQL? The table contains millions of rows, and at any given time I don\'t know how large

8条回答
  •  时光取名叫无心
    2020-12-10 02:32

    Maybe order it by the unique id descending:

    SELECT * FROM table ORDER BY id DESC LIMIT n
    

    The only problem with this is that you might want to select in a different order, and this problem has made me have to select the last rows by counting the number of rows and then selecting them using LIMIT, but obviously that's probably not a good solution in your case.

提交回复
热议问题