I've to add row number in my existing query so that I can track how much data has been added into Redis. If my query failed so I can start from that row no which is updated in other table.
Query to get data start after 1000 row from table
SELECT * FROM (SELECT *, ROW_NUMBER() OVER (Order by (select 1)) as rn ) as X where rn > 1000
Query is working fine. If any way that I can get the row no without using order by.
What is select 1
here?
Is the query optimized or I can do it by other ways. Please provide the better solution.
You can use any literal value
ex
etc
Refer this for more information https://exploresql.com/2017/03/31/row_number-function-with-no-specific-order/
There is no need to worry about specifying constant in the
ORDER BY
expression. The following is quoted from the Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions written byItzik Ben-Gan
(it was available for free download from Microsoft free e-books site):The above means that when you are using constant ordering is not performed. I will strongly recommend to read the book as
Itzik Ben-Gan
describes in depth how the window functions are working and how to optimize various of cases when they are used.Try just
order by 1
. Read the error message. Then reinstate theorder by (select 1)
. Realise that whoever wrote this has, at some point, read the error message and then decided that the right thing to do is to trick the system into not raising an error rather than realising the fundamental truth that the error was trying to alert them to.Tables have no inherent order. If you want some form of ordering that you can rely upon, it's up to you to provide enough deterministic expression(s) to any
ORDER BY
clause such that each row is uniquely identified and ordered.Anything else, including tricking the system into not emitting errors, is hoping that the system will do something sensible without using the tools provided to you to ensure that it does something sensible - a well specified
ORDER BY
clause.