Does the database break the selection loop as fast as it has got one record when using Top 1?
So that
select top 1 * from customer where cusId = 1234
is faster than
select * from customer where cusId = 1234
?
cusId
is unique, so does MSSql understand to do it "faster" without top 1
?
Typically, these are accomplished using the TOP or LIMIT clause. Problem is, Top N result sets are limited to the highest values in the table, without any grouping. The GROUP BY clause can help with that, but it is limited to the single top result for each group.
SELECT TOP 1 1 will select exactly 0 or 1 1 s. SELECT 1 will select 1 exactly N rows, where N is the number of rows that match your criteria.
To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.
Answer: In Order by clause you can use a number which will indicate the ordinal position of the column name used in the select statement. For example Order by 2 means order by the second column values specified in the SELECT statement.
If cusId
is a primary key, both should be the same re: performance.
EDIT:
You are only adding overhead with TOP 1 if you have unique index that will return 1 result anyway.
It will be different if you have order by something than you are interested in only one row.
MORE:
There is no looping involved, unless there is a table scan involved and there is no index at all for cusId
. In that case, TOP 1
can't help you anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With