在sql中,调用length(VARCHAR column)是O(1)还是O(length)?
相关:VARCHAR 字符串是存储为长度前缀字符串(也称为 pascal 字符串)还是以 null 结尾的字符串?
我关心的实现是 sqlite 和 postgresql,但请随意回答其他实现。
最佳答案
根据 Postgres documentation :
The storage requirement for a short string (up to 126 bytes) is 1 byte plus the actual string, which includes the space padding in the case of character. Longer strings have 4 bytes of overhead instead of 1.
这强烈表明 Postgres 与许多其他数据库一样,对值使用长度编码。即第一个字节或四个字节存储字符串的长度。之后数据存储在数据库页面上。
使用这种存储结构,长度函数的任何合理实现都将是 O(1)。
关于sql - 在sql中,长度函数是O(1)还是O(length),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23656203/