开发者

Compare strings length equality

开发者 https://www.devze.com 2023-03-21 07:15 出处:网络
I would like to know which of two following operations under indexed varchar field is more efficient (they play as alternatives in my more difficult query):

I would like to know which of two following operations under indexed varchar field is more efficient (they play as alternatives in my more difficult query):

length(field) == 5

or

field == "12345"

First it looked obvious for me that first clause if quicker. But as field is indexed comparison is rather quick. But what about length? I guess index doesn't store information 开发者_高级运维about string length...


The second is far more efficient in an indexed varchar field. Specifically, the restriction criteria (exact match) is far more restrictive than a length restriction; that greater restriction speeds up the selection.


In any case, length comparison is better. Even if that strings are hashed and indexed in any way, their comparison can't become faster than simple integer comparison. Even if length is not stored (but I don't believe that), empty loop while(s[i] != '\0'); for length calculation will be faster then char-by-char loop somparison.

0

精彩评论

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

关注公众号