Home
Side-effectsSide-effects
One area to consider is that of method call side-effects. For example, to call the length method on a java.io.File object seems to entail an actual call down to the operating system to retrieve the current length of the file. Of course this is a costly operation but for the purposes of a SQL query, it should be assumed that the length does not change. For small numbers of java.io.File objects (say < 100) then making a call to the length method will not affect the performance that much. However if the number of objects is larger, say around 1000 then it does become noticeable. It can often degrade performance by 2 orders of magnitude!

For example: calling the following Query, on a test machine (Linux, Fedora Core 2, 2.6 kernel, 1.4 Java) passing in 33215 java.io.File objects takes around 30s to return!
SELECT *
FROM   java.io.File
WHERE  length > @avg_length
ORDER BY length
LIMIT 1, 1
EXECUTE ON ALL avg (:_allobjs, length) AS avg_length
However, if a "wrapper object" is created to "cache" the information that will be used in the search then the same query will run in around 150ms.

For example, using the class defined below:
public class FileWrapper
{

    long length = 0;
    File f = null;

    public FileWrapper (File f)
    {

        this.f = f;
        this.length = f.length ();

    }

    public long length ()
    {

        return this.length;

    }

    public File getFile ()
    {

        return this.f;

    }

}
And then re-writing the query:
SELECT file
FROM   FileWrapper
WHERE  length > @avg_length
ORDER BY length
LIMIT 1, 1
EXECUTE ON ALL avg (:_allobjs, length) AS avg_length
Will provide the necessary speed-up.

In effect this creates an "index" on the columns you wish to search on.