LIMIT OFFSET是分页的最佳解决方案吗?

时间:2018-12-08 08:26:28

标签: java sqlite

说明:

我正在使用SQLite创建发票系统。 数据库的表包括:客户和发票。 每个invoiceOBJ(14列数据)包含一个customerOBJ(4列数据), 而每个invoiceOBJ将包含一个customerID以检索相应的客户。

我在发票表中随机生成了100万条记录, 根据我的理解和research (outDated?) 的100万条记录,在sqlite(性能方面)方面略有推动

我将发票加载到javaFX表格视图中的查询是

ResultSet rs = stmt.executeQuery(String.format("SELECT id,year,vin,carModel,condition,licenseState,regNum,stage,vehicleID,paintCode,dateOfInvoice," +
                "bodyStyle,manufacturer, customerID  FROM invoices LIMIT 100  OFFSET (SELECT COUNT(*) FROM invoices)-%d", indexStart));

indexStart是要通过gui在发票上翻页的数字-100或+100。

我想知道的是这是否是使用sqlite的最佳解决方案 每次用户决定转到下一页时,如果有100万张发票/记录,则平均需要250-264毫秒。然后,每次制作invoiceOBJ时要获得customerOBJ的每个查询平均为0.6-1ms * 100(限制),每次迭代通常总计约为300ms。

实际上,一百万张发票要比销毁发票所需的价格高出98%,但是,出于教育目的,我试图使其尽可能高效。

请澄清一下,我不是在问这个sqlite是否是实现此目的的最佳方法,而是问这是否在sqlite中实现此目的的最佳方法。

sqlitedb对象,相关方法:

 public void connect() {
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:school.sqlite");


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public ArrayList<invoiceOBJ> listInvoices(int indexStart) {
        //invoiceAmount  = invoices to index.
        ArrayList<invoiceOBJ> invoices = new ArrayList<>();
        //System.out.println(indexStart + "index start");
        try {
            connect();
            stmt = c.createStatement();
            stmt.setFetchSize(100);

            ResultSet rs = stmt.executeQuery(String.format("SELECT id,year,vin,carModel,condition,licenseState,regNum,stage,vehicleID,paintCode,dateOfInvoice," +
                    "bodyStyle,manufacturer, customerID  FROM invoices LIMIT 100  OFFSET (SELECT COUNT(*) FROM invoices)-%d", indexStart));

            int id;
            String customerID;
            String year;
            String vin;
            String carModel;
            String condition;
            String licenseState;
            String regNum;
            String stage;
            String vehicleID;
            String paintCode;
            String dateOfInvoice;
            String bodyStyle;
            String manufacturer;
            c.setAutoCommit(false);

            while (rs.next()) {


                id = rs.getInt(1);
                year = rs.getString(2);
                vin = rs.getString(3);
                carModel = rs.getString(4);
                condition = rs.getString(5);
                licenseState = rs.getString(6);
                regNum = rs.getString(7);
                stage = rs.getString(8);
                vehicleID = rs.getString(9);
                paintCode = rs.getString(10);
                dateOfInvoice = rs.getString(11);
                bodyStyle = rs.getString(12);
                manufacturer = rs.getString(13);
                customerID = rs.getString(14);


//                System.out.println(String.format("ID: %d , CustomerID: %s Year: %s, Vin: %s, \ncarModel %s, condition: %s, licenseState: %s \n" +
//                                "regNum: %s, stage: %s vehicleID: %s, paintCode: %s, dateOfInvoice: %s, bodyStyle:%s", id, customerID, year, vin, carModel
//                        , condition, licenseState, regNum, stage, vehicleID, paintCode, dateOfInvoice, bodyStyle));


                //add to invoice list.

                invoices.add(new invoiceOBJ(id,
                        carModel, manufacturer,
                        vin, condition, licenseState,
                        regNum, stage, vehicleID, paintCode,
                        bodyStyle, year, dateOfInvoice, findCustomer(customerID)));


                // System.out.println("added A customer");


            }
            stmt.close();
            rs.close();
            c.close();
            //System.out.println("load complete..");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // System.out.println(System.currentTimeMillis() - time);

        return invoices;

    }

    public HashMap<String, String> findCustomer(String id) {

        String sql = String.format("SELECT firstName, lastName,id, date FROM customers WHERE id=%s", id);
        HashMap<String, String> customerData = new HashMap<>();
        try {
            Statement stmt = c.createStatement();
            ResultSet data = stmt.executeQuery(sql);
            customerData.put("firstName", data.getString(1));
            customerData.put("lastName", data.getString(2));
            customerData.put("date", data.getString(4));
            customerData.put("id", data.getString(3));
            stmt.close();
            data.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return customerData;
    }

    public void disconnect() {
        try {
            c.close();
            stmt.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

P.S这是我的第一次尝试,也是我第一次使用SQL,因此,感谢您提供任何其他解释!谢谢!

编辑: 我也已经阅读了一些资源:

SQlite improve query efficiency

SQLite storage class And retrieval efficiency

Faster Pagination in Mysql – Why Order By With Limit and Offset is Slow?

不知道这些是否好/相关,只是为了获得更多细节。

图片中的功能:

https://imgur.com/a/pczeCSO:之前

https://imgur.com/a/kZT3l9C:更多/下一张发票。

0 个答案:

没有答案