2016-05-29 7 views
0

Ich mache in sqlite db von JSON-Datei einfügen. Json enthält ~ 20000 Produkte. Jedes Produktfeld enthält ~ 10 Kategorien (hinzugefügt in separate Tabelle, CategoryProduct_MM). Dies ergibt ungefähr 200000 Datensätze. Dieser Vorgang dauerte etwa 30 Sekunden. Ist es möglich, es schneller zu machen?Sqlite Batch INSERT ist langsam

@Override 
    protected String doInBackground(Void... voids) { 

     if (db.isOpen()) { 

      db.beginTransaction(); 

      String sql_prod = "Insert or Replace into Products (Amount, SourcesIDs, ImageID, PrdID," + 
        " Barcode, UnitsID, Brand, PrdName, PrdImgURL, SellType, CategoriesIDs)" + 
        " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
      String sql_cat = "Insert or Replace into Categories (ImageUpdateDate, CategoryID, ParentCatID," + 
        " ThisItemsOrder, CategoryName) values(?, ?, ?, ?, ?)"; 
      String sql_cat_prod = "Insert or Replace into CategoryProduct_MM (PrdID, CategoryID)" + 
        " values(?, ?)"; 
      String sql_fts = "Insert or Replace into Products_fts (rowid, PrdID, ConcatSearchField)" + 
        " values(?, ?, ?)"; 

      insert_prod = db.compileStatement(sql_prod); 
      insert_cat = db.compileStatement(sql_cat); 
      insert_cat_prod = db.compileStatement(sql_cat_prod); 
      insert_fts = db.compileStatement(sql_fts); 

      try { 

       for (final Product product : products) { 

        insertField(1, product.getAmount(), insert_prod); 
        insertField(2, product.getSourcesIDs(), insert_prod); 
        insertField(3, product.getImageID(), insert_prod); 
        insertField(4, product.getPrdID(), insert_prod); 
        insertField(5, product.getBarcode(), insert_prod); 
        insertField(6, product.getUnitsID(), insert_prod); 
        insertField(7, product.getBrand(), insert_prod); 
        insertField(8, product.getPrdName(), insert_prod); 
        insertField(9, product.getPrdImgURL(), insert_prod); 
        insertField(10, product.getSellType(), insert_prod); 
        insertField(11, product.getCategoriesIDs(), insert_prod); 
        insert_prod.execute(); 

        String[] id = product.getCategoriesIDs().split(","); 
        for (int i = 0; i < id.length; i++) { 
          insertField(1, product.getPrdID(), insert_cat_prod); 
          insertField(2, id[i], insert_cat_prod); 
          insert_cat_prod.execute(); 
        } 

        if (product.getConcatSearchField() != null) { 
         insertField(1, product.getPrdID(), insert_fts); 
         insertField(2, product.getPrdID(), insert_fts); 
         insertField(3, product.getConcatSearchField(), insert_fts); 
         insert_fts.execute(); 
        } 
       } 

       for (Category category : categories) { 

        insertField(1, category.getImageUpdateDate(), insert_cat); 
        insertField(2, category.getCategoryID(), insert_cat); 
        insertField(3, category.getImageUpdateDate(), insert_cat); 
        insertField(4, category.getParentCatID(), insert_cat); 
        insertField(5, category.getCategoryName(), insert_cat); 
        insert_cat.execute(); 
       } 

       db.setTransactionSuccessful(); 
       listener.onUpdateFinished(); 

      } finally { 
       db.endTransaction(); 
       db.close(); 
      } 
     } 

     return null; 
    } 

    private void insertField(int index, String field, SQLiteStatement insert) { 
     if (field == null) { 
      insert.bindNull(index); 
     } else { 
      insert.bindString(index, field); 
     } 
    } 

Product.class

package me.superup.updatedb.model; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

import javax.annotation.Generated; 

@Generated("org.jsonschema2pojo") 
public class Product { 

