Guest User

Untitled

a guest
Dec 6th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. import java.sql.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. try{
  5. Class.forName("org.postgresql.Driver");
  6. } catch (ClassNotFoundException e) {
  7. System.out.println("Can't find driver");
  8. }
  9. Connection connection = null;
  10. try {
  11. connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "948200");
  12. } catch (SQLException e){
  13. e.printStackTrace();
  14. }
  15. if (connection != null) {
  16. // try {
  17. // ResultSet rs = connection.createStatement().executeQuery("SELECT count(*) FROM \"user\"");
  18. // rs.next();
  19. // System.out.println(rs.getString(1));
  20. // } catch (SQLException e) {
  21. // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  22. // }
  23. //add lots of records to table
  24. // for (int i = 0; i < 1000000; i++) {
  25. // String sql_no_indexes = new StringBuilder("INSERT INTO test_users_without_index VALUES (").append(i).
  26. // append(",").append(i).append(",").append(i).append(", '1991-11-11')").toString();
  27. // String sql_indexes = new StringBuilder("INSERT INTO test_users_with_index VALUES (").append(i).
  28. // append(",").append(i).append(",").append(i).append(", '1991-11-11')").toString();
  29. // try {
  30. // System.out.println(i);
  31. // connection.createStatement().executeUpdate(sql_no_indexes);
  32. // connection.createStatement().executeUpdate(sql_indexes);
  33. // } catch (SQLException e) {
  34. // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  35. // }
  36. // }
  37. //try to read data
  38. final Connection cnx = connection;
  39. long result = getMills(new Runnable() {
  40. public void run() {
  41. try {
  42. ResultSet resultSet = cnx.createStatement().
  43. executeQuery("SELECT DISTINCT * FROM test_users_without_index WHERE login = '555555'");
  44. resultSet.next();
  45. } catch (SQLException e) {
  46. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  47. }
  48. }
  49. });
  50. System.out.println("Search without index");
  51. System.out.println(result);
  52. //try to read data
  53. result = getMills(new Runnable() {
  54. public void run() {
  55. try {
  56. ResultSet resultSet = cnx.createStatement().executeQuery("SELECT DISTINCT * " +
  57. "FROM test_users_with_index WHERE login = '555555'");
  58. resultSet.next();
  59. } catch (SQLException e) {
  60. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  61. }
  62. }
  63. });
  64. System.out.println("Search with index");
  65. System.out.println(result);
  66. // try to update data
  67. result = getMills(new Runnable() {
  68. public void run() {
  69. try {
  70. cnx.createStatement().executeUpdate("INSERT INTO test_users_without_index VALUES (1000002, " +
  71. "'111111113', 'password', '1991-01-01')");
  72. } catch (SQLException e) {
  73. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  74. }
  75. }
  76. });
  77. System.out.println("Insert without index");
  78. System.out.println(result);
  79. // try to update data
  80. result = getMills(new Runnable() {
  81. public void run() {
  82. try {
  83. cnx.createStatement().executeUpdate("INSERT INTO test_users_with_index VALUES (1000001, " +
  84. "'111111113', 'password', '1991-01-01')");
  85. } catch (SQLException e) {
  86. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  87. }
  88. }
  89. });
  90. System.out.println("Insert with index");
  91. System.out.println(result);
  92. // delete with index
  93. result = getMills(new Runnable() {
  94. public void run() {
  95. try {
  96. cnx.createStatement().executeUpdate("DELETE FROM test_users_without_index WHERE id=1000001");
  97. } catch (SQLException e) {
  98. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  99. }
  100. }
  101. });
  102. System.out.println("Delete without index");
  103. System.out.println(result);
  104. // delete
  105. result = getMills(new Runnable() {
  106. public void run() {
  107. try {
  108. cnx.createStatement().executeUpdate("DELETE FROM test_users_with_index WHERE id=1000001");
  109. } catch (SQLException e) {
  110. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  111. }
  112. }
  113. });
  114. System.out.println("Delete with index");
  115. System.out.println(result);
  116. }
  117. }
  118.  
  119. private static long getMills(Runnable r) {
  120. long start = System.currentTimeMillis();
  121. r.run();
  122. long end = System.currentTimeMillis();
  123. return end - start;
  124. }
  125. }
Add Comment
Please, Sign In to add comment