我有2個實體的數據庫:商店,產品。每個產品都有他的描述和商店ID的一些列,你可以把它作爲FK。商店也有一些描述的專欄。從網站上PostgreSQL數據庫讀取的有效方式
我想TreeTable列表的商店。當你在每家店鋪展開樹時,你會得到這家店的產品清單。
正如我所說的使用Vaadin TreeTable,JDBC驅動程序來連接PostgreSQL數據庫。
選擇商店(約900行)極其快捷。少於1秒。然而,開放聲明,選擇每家商店的所有產品,並關閉它是殺死我的數據庫。
頁面正在加載7-11秒,即使我將在一個商店得到1個產品。所以即時猜測開幕和閉幕聲明是一個主要問題。
編輯:常見的情況是,在一個商店只有1-2個產品。不再。
我已經發布了我在5個步驟中所做的,但是如果你需要rly代碼,我會發布一些部分 這是常見的JDBC使用然而即時通訊問如果我需要得到很多行或網站使用許多用戶。使用JDBC使其更快的最佳實踐是什麼?
我的代碼:
開放連接:
//in db helper class
Connection conn = null;
void openConnection(){
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/shops";
static final String USER = "username";
static final String PASS = "password";
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
List<Shop> getAllShops(){
Statement stmt = null;
try{
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
//save rs into Shop object
}
rs.close();
}catch(SQLException se){
se.printStackTrace();
}finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}
}
//method to get operations is exacly the same but im passing Shop int id into method
void closeConnection(){
if(connection != null)
conn.close();
}
//in some layout class
void createAndFillTableMethod(){
//create table
DatabaseHelper db = new DatabaseHelper();
List<Shop> ls = new ArrayList<Shop>();
ls = getAllShops();
for(Shop shop: ls){
List<Product> lp = new ArrayList<Product>();
lp = getAllProducts(shop.id);
//add shop to table
//add lp list as a childs of shop to table
}
db.closeConnection();
}
PS:我會跳過一些嘗試抓住。
好的,你嘗試了什麼? – Houari 2014-11-03 19:00:29
每間店有多少種產品?商店裏1000行1秒的聲音聽起來並不快。一般來說,您的頻道可能已經存在與數據庫或您的數據庫有關的問題。那麼這一切都取決於數據,這是一個非常廣泛的問題... – cfrick 2014-11-03 19:34:40
@houari代碼發佈 – ilovkatie 2014-11-03 21:18:08