--- dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2014/09/11 08:14:40 2200 +++ dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2014/09/29 18:15:48 2230 @@ -14,9 +14,11 @@ import java.util.logging.Logger; + public class Database { Logger logger = Logger.getLogger(Database.class.getName()); + int batchCount = 0; Connection conn; PreparedStatement saveStmt; @@ -42,12 +44,17 @@ } public void renameResultTables() throws SQLException { + Constants consts = Constants.getInstance(); + String ext = consts.getTableExtension(); + logger.info("Dropping old backup table (if exists)"); - String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old"; + String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; conn.createStatement().executeUpdate(sql); logger.info("Rename tables"); - sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute"; + sql = "RENAME TABLE fulddaekning.afstand_anden_rute" + ext + " TO fulddaekning.afstand_anden_rute_old" + ext + ", fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute" + ext; + + logger.info("Executing: " + sql); conn.createStatement().executeUpdate(sql); } @@ -94,15 +101,20 @@ public List hentPostnumre() throws SQLException { ArrayList list = new ArrayList(); + + Constants consts = Constants.getInstance(); String sql = "SELECT postnr " + "FROM fulddaekning.adressetabel " + - "WHERE distributor = ? and rute is not null " + + //"WHERE distributor = ? and rute is not null " + + "WHERE postnr BETWEEN ? AND ? " + "GROUP BY postnr " + "ORDER by postnr"; PreparedStatement stmt = conn.prepareStatement(sql); - stmt.setString(1, Lookup.distributor ); + //stmt.setString(1, Lookup.distributor ); + stmt.setInt(1, consts.getMinPostnr()); + stmt.setInt(2, consts.getMaxPostnr()); ResultSet res = stmt.executeQuery(); while (res.next()) { @@ -117,7 +129,7 @@ return list; } - public ArrayList hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { + public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + "FROM fulddaekning.adressetabel " + "WHERE rute IS NOT NULL " + @@ -137,10 +149,32 @@ stmt.setDouble(4, bbox.longitudeMax); stmt.setString(5, Lookup.distributor); - return hentAdresseListe( stmt ); - + List list = hentAdresseListe( stmt ); + return list.toArray( new Adresse[ list.size() ] ); } + + + public Adresse[] hentAlleDaekkedeAdresser() throws SQLException { + String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + + "FROM fulddaekning.adressetabel " + + "WHERE rute IS NOT NULL " + + "AND latitude IS NOT NULL " + + "AND longitude IS NOT NULL " + + "AND distributor = ? "; + + // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) + // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html + //PreparedStatement stmt = conn.prepareStatement(sql); + PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); + stmt.setFetchSize(Integer.MIN_VALUE); + + stmt.setString(1, Lookup.distributor); + List list = hentAdresseListe( stmt ); + return list.toArray( new Adresse[ list.size() ] ); + } + + public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { /*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ @@ -171,18 +205,27 @@ saveStmt.setDouble(19, bedsteAfstand); - saveStmt.executeUpdate(); - saveStmt.clearParameters(); - + saveStmt.addBatch(); + batchCount++; + if (batchCount >= 100) { + saveStmt.executeBatch(); + batchCount = 0; + } + //saveStmt.executeUpdate(); + //saveStmt.clearParameters(); //saveStmt.close(); - + } + + public synchronized void saveBatch() throws SQLException{ + saveStmt.executeBatch(); + batchCount = 0; } protected ArrayList hentAdresseListe(PreparedStatement stmt) throws SQLException{ - ArrayList list = new ArrayList( 30000 ); + ArrayList list = new ArrayList( 1000000 ); //logger.info("Starting query"); ResultSet res = stmt.executeQuery(); @@ -225,10 +268,11 @@ connectionProps.put("user", db_user); connectionProps.put("password", db_pass); + //For debug output, tilføj denne til JDBC url'en: &profileSQL=true conn = DriverManager.getConnection( "jdbc:mysql://" + db_host + - ":3306/", + ":3306/?rewriteBatchedStatements=true", connectionProps); logger.info("Connected to database"); return conn;