How can the downloading of OBD data be improved?



  • There's a D.O.D. with "books," "books_authors," "authors." "books_authors" is used to show a lot of relationships. We need to get a list of the books the authors will be inside. That's how I do it:

    public ArrayList<Book> getBooks() {
        Cursor booksCursor = mDB.rawQuery(TOP_SALES_BOOKS_QUERY, null);
    
    ArrayList&lt;Book&gt; books = new ArrayList&lt;&gt;();
    if(booksCursor.moveToFirst()) {
        int idIndex = booksCursor.getColumnIndex(Book.ID_TAG);
        int nameIndex = booksCursor.getColumnIndex(Book.NAME_TAG);
        //...
    
        do {
            int bookId = booksCursor.getInt(idIndex);
            String bookName = booksCursor.getString(nameIndex);
            //...
    
            Cursor authorsBooksCursor = mDB.rawQuery(AUTHORS_BY_BOOK_ID_QUERY,
                    new String[] {String.valueOf(bookId)});
            ArrayList&lt;Author&gt; authors = new ArrayList&lt;&gt;();
            if(authorsBooksCursor.moveToFirst()) {
                int authorNameIndex = authorsBooksCursor.getColumnIndex(Author.NAME_TAG);
    
                do {
                    String authorName = authorsBooksCursor.getString(authorNameIndex);
                    authors.add(new Author(authorName));
    
                } while (authorsBooksCursor.moveToNext());
            }
    
            authorsBooksCursor.close();
    
            books.add(new Book(bookId, bookName, authors, ...));
        } while (booksCursor.moveToNext());
    }
    
    booksCursor.close();
    
    return books;
    

    }

    The method is performed ~40 seconds. If you don't get the authorsBooksCursor, it's 1 second. How can the method or request be improved to speed up its implementation?

    Here are the requests:

    String TOP_SALES_BOOKS_QUERY = "SELECT * FROM " + Book.TABLE_NAME;

    String AUTHORS_BY_BOOK_ID_QUERY = "SELECT BAT." + DEFAULT_ID_TAG + ", AT."
    + Author.NAME_TAG + " FROM " + BOOKS_AUTHORS_TABLE_NAME
    + " AS BAT INNER JOIN " + Author.TABLE_NAME + " AS AT ON BAT." + Author.ID_TAG +
    " = AT." + Author.ID_TAG + " WHERE BAT." + Book.ID_TAG + " = ?";



  • private Map<Integer, Book> booksMap = new HashMap<>();
    
    ...
    
    //Запрос
    String query = "SELECT ba.book_id, b.name, a.name   
    FROM books_authors ba 
    INNER JOIN authors a ON ba.author_id = a.id
    INNER JOIN books b ON ba.book_id = b.id"
    
    ...
    
    //Заполнение
    
    if(cursor.moveToFirst()) {
    
        do {
            int id = cursor.getInt(0);
            Book book = booksMap.get(id);
            if(book == null) {
                book = new Book();
                book.setName(cursor.getString(1));
                booksMap.put(id, book);
            }
    
            String authorName = cursor.getString(2);
            book.addAuthor(authorName);
    
        } while (authorsBooksCursor.moveToNext());
    }
    

    He was on his knee. The idea was expressed, but it's not the fact that it'll be compromised. The request was spectacular, put your names in it.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2