So I have a normal UserEntity
class along with a normal UserRepository
interface. All with proper annotation (I hope).
I am curious as to what would be the time complexity of these queries:
userRepository.findById(id)
and
userRepository.findByPublicId(publicId)
As you can see the first one is using the primary key id
and the second one is using the column publicId
.
I have a database with 44,356 fake UserEnities and somehow the speed for both of these is generally the same, even after fresh runs. Of course, this is good but how is this possible when one is supposed to be o(log(n))
and the other is o(n)
Here are the details of the objects:
UserEntity
:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Email
@NotEmpty(message = "Email is required")
private String email;
@NotBlank(message = "Username is required")
private String username;
@NotBlank(message = "Profile name is required")
private String profileName;
}
UserRepository
:
@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {
Optional<UserEntity> findByUsername(String username);
Optional<UserEntity> findByPublicId(String publicId);
Optional<UserEntity> findById(String id);
}