Thursday, December 20, 2012

How fast is Hibernate and MySQL

I would like to find what is the best practice to build method like public Boolean isUser() when I use Hibernate framework.

For the test I build a simple user table contains 285 141 records.
I use simple select to find a user like

SELECT * FROM mavenweb.users where email="3733847@test.com";

They return 1 row in 218 Millisec.
They return 1 row in 234 Millisec.
They return 1 row in 219 Millisec.

After indexing email column sql query is much faster.

They return 1 row in 031 Millisec.
They return 1 row in 000 Millisec.
They return 1 row in 015 Millisec.

I removed index from email and now we can start testing isUser() in my code.

In first solution I used “find” method from org.springframework.orm.hibernate3.HibernateTemplate and use Hibernate Query Language (HQL):

@Override
    public Boolean isUser(User user) {
        List<User> result =  hibernateTemplate.find("from User where email=?",user.getEmail());
        if(result==null){
            return false;
        }
        return true;
    }

Search:3733847@test.com Time: 343 Millisec. isUser:true
Search:3733847@test.com Time: 343 Millisec. isUser:true
Search:3733847@test.com Time: 358 Millisec. isUser:true

The time it isn’t so bad and comparable to results from sql query ;-).

I change my code a little bit and now I use executeQuery method on import org.hibernate.Session and use native sql.

@Override
    public Boolean isUser(User user) {
        // TODO Auto-generated method stub
        Session session = sessionFactory.getCurrentSession();
        List<User> result =session.createSQLQuery("SELECT * FROM mavenweb.users where email='"+user.getEmail()+"'").list();
        if(result==null ||
result.isEmpty()){
            return false;
        }
        return true;
}
Search:3733847@test.com Time: 270 Millisec. isUser:true
Search:3733847@test.com Time: 290 Millisec. isUser:true
Search:3733847@test.com Time: 310 Millisec. isUser:true

Looks nice. Times is much better than previously so I think is better idea to use native sql in this case.

Let’s check what happen if we revert index in the user table and use HQL.

Search:3733847@test.com Time: 156 Millisec. isUser:true
Search:3733847@test.com Time: 158 Millisec. isUser:true
Search:3733847@test.com Time: 156 Millisec. isUser:true

And again let’s check executeQuery with native SQL

Search:3733847@test.com Time: 78 Millisec. isUser:true
Search:3733847@test.com Time: 70 Millisec. isUser:true
Search:3733847@test.com Time: 78 Millisec. isUser:true

So the winner is…
No.Name Time in milli secdescription
SQL Query in Workbrench 224
SQL Query in Workbrench + index on email 15
1 HQL 348
2 Native sql 290
3 HQL + index on email 1572 time quicker than method 1
4 Native sql + index on email 754 time quicker then method 2

Sitting at the table

janetmck / Foter / CC BY
I had very funny situation.

I should be on the meeting with our client but I was late a little bit. Something about 10 min. The meeting was in Warsaw and I work in department in Katowice. Of course I called to my Project Manager to told him about it. He decided to start a meeting without me. So when I arrived I welcomed everyone and I joined to to the meeting. 
It wasn't first meeting so I knew each other (except one man) and we talked about client requirements. We talked talked but one man who was sitting front of me just sit and never spoke. I have seen him first time and I was very interesting what he is doing here, but I was late so I could know who is he.

After meeting we planned to go to Warsaw office to talk about client requirements. So when the meeting was finished I started exchange handshakes with our clients and suddenly guy who was sitting a front of me when I tried to say goodbye to him He told me: "But I arrived with us !!???"  It was a second shoot in my foot in one day. Everyone started laughing because the guy work in the same organization then I. And we finished a meeting with a smile but why because of me ;).

So remember my friends.

If you are in one team set at the one site of the table (It is a good negotiation practise) and of course never be late ;-)      

The silent man

!borghetti / Foter / CC BY-NC-ND
In previous post I described man who have said nothing on the meeting He just sat. 
I think about a little bit and I remained myself...
I had no plans in my scheduler. The Project Manager invited me to visit the client. He told me  you have some free time so lets go with me to ... we will be talking about solution with you will be responsible for. I thought ok. Maybe I will learn something more and I was.
I was memorable meeting. I seat and just watched. 
Why ??? Because the man who was a Project Manager from client site invite 20 people to talk about  solution.  So it was the biggest meeting where not found anything. 
When I came and look on the meeting room. And count the people I understood that is impassible to  make any conclusion from that.