提升查询MySQL性能
This commit is contained in:
parent
b767d6c4a3
commit
3678fa69c3
@ -43,6 +43,9 @@ public class ServerGet2 {
|
||||
* "Comment[]":[{...}, ...]
|
||||
*/
|
||||
|
||||
// SelectTable2.getInstance().close();
|
||||
SelectTable3.getInstance().close();
|
||||
|
||||
return requestObject;
|
||||
}
|
||||
|
||||
@ -366,7 +369,7 @@ public class ServerGet2 {
|
||||
*/
|
||||
private synchronized JSONObject getSQLObject(QueryConfig config) {
|
||||
System.out.println("getSQLObject config = " + JSON.toJSONString(config));
|
||||
return SelectTable2.select(config);//SelectTable3.getInstance().select(config);//
|
||||
return SelectTable3.getInstance().select(config);//SelectTable2.getInstance().select(config);//
|
||||
}
|
||||
|
||||
/**获取查询配置
|
||||
|
@ -1,10 +1,6 @@
|
||||
package zuo.biao.apijson.server.sql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -21,7 +17,22 @@ public class SelectTable2 {
|
||||
// System.out.println(TAG + JSON.toJSONString(select("stu")));
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws Exception {
|
||||
private static Connection connection;
|
||||
private static Statement statement;
|
||||
private static DatabaseMetaData metaData;
|
||||
private SelectTable2() {
|
||||
}
|
||||
|
||||
private static SelectTable2 instance;
|
||||
public static synchronized SelectTable2 getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new SelectTable2();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
public Connection getConnection() throws Exception {
|
||||
//调用Class.forName()方法加载驱动程序
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
System.out.println(TAG + "成功加载MySQL驱动!");
|
||||
@ -29,29 +40,44 @@ public class SelectTable2 {
|
||||
return DriverManager.getConnection(url, "root", "199531tommy");
|
||||
}
|
||||
|
||||
|
||||
public static JSONObject select(String table) {
|
||||
return select(new QueryConfig(table));
|
||||
public void close() {
|
||||
try {
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public static JSONObject select(QueryConfig config) {
|
||||
metaData = null;
|
||||
statement = null;
|
||||
}
|
||||
|
||||
public JSONObject select(QueryConfig config) {
|
||||
if (config == null || StringUtil.isNotEmpty(config.getTable(), true) == false) {
|
||||
System.out.println(TAG + "select config==null||StringUtil.isNotEmpty(config.getTable(), true)==false>>return null;");
|
||||
return null;
|
||||
}
|
||||
final String sql = config.getSQL();
|
||||
|
||||
try{
|
||||
Connection conn = getConnection();
|
||||
Statement stmt = conn.createStatement(); //创建Statement对象
|
||||
|
||||
|
||||
System.out.println(TAG + "成功连接到数据库!");
|
||||
|
||||
List<String> list = getColumnList(config.getTable(), conn.getMetaData());
|
||||
if (connection == null || connection.isClosed()) {
|
||||
System.out.println(TAG + "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
|
||||
connection = getConnection();
|
||||
statement = connection.createStatement(); //创建Statement对象
|
||||
metaData = connection.getMetaData();
|
||||
}
|
||||
|
||||
List<String> list = getColumnList(config.getTable());
|
||||
if (list == null || list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String sql = "select * from " + config.getTable() + config.getWhereString() + config.getLimitString(); //要执行的SQL
|
||||
System.out.println(TAG + "select sql = " + sql);
|
||||
|
||||
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
|
||||
ResultSet rs = statement.executeQuery(sql);//创建数据对象
|
||||
|
||||
JSONObject object = null;//new JSONObject();//null;
|
||||
int position = -1;
|
||||
@ -74,8 +100,6 @@ public class SelectTable2 {
|
||||
}
|
||||
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
|
||||
return object;
|
||||
} catch(Exception e) {
|
||||
@ -90,11 +114,11 @@ public class SelectTable2 {
|
||||
* @param table
|
||||
* @return
|
||||
*/
|
||||
public static List<String> getColumnList(String table, DatabaseMetaData meta) {
|
||||
public List<String> getColumnList(String table) {
|
||||
List<String> list = new ArrayList<String>();
|
||||
ResultSet rs;
|
||||
try {
|
||||
rs = meta.getColumns("sys", null, table, "%");
|
||||
rs = metaData.getColumns("sys", null, table, "%");
|
||||
while (rs.next()) {
|
||||
System.out.println(TAG + rs.getString(4));
|
||||
list.add(rs.getString(4));
|
||||
|
@ -1,10 +1,6 @@
|
||||
package zuo.biao.apijson.server.sql;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -20,7 +16,7 @@ public class SelectTable3 {
|
||||
|
||||
private Map<String, List<JSONObject>> cacheMap;
|
||||
private SelectTable3() {
|
||||
cacheMap = new HashMap<String, List<JSONObject>>();
|
||||
|
||||
}
|
||||
|
||||
private static SelectTable3 instance;
|
||||
@ -59,6 +55,20 @@ public class SelectTable3 {
|
||||
return list == null || position < 0 || position >= list.size() ? null : list.get(position);
|
||||
}
|
||||
|
||||
private static Connection connection;
|
||||
private static Statement statement;
|
||||
private static DatabaseMetaData metaData;
|
||||
public void close() {
|
||||
try {
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
metaData = null;
|
||||
statement = null;
|
||||
cacheMap = null;
|
||||
}
|
||||
|
||||
public JSONObject select(QueryConfig config) {
|
||||
if (config == null || StringUtil.isNotEmpty(config.getTable(), true) == false) {
|
||||
@ -68,17 +78,22 @@ public class SelectTable3 {
|
||||
final String sql = config.getSQL();
|
||||
final int position = config.getPosition();
|
||||
|
||||
if (cacheMap == null) {
|
||||
cacheMap = new HashMap<String, List<JSONObject>>();
|
||||
}
|
||||
JSONObject object = getFromCache(sql, position);
|
||||
if (object != null) {
|
||||
return object;
|
||||
}
|
||||
|
||||
try{
|
||||
Connection conn = getConnection();
|
||||
Statement stmt = conn.createStatement(); //创建Statement对象
|
||||
System.out.println(TAG + "成功连接到数据库!");
|
||||
|
||||
List<String> list = getColumnList(config.getTable(), conn.getMetaData());
|
||||
if (connection == null || connection.isClosed()) {
|
||||
System.out.println(TAG + "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ;
|
||||
connection = getConnection();
|
||||
statement = connection.createStatement(); //创建Statement对象
|
||||
metaData = connection.getMetaData();
|
||||
}
|
||||
List<String> list = getColumnList(config.getTable(), metaData);
|
||||
if (list == null || list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
@ -86,7 +101,7 @@ public class SelectTable3 {
|
||||
|
||||
System.out.println(TAG + "select sql = " + sql);
|
||||
|
||||
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
|
||||
ResultSet rs = statement.executeQuery(sql);//创建数据对象
|
||||
|
||||
List<JSONObject> resultList = new ArrayList<JSONObject>();
|
||||
while (rs.next()){
|
||||
@ -104,8 +119,6 @@ public class SelectTable3 {
|
||||
resultList.add(object);
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
|
||||
//从缓存存取,避免 too many connections崩溃
|
||||
if (position < config.getLimit() - 1) {
|
||||
|
Loading…
Reference in New Issue
Block a user