3

I was wondering if any of you could help me identify why my simple test applications are giving me so different results. I made two test programs that makes a simple database query and loop through the results. The first one was made by using PHP the second was made with Java.

I knew that Java would perform better, but I have hard time believing that java would perform almost 20 times better (see results below).

PHP:

$sql = "select * from message where user_id=20";
$db = get_PDO(); 
$stm = $db->prepare($sql);
for($i=0;$i<10000;$i++)
{
    echo $i."\n";
    $res = $stm->execute();
    $rows = $stm->fetchAll();
}
echo "done";

The get_PDO is just a function that connects to the database and returns an pdo object.

Java:

public class Connect
{
    public static void main (String[] args)
    {
        Connection conn = null;
        Statement st= null;

        try
        {
            String userName = ""; // db username
            String password = ""; // db password
            String url = "jdbc:mysql://localhost/test"; //test = db name
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server: "+e.toString());
        }
        finally
        {
            if (conn != null)
            {
                String query="select * from message where user_id=20";

                try{
                    st = conn.createStatement();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    try{
                        conn.close();
                    }
                    catch(Exception er) {}
                    return;
                }

                for(int i=0;i<10000;i++)
                {
                    System.out.println(i);
                    try   
                    {
                        ResultSet rs = st.executeQuery(query);
                        while(rs.next())
                        {
                        }
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                try
                {
                    conn.close ();
                    System.out.println ("Database connection terminated");
                }
                catch (Exception e) { /* ignore close errors */ }
            }
        }
    }
}

The results:

I measured the performance using time. (ie. time php test.php and time java Connect)

PHP:

real    1m34.337s
user    1m32.564s
sys     0m0.716s

Java:

real    0m5.388s
user    0m4.428s
sys     0m0.972s

Is java really that much faster or have I done something stupid? :)

5
  • No clue really, but perhaps java does some kind of result caching with "st.executeQuery(query)"?
    – aderuwe
    May 27, 2013 at 10:50
  • Hmh. I moved the createStatement() part inside the loop. Results changed a bit. Now it gives the following results with java: real 0m13.731s user 0m44.996s sys 0m3.740s
    – hefa
    May 27, 2013 at 11:02
  • Oh my bad. I didn't add st.close() anywhere inside that loop. After that the time consumed dropped back to real 0m6.542s user 0m5.852s sys 0m0.848s
    – hefa
    May 27, 2013 at 11:19
  • There's really no use of moving the createStatement() inside the loop. You don't do it in PHP either.
    – Axel
    May 27, 2013 at 12:18
  • I was just trying to see if the statement object caches the queries. My theory was that if I would delete and create new statement objects I would end up not having caching. That would have answered part of my question :)
    – hefa
    May 27, 2013 at 12:36

1 Answer 1

1
  1. Depending on the number of messages, PHP might need much more memory because the complete result set is held after calling fetchAll().
  2. AFAIK PHP creates an associative Array for the result, which might also be time consuming.
  3. It could be that in PHP, you retrieve all the data from the database by calling fetchAll() whereas in Java, you just move through the result set using rs.next() without actually reading. Depending on the JDBC driver implementation, this might give an opportunity for optimization that's not possible in the way the PHP version is implemented.
1
  • Thanks. 1. I changed the PHP to use fetch and got 40 seconds off. Now the php takes: real 0m43.256s user 0m41.048s sys 0m1.452s. 2. I didn't come up with a way to change this behavour. Do you know any? 3. I added j = rs.getInt("id"); inside the Java result set handling. There was no effect.
    – hefa
    May 27, 2013 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.