Skip to main content

Posts

Optimize MySQL query with STRAIGHT_JOIN

Before finalizing my queries, I use MySQL Workbench to explain it to make sure the performance is good. I always notice that the performance of the queries with joins on multiple tables varies at times. Then I start indexing my tables to make sure that the Explain is happy and scanning minimum records before handing over the data. But with one of my query, even after adding an appropriate index I was not getting the performance I was expecting. I asked Workbench to explain the query and what? MySQL was not using my newly added index which could optimize the query. I tried FORCE INDEX and MySQL liked it. It started using the index which I forced it to. But I was not happy at forcing MySQL to use my index. I wanted it to do it itself. After researching a bit, I came across STRAIGHT_JOIN and without wasting any time, I added it to my query and what? MySQL started using my index without being forced to. Now the question was, how the STRAIGHT_JOIN works and is different from INNER JOIN. Wit
Recent posts

How to Copy files to remote server using Ant Build

While working on one of the project, I was tired of copying the files manually to remote server using SFTP tools. I was wondering if Ant Builder is able to copy the files from one directory to other, it should also be able to copy to remote server. After googling a bit, I found a bunch of answers, suggestions and finally, it worked for me. The files from my local machine are now getting copied to remote server with Ant Build. I just build my project in Eclipse and files get pushed to the server. The only thing I need to do is; refresh the web page as the server development became the local development for me now. Below are the step-wise instructions to Copy files to remote server using Ant Build: Download jsch.jar 0.1.55 jar. Add jsch.jar 0.1.55 jar to C:\eclipse_folder_path\plugins\org.apache.ant_1.10.8.v20200515-1239\lib folder. Go into the Eclipse Preferences, in the Ant/Runtime entry, in the 'Classpath' tab. Use the button 'Add External Jars' to add your jsch.jar to

How to schedule a periodic task in Java?

To schedule a task periodically in java: 1. Create an object of the Timer class.     Timer timer = new Timer(); 2. Create an object of the TimerTask class and override the run() method of it.     TimerTask task = new TimerTask() {     @Override     public void run() {            //call your task here             }    }; 3. Call the schedule method of the Timer.     timer.schedule(task, delay, period);     Here:     a) task - task to be scheduled. Pass the task object we have created in step 2.     b) delay - delay in milliseconds before task is to be executed.     c) period - time in milliseconds between successive task executions.

In Java, how does System.out.println() work?

In Java, the dot operator can only be used to call methods and variables so we know that ‘out’ must be either a method or a variable. Now, how do we categorize ‘out’? Well, ‘out’ could not possibly be a method because of the fact that there are no parentheses – the ‘( )’ – after ‘out’, which means that out is clearly not a method that is being invoked. And, ‘out’ does not accept any arguments because only methods accept arguments – you will never see something like “System.out(2,3).println”. This means ‘out’ must be a variable. What is “out” in System.out.println()? We now know that ‘out’ is a variable, so we must now ask ourselves what kind of variable is it? There are two possibilities – it could be a static or an instance variable. Because ‘out’ is being called with the ‘System’ class name itself, and not an instance of a class (an object), then we know that ‘out’ must be a static variable, since only static variables can be called with just the class name itself. So now we kn

In Java, what’s the difference between method overloading and method overriding?

The difference between overriding and overloading in Java is a common source of confusion – but it is fairly simple to understand. Let’s start the discussion by talking more about method overloading. Overloading in Java  can  occur when two or more methods in the same class share the same name or even if a child class shares a method with the same name as one of it’s parent classes. But, in order to  actually  have overloaded methods, the methods not only have to have the same name, but there are other conditions that must be satisfied – read below to see what those conditions are. Suppose we have a class called TestClass which has 2 methods, and both methods have the  same  name – let’s say that name is “someMethod” – this would be considered to be method overloading if  at least one of these 2 things is true: 1.) The number of parameters is different for the methods 2.) The parameter types are different. How to NOT overload methods: It’s important to underst

In Java, what’s the difference between an object and a class?

The terms ‘class’ and ‘object’ are absolutely related to one another, but each term holds its own distinct meaning. The term ‘class’ refers to the actual written piece of code in which the class is defined. A class is a static piece of code that consists of attributes which don’t change during the execution of a program – like the method definitions within a class. An object is an instance of a class The term ‘object’, however, refers to an actual  instance  of a class. Every object must belong to a class. Objects are created and eventually destroyed – so they only live in the program for a limited time. While objects are ‘living’ their properties may also be changed signficantly. An example will help clarify what we’ve said. Suppose we have a class called ‘Animal’. All Animals have bodies and brains. So, this general ‘template’ of an Animal does not change. An instance of the Animal class would be a  specific  animal – like a lion, a cat, or a zebra. These instances of t

What’s the difference between an interface and an abstract class in Java?

A class must be declared  abstract  when it has one or more abstract  methods . A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do. When to use abstract methods in Java? Why you would want to declare a method as abstract is best illustrated by an example. Take a look at the code below: /* the Figure class must be declared as abstract because it contains an abstract method */ public abstract class Figure { /* because this is an abstract method the body will be blank */ public abstract float getArea(); } public class Circle extends Figure { private float radius; public float getArea() { return (3.14 * (radius * 2)); } } public class Rectangle extends Figure { private float length, width; public float getArea(Figure other) { return length * width; } } In the Figure class above, we have an abstract method called getAre