    @SerializedName("Amount") 
    @Expose 
    private String amount; 
    @SerializedName("SourcesIDs") 
    @Expose 
    private String sourcesIDs; 
    @SerializedName("ImageID") 
    @Expose 
    private String imageID; 
    @SerializedName("Action") 
    @Expose 
    private String action; 
    @SerializedName("PrdID") 
    @Expose 
    private String prdID; 
    @SerializedName("Barcode") 
    @Expose 
    private String barcode; 
    @SerializedName("UnitsID") 
    @Expose 
    private String unitsID; 
    @SerializedName("Brand") 
    @Expose 
    private String brand; 
    @SerializedName("PrdName") 
    @Expose 
    private String prdName; 
    @SerializedName("PrdImgURL") 
    @Expose 
    private String prdImgURL; 
    @SerializedName("SellType") 
    @Expose 
    private String sellType; 
    @SerializedName("CategoriesIDs") 
    @Expose 
    private String categoriesIDs; 
    @SerializedName("ConcatSearchField") 
    @Expose 
    private String concatSearchField; 

    /** 
    * 
    * @return 
    *  The amount 
    */ 
    public String getAmount() { 
     return amount; 
    } 

    /** 
    * 
    * @param amount 
    *  The Amount 
    */ 
    public void setAmount(String amount) { 
     this.amount = amount; 
    } 

    /** 
    * 
    * @return 
    *  The sourcesIDs 
    */ 
    public String getSourcesIDs() { 
     return sourcesIDs; 
    } 

    /** 
    * 
    * @param sourcesIDs 
    *  The SourcesIDs 
    */ 
    public void setSourcesIDs(String sourcesIDs) { 
     this.sourcesIDs = sourcesIDs; 
    } 

    /** 
    * 
    * @return 
    *  The imageID 
    */ 
    public String getImageID() { 
     return imageID; 
    } 

    /** 
    * 
    * @param imageID 
    *  The ImageID 
    */ 
    public void setImageID(String imageID) { 
     this.imageID = imageID; 
    } 

    /** 
    * 
    * @return 
    *  The action 
    */ 
    public String getAction() { 
     return action; 
    } 

    /** 
    * 
    * @param action 
    *  The Action 
    */ 
    public void setAction(String action) { 
     this.action = action; 
    } 

    /** 
    * 
    * @return 
    *  The prdID 
    */ 
    public String getPrdID() { 
     return prdID; 
    } 

    /** 
    * 
    * @param prdID 
    *  The PrdID 
    */ 
    public void setPrdID(String prdID) { 
     this.prdID = prdID; 
    } 

    /** 
    * 
    * @return 
    *  The barcode 
    */ 
    public String getBarcode() { 
     return barcode; 
    } 

    /** 
    * 
    * @param barcode 
    *  The Barcode 
    */ 
    public void setBarcode(String barcode) { 
     this.barcode = barcode; 
    } 

    /** 
    * 
    * @return 
    *  The unitsID 
    */ 
    public String getUnitsID() { 
     return unitsID; 
    } 

    /** 
    * 
    * @param unitsID 
    *  The UnitsID 
    */ 
    public void setUnitsID(String unitsID) { 
     this.unitsID = unitsID; 
    } 

    /** 
    * 
    * @return 
    *  The brand 
    */ 
    public String getBrand() { 
     return brand; 
    } 

    /** 
    * 
    * @param brand 
    *  The Brand 
    */ 
    public void setBrand(String brand) { 
     this.brand = brand; 
    } 

    /** 
    * 
    * @return 
    *  The prdName 
    */ 
    public String getPrdName() { 
     return prdName; 
    } 

    /** 
    * 
    * @param prdName 
    *  The PrdName 
    */ 
    public void setPrdName(String prdName) { 
     this.prdName = prdName; 
    } 

    /** 
    * 
    * @return 
    *  The prdImgURL 
    */ 
    public String getPrdImgURL() { 
     return prdImgURL; 
    } 

    /** 
    * 
    * @param prdImgURL 
    *  The PrdImgURL 
    */ 
    public void setPrdImgURL(String prdImgURL) { 
     this.prdImgURL = prdImgURL; 
    } 

    /** 
    * 
    * @return 
    *  The sellType 
    */ 
    public String getSellType() { 
     return sellType; 
    } 

    /** 
    * 
    * @param sellType 
    *  The SellType 
    */ 
    public void setSellType(String sellType) { 
     this.sellType = sellType; 
    } 

    /** 
    * 
    * @return 
    *  The categoriesIDs 
    */ 
    public String getCategoriesIDs() { 
     return categoriesIDs; 
    } 

    /** 
    * 
    * @param categoriesIDs 
    *  The CategoriesIDs 
    */ 
    public void setCategoriesIDs(String categoriesIDs) { 
     this.categoriesIDs = categoriesIDs; 
    } 

    /** 
    * 
    * @return 
    *  The concatSearchField 
    */ 
    public String getConcatSearchField() { 
     return concatSearchField; 
    } 

