开发者

Top 1 faster if only selecting one row

开发者 https://www.devze.com 2023-03-17 03:50 出处:网络
Does the database break the selection loop as fast as it has got one record when using Top 1? So that

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 cust开发者_如何学Comer where cusId = 1234 ?

cusId is unique, so does MSSql understand to do it "faster" without top 1?


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.


In my opinion select * from customer where cusId = 1234 will be more faster..b/c it has one operation less to perform than first one...

0

精彩评论

暂无评论...
验证码 换一张
取 消