Menu

executequery running extremely slow with sort of complicated query

Help
bran
2017-02-17
2017-02-17
  • bran

    bran - 2017-02-17

    Hi everyone - I have a query that selects multiple fields from several different tables using an inner join and two left joins. When I run the exact query in access, the results are instant. When it's run using ucanaccess, it takes nearly four minutes. Can anyone help? Code:

    @Test
    public void read_test() throws SQLException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null; 
        try
       {
            conn = DriverManager.getConnection("jdbc:ucanaccess:localpath/to/db.accdb;keepMirror=C:/mirror/path/theMirror1");
            s = conn.createStatement();
            String qry = "SELECT [AGRMNT-REF], PRIORITY, VENDOR, TYPE, [BASE-COST], UOM, COMPANY, [BASE-PERCENT] FROM ((AGRMNTHDR AS H INNER JOIN AGRMNTLINE AS L ON "
                    + "L.[AGRMNT-REF]=H.[AGRMNT-REF]) LEFT JOIN POAGRMNTPART P ON L.[AGRMNT-REF]=p.[AGRMNT-REF]) LEFT JOIN POAGRMNTPERCENTAGE PP on "
                    + "L.[AGRMNT-REF]=PP.[AGRMNT-REF]";
    
            rs = s.executeQuery(qry);
           while(rs.next())
           {
               System.out.println(rs.getString("UOM"));
           }
    
       }
          finally{
              s.close();
              conn.close();
          }
    
    }
    
     
  • Marco Amadei

    Marco Amadei - 2017-02-18

    Firstly. you have to pay attention: if the access db is updated in any way after the mirror db has been cteated, the mirror db will be re-created at the next connection. So please, measure just the query executuion time:

    @Test
    public void read_test() throws SQLException{
        Connection conn = null;
        Statement s = null;
        ResultSet rs = null; 
        try
       {
            conn = DriverManager.getConnection("jdbc:ucanaccess:localpath/to/db.accdb;keepMirror=C:/mirror/path/theMirror1");
            s = conn.createStatement();
       long time = System.currentTimeMillis();
            String qry = "SELECT [AGRMNT-REF], PRIORITY, VENDOR, TYPE, [BASE-COST], UOM, COMPANY, [BASE-PERCENT] FROM ((AGRMNTHDR AS H INNER JOIN AGRMNTLINE AS L ON "
                    + "L.[AGRMNT-REF]=H.[AGRMNT-REF]) LEFT JOIN POAGRMNTPART P ON L.[AGRMNT-REF]=p.[AGRMNT-REF]) LEFT JOIN POAGRMNTPERCENTAGE PP on "
                    + "L.[AGRMNT-REF]=PP.[AGRMNT-REF]";
    
            rs = s.executeQuery(qry);
            long time1 = System.currentTimeMillis();
                System.out
                        .println("time in millisec:"
                                + (time1 - time) );
           while(rs.next())
           {
               System.out.println(rs.getString("UOM"));
           }
    
       }
          finally{
              s.close();
              conn.close();
          }
    
    }
    
     
  • bran

    bran - 2017-02-20

    Hey Marco - the db hasn't been updated and the output shows a time of: 268394 milliseconds

     

    Last edit: bran 2017-02-20
  • Gord Thompson

    Gord Thompson - 2017-02-20

    Can you confirm that the AGRMNT-REF column is indexed in all four (4) tables? (If you open the database using "console.bat" or "console.sh" you should see the indexes listed as the database loads.)

     
  • bran

    bran - 2017-02-20

    Here's the output

     

    Last edit: bran 2017-02-20
  • Gord Thompson

    Gord Thompson - 2017-02-20

    Certainly worth a try to add indexes on the [AGRMNT-REF] columns in the [POAGRMNTPART] and [POAGRMNTPERCENTAGE] tables to see if that helps.

     
  • bran

    bran - 2017-02-20

    wow, that helped a ton! time now is 1790 millsecs, thanks!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.