    /** 
    * 
    * @param concatSearchField 
    *  The ConcatSearchField 
    */ 
    public void setConcatSearchField(String concatSearchField) { 
     this.concatSearchField = concatSearchField; 
    } 

} 

Category.class

public class Category { 

    @SerializedName("ImageUpdateDate") 
    @Expose 
    private String imageUpdateDate; 
    @SerializedName("CategoryID") 
    @Expose 
    private String categoryID; 
    @SerializedName("ParentCatID") 
    @Expose 
    private String parentCatID; 
    @SerializedName("ThisItemsOrder") 
    @Expose 
    private String thisItemsOrder; 
    @SerializedName("CategoryName") 
    @Expose 
    private String categoryName; 

    /** 
    * 
    * @return 
    *  The imageUpdateDate 
    */ 
    public String getImageUpdateDate() { 
     if (imageUpdateDate.equals("")) 
      return String.valueOf(0); 
     return imageUpdateDate; 
    } 

    /** 
    * 
    * @param imageUpdateDate 
    *  The ImageUpdateDate 
    */ 
    public void setImageUpdateDate(String imageUpdateDate) { 
     this.imageUpdateDate = imageUpdateDate; 
    } 

    /** 
    * 
    * @return 
    *  The categoryID 
    */ 
    public String getCategoryID() { 
     return categoryID; 
    } 

    /** 
    * 
    * @param categoryID 
    *  The CategoryID 
    */ 
    public void setCategoryID(String categoryID) { 
     this.categoryID = categoryID; 
    } 

    /** 
    * 
    * @return 
    *  The parentCatID 
    */ 
    public String getParentCatID() { 
     return parentCatID; 
    } 

    /** 
    * 
    * @param parentCatID 
    *  The ParentCatID 
    */ 
    public void setParentCatID(String parentCatID) { 
     this.parentCatID = parentCatID; 
    } 

    /** 
    * 
    * @return 
    *  The thisItemsOrder 
    */ 
    public String getThisItemsOrder() { 
     return thisItemsOrder; 
    } 

    /** 
    * 
    * @param thisItemsOrder 
    *  The ThisItemsOrder 
    */ 
    public void setThisItemsOrder(String thisItemsOrder) { 
     this.thisItemsOrder = thisItemsOrder; 
    } 

    /** 
    * 
    * @return 
    *  The categoryName 
    */ 
    public String getCategoryName() { 
     return categoryName; 
    } 

    /** 
    * 
    * @param categoryName 
    *  The CategoryName 
    */ 
    public void setCategoryName(String categoryName) { 
     this.categoryName = categoryName; 
    } 

} 
+0

können Sie auch das gemeinsam nutzen Produktklasse und Kategorieklasse? –

+0

@Markus sicher, ich habe sie und andere Aussagen hinzugefügt. –

+1

http://stackoverflow.com/questions/1711631/improve-insert-per-second-performance-of-sqlite – pvg

Antwort

0

Es gibt nicht viel können Sie diesen Code zu beschleunigen; Die Verarbeitung von 200.000 Zeilen auf einem mobilen Gerät ist immer langsam. (Die Flash-I/O-Vorgänge für diese Datenmenge benötigen viel Zeit.)

Wenn möglich, senden Sie die vollständige Datenbank mit Ihrer App.

Wenn Sie die Daten aus anderen Ländern bekommen müssen, versuchen Sie die JSON-Datei mit einer Datenbank-Datei zu ersetzen, die ATTACH ed auf Ihre Haupt-Datenbank sein kann und nur kopiert:

INSERT INTO main.MyTable SELECT * FROM downloadDB.MyTable; 
+0

Ok, es scheint die akzeptabelste Lösung zu sein. Aber meine Update-DB-Datei enthält einige Daten, die bereits in der Hauptdatenbank existieren. Zum Beispiel enthält die Haupt-DB-Tabelle zwei Felder mit Werten: 1-a, 1-b, 1-c und die Aktualisierungstabelle enthält 1-a, 1-b, 1-d. Die resultierende Tabelle muss 1-a, 1-b, 1-c, 1-d enthalten. Wie kann ich es tun? –

+0

Das wäre eine andere Frage. –

+0

Bitte beantworten Sie, wenn Sie können http://stackoverflow.com/questions/37551680/delete-same-values-from-another-db-table –

Verwandte Themen