Advertisement
_Josef_

SQL speed test

Jan 11th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8.  
  9. /*
  10.  * @author Josef
  11.  *
  12.  */
  13.  
  14. public class Main {
  15.  
  16.     public static void main(String[] args) {
  17.         try {
  18.             Connection con = DriverManager.getConnection(
  19.                     "jdbc:mysql://127.0.0.1:3308/test", "root", "password");
  20.             PreparedStatement statement;
  21.             ResultSet res;
  22.             ArrayList<TestObject> locations = new ArrayList<TestObject>();
  23.             ArrayList<TestObject> fetchedLocations;
  24.             String[] worlds = { "Bobs World", "Potatoland", "Baconcity",
  25.                     "Cocoa Bean", "Feelsville", "Sweden" };
  26.             long time;
  27.             long doneTime;
  28.             int times = 1;
  29.  
  30.             /* Creating 100 random made-up cords before starting test */
  31.             for (int c = 0; c < times; c++) {
  32.                 Random r = new Random();
  33.                 locations.add(new TestObject(r.nextDouble() * 20000 - 10000, r
  34.                         .nextDouble() * 20000 - 10000,
  35.                         r.nextDouble() * 20000 - 10000, worlds[r.nextInt(5)], r
  36.                                 .nextFloat() * 360 - 180, r.nextFloat() * 180));
  37.             }
  38.  
  39.             for (int i = 0; i < 5; i++) {
  40.                 /* Clear tables from previous tries */
  41.                 statement = con
  42.                         .prepareStatement("DELETE FROM othertest WHERE 1;");
  43.                 statement.executeUpdate();
  44.                 statement = con
  45.                         .prepareStatement("DELETE FROM testtable WHERE 1;");
  46.                 statement.executeUpdate();
  47.                
  48.                 /* Saving then fetching as combined String */
  49.                 fetchedLocations = new ArrayList<TestObject>();
  50.                 System.out.println("Starting combined at "
  51.                         + System.currentTimeMillis());
  52.                 time = System.currentTimeMillis();
  53.                 for (int c = 0; c < times; c++) {
  54.                     TestObject l = locations.get(c);
  55.                     statement = con
  56.                             .prepareStatement("INSERT INTO othertest (loc) VALUES (?);");
  57.                     statement.setString(1, l.x + ":" + l.y + ":" + l.z + ":"
  58.                             + l.w + ":" + l.ya + ":" + l.p);
  59.                     statement.executeUpdate();
  60.                 }
  61.                 for (int c = 0; c < times; c++) {
  62.                     statement = con.prepareStatement("SELECT * FROM othertest");
  63.                     res = statement.executeQuery();
  64.                     while (res.next()) {
  65.                         String[] a = res.getString("loc").split(":");
  66.                         fetchedLocations.add(new TestObject(Double
  67.                                 .parseDouble(a[0]), Double.parseDouble(a[1]),
  68.                                 Double.parseDouble(a[2]), a[3], Float
  69.                                         .parseFloat(a[4]), Float
  70.                                         .parseFloat(a[5])));
  71.                     }
  72.                 }
  73.                 doneTime = System.currentTimeMillis();
  74.                 System.out.println("Stopped combined at "
  75.                         + System.currentTimeMillis() + " (Total of "
  76.                         + (doneTime - time) + ")");
  77.  
  78.                 /* Saving then fetching as individual variables */
  79.                 fetchedLocations = new ArrayList<TestObject>();
  80.                 System.out.println("Starting individual at "
  81.                         + System.currentTimeMillis());
  82.                 time = System.currentTimeMillis();
  83.                 for (int c = 0; c < times; c++) {
  84.                     TestObject l = locations.get(c);
  85.                     statement = con
  86.                             .prepareStatement("INSERT INTO testtable (x, y, z, world, yaw, pitch) VALUES (?, ?, ?, ?, ?, ?);");
  87.                     statement.setString(1, l.x + "");
  88.                     statement.setString(2, l.y + "");
  89.                     statement.setString(3, l.z + "");
  90.                     statement.setString(4, l.w);
  91.                     statement.setString(5, l.ya + "");
  92.                     statement.setString(6, l.p + "");
  93.                     statement.executeUpdate();
  94.                 }
  95.                 for (int c = 0; c < times; c++) {
  96.                     statement = con.prepareStatement("SELECT * FROM testtable");
  97.                     res = statement.executeQuery();
  98.                     while (res.next()) {
  99.                         fetchedLocations
  100.                                 .add(new TestObject(res.getDouble("x"), res
  101.                                         .getDouble("y"), res.getDouble("z"),
  102.                                         res.getString("world"), res
  103.                                                 .getFloat("yaw"), res
  104.                                                 .getFloat("pitch")));
  105.                     }
  106.                 }
  107.                 doneTime = System.currentTimeMillis();
  108.                 System.out.println("Stopped individual at "
  109.                         + System.currentTimeMillis() + " (Total of "
  110.                         + (doneTime - time) + ")");
  111.             }
  112.  
  113.         } catch (SQLException e) {
  114.             e.printStackTrace();
  115.         }
  116.     }
  117. }
  118.  
  119. class TestObject {
  120.  
  121.     public double x, y, z;
  122.     public float ya, p;
  123.     public String w;
  124.  
  125.     TestObject(double x, double y, double z, String w, float ya, float p) {
  126.         this.x = x;
  127.         this.y = y;
  128.         this.z = z;
  129.         this.w = w;
  130.         this.ya = ya;
  131.         this.p = p;
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement