Skip to content

Commit 3678fa6

Browse files
committed
提升查询MySQL性能
1 parent b767d6c commit 3678fa6

3 files changed

Lines changed: 76 additions & 36 deletions

File tree

‎APIJSON(Server)/src/main/java/zuo/biao/apijson/server/ServerGet2.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public JSONObject get(String json) {
4343
* "Comment[]":[{...}, ...]
4444
*/
4545

46+
// SelectTable2.getInstance().close();
47+
SelectTable3.getInstance().close();
48+
4649
return requestObject;
4750
}
4851

@@ -366,7 +369,7 @@ private JSONObject getJSONObject(JSONObject object, String key) {
366369
*/
367370
private synchronized JSONObject getSQLObject(QueryConfig config) {
368371
System.out.println("getSQLObject config = " + JSON.toJSONString(config));
369-
return SelectTable2.select(config);//SelectTable3.getInstance().select(config);//
372+
return SelectTable3.getInstance().select(config);//SelectTable2.getInstance().select(config);//
370373
}
371374

372375
/**获取查询配置

‎APIJSON(Server)/src/main/java/zuo/biao/apijson/server/sql/SelectTable2.java‎

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package zuo.biao.apijson.server.sql;
22

3-
import java.sql.Connection;
4-
import java.sql.DatabaseMetaData;
5-
import java.sql.DriverManager;
6-
import java.sql.ResultSet;
7-
import java.sql.Statement;
3+
import java.sql.*;
84
import java.util.ArrayList;
95
import java.util.List;
106

@@ -21,37 +17,67 @@ public static void main(String[] args){
2117
// System.out.println(TAG + JSON.toJSONString(select("stu")));
2218
}
2319

24-
public static Connection getConnection() throws Exception {
20+
private static Connection connection;
21+
private static Statement statement;
22+
private static DatabaseMetaData metaData;
23+
private SelectTable2() {
24+
}
25+
26+
private static SelectTable2 instance;
27+
public static synchronized SelectTable2 getInstance() {
28+
if (instance == null) {
29+
instance = new SelectTable2();
30+
}
31+
return instance;
32+
}
33+
34+
35+
public Connection getConnection() throws Exception {
2536
//调用Class.forName()方法加载驱动程序
2637
Class.forName("com.mysql.jdbc.Driver");
2738
System.out.println(TAG + "成功加载MySQL驱动!");
2839
String url="jdbc:mysql://localhost:3306/sys"; //JDBC的URL
2940
return DriverManager.getConnection(url, "root", "199531tommy");
3041
}
3142

32-
33-
public static JSONObject select(String table) {
34-
return select(new QueryConfig(table));
43+
public void close() {
44+
try {
45+
statement.close();
46+
connection.close();
47+
} catch (SQLException e) {
48+
e.printStackTrace();
49+
}
50+
metaData = null;
51+
statement = null;
3552
}
36-
public static JSONObject select(QueryConfig config) {
53+
54+
public JSONObject select(QueryConfig config) {
3755
if (config == null || StringUtil.isNotEmpty(config.getTable(), true) == false) {
3856
System.out.println(TAG + "select config==null||StringUtil.isNotEmpty(config.getTable(), true)==false>>return null;");
3957
return null;
4058
}
59+
final String sql = config.getSQL();
60+
4161
try{
42-
Connection conn = getConnection();
43-
Statement stmt = conn.createStatement(); //创建Statement对象
62+
63+
4464
System.out.println(TAG + "成功连接到数据库!");
4565

46-
List<String> list = getColumnList(config.getTable(), conn.getMetaData());
66+
if (connection == null || connection.isClosed()) {
67+
System.out.println(TAG + "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
68+
connection = getConnection();
69+
statement = connection.createStatement(); //创建Statement对象
70+
metaData = connection.getMetaData();
71+
}
72+
73+
List<String> list = getColumnList(config.getTable());
4774
if (list == null || list.isEmpty()) {
4875
return null;
4976
}
5077

51-
String sql = "select * from " + config.getTable() + config.getWhereString() + config.getLimitString(); //要执行的SQL
5278
System.out.println(TAG + "select sql = " + sql);
53-
54-
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
79+
80+
ResultSet rs = statement.executeQuery(sql);//创建数据对象
5581

5682
JSONObject object = null;//new JSONObject();//null;
5783
int position = -1;
@@ -74,8 +100,6 @@ public static JSONObject select(QueryConfig config) {
74100
}
75101

76102
rs.close();
77-
stmt.close();
78-
conn.close();
79103

80104
return object;
81105
} catch(Exception e) {
@@ -90,11 +114,11 @@ public static JSONObject select(QueryConfig config) {
90114
* @param table
91115
* @return
92116
*/
93-
public static List<String> getColumnList(String table, DatabaseMetaData meta) {
117+
public List<String> getColumnList(String table) {
94118
List<String> list = new ArrayList<String>();
95119
ResultSet rs;
96120
try {
97-
rs = meta.getColumns("sys", null, table, "%");
121+
rs = metaData.getColumns("sys", null, table, "%");
98122
while (rs.next()) {
99123
System.out.println(TAG + rs.getString(4));
100124
list.add(rs.getString(4));

‎APIJSON(Server)/src/main/java/zuo/biao/apijson/server/sql/SelectTable3.java‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package zuo.biao.apijson.server.sql;
22

3-
import java.sql.Connection;
4-
import java.sql.DatabaseMetaData;
5-
import java.sql.DriverManager;
6-
import java.sql.ResultSet;
7-
import java.sql.Statement;
3+
import java.sql.*;
84
import java.util.ArrayList;
95
import java.util.HashMap;
106
import java.util.List;
@@ -20,7 +16,7 @@ public class SelectTable3 {
2016

2117
private Map<String, List<JSONObject>> cacheMap;
2218
private SelectTable3() {
23-
cacheMap = new HashMap<String, List<JSONObject>>();
19+
2420
}
2521

2622
private static SelectTable3 instance;
@@ -59,7 +55,21 @@ private JSONObject getFromCache(String key, int position) {
5955
return list == null || position < 0 || position >= list.size() ? null : list.get(position);
6056
}
6157

62-
58+
private static Connection connection;
59+
private static Statement statement;
60+
private static DatabaseMetaData metaData;
61+
public void close() {
62+
try {
63+
statement.close();
64+
connection.close();
65+
} catch (SQLException e) {
66+
e.printStackTrace();
67+
}
68+
metaData = null;
69+
statement = null;
70+
cacheMap = null;
71+
}
72+
6373
public JSONObject select(QueryConfig config) {
6474
if (config == null || StringUtil.isNotEmpty(config.getTable(), true) == false) {
6575
System.out.println(TAG + "select config==null||StringUtil.isNotEmpty(config.getTable(), true)==false>>return null;");
@@ -68,25 +78,30 @@ public JSONObject select(QueryConfig config) {
6878
final String sql = config.getSQL();
6979
final int position = config.getPosition();
7080

81+
if (cacheMap == null) {
82+
cacheMap = new HashMap<String, List<JSONObject>>();
83+
}
7184
JSONObject object = getFromCache(sql, position);
7285
if (object != null) {
7386
return object;
7487
}
7588

7689
try{
77-
Connection conn = getConnection();
78-
Statement stmt = conn.createStatement(); //创建Statement对象
79-
System.out.println(TAG + "成功连接到数据库!");
80-
81-
List<String> list = getColumnList(config.getTable(), conn.getMetaData());
90+
if (connection == null || connection.isClosed()) {
91+
System.out.println(TAG + "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
92+
connection = getConnection();
93+
statement = connection.createStatement(); //创建Statement对象
94+
metaData = connection.getMetaData();
95+
}
96+
List<String> list = getColumnList(config.getTable(), metaData);
8297
if (list == null || list.isEmpty()) {
8398
return null;
8499
}
85100

86101

87102
System.out.println(TAG + "select sql = " + sql);
88103

89-
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
104+
ResultSet rs = statement.executeQuery(sql);//创建数据对象
90105

91106
List<JSONObject> resultList = new ArrayList<JSONObject>();
92107
while (rs.next()){
@@ -104,8 +119,6 @@ public JSONObject select(QueryConfig config) {
104119
resultList.add(object);
105120
}
106121
rs.close();
107-
stmt.close();
108-
conn.close();
109122

110123
//从缓存存取,避免 too many connections崩溃
111124
if (position < config.getLimit() - 1) {

0 commit comments

Comments
 (0)