Java and SQL samples of hard choice between performance and complexity.

Image for post

Big O notation looks at the upper bound of the performance of an algorithm to define its worst-case behavior. It looks at algorithms asymptotic behavior and describe the size of the input as represented by n.

I like to work with T-SQL, so I combined together different time complexities.

Certainly, we can use indexes of SQL tables to achieve more speed.

Clustered indexes are physically sorted on the disk, you can have only one per table. Simple indexes are logically sorted and a table can have many of those (for significant fields).

Theory logic of this notation is simple:

Image for post

Let’s say for the beginning, that we have 2 SQL tables: User and Post.

Constant time complexity O(1)

SELECT TOP 1 u.Name FROM User u

If the time taken by the algorithm does not change and remains constant as the input size is increased, then the algorithm has the complexity of O(1) performance. The algorithm doesn’t depend on the size of the input.

SELECT COUNT(*) FROM User

a) it has O(1) based on the count statistics

b) but it will be O(log(n)) complexity in case of using a primary key

c) or O(n) complexity without a primary key

or Java implementation of O(1):

public class MyClass {
	    public static void main(String args[]) {

	       ConstantTimeComplexity(10);
	    }
	    static void ConstantTimeComplexity(int n) {
	       int x=10;
	       int y=25;      
	       int sum = x + n;
	       int multiply = y * n;
	       System.out.println("Sum of x+y = " + sum + ", Multiply: " + multiply);
	    }
	}

#java #performanc #big-o #sql #complexity

Estimate time complexity of Java and SQL query.
2.80 GEEK