1

I am using MYSQL to manage book library data, here is my main table

CREATE TABLE `book` (
`id` int(11) NOT NULL,
`bookId` int(11) DEFAULT NULL,
`pageNum` smallint(6) DEFAULT NULL,
`pageData` longtext
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

Now when I run this query

SELECT c.id, c.bookId,c.pageNum, c.pageData, o.BookName FROM book c  left 
join kotarbooks o on c.bookId=o.BookId WHERE pageData like '%[Search Word]%'"

It takes about 3 minutes

and when I run query

SELECT * FROM book WHERE bookid=[bookid] AND pageNum=[pageNumber]

it takes about 2 minutes Is there any idea to speed up these queries? Thanks a lot

JustMe
  • 6,065
  • 3
  • 19
  • 17

2 Answers2

0

I run query SELECT * FROM book WHERE bookid=[bookid] AND pageNum=[pageNumber] it takes about 2 minutes

You could create index to speed up filtering:

CREATE INDEX idx ON book(bookid, pageNum);

As for WHERE pageData like '%[Search Word]%' is not so trivial because you are searching using '%' at the beginning and it makes a query not SARGable.

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
0

The LIKE expression:

WHERE book.pageData LIKE '%[Search Word]%'

is not sargable, and therefore any index on the book table likely would not be used by MySQL. So, we can instead approach a strategy of a full table scan on the book table along with an index on the kotarbooks table:

CREATE INDEX kotaridx ON kotarabooks (BookId, BookName);

The inclusion of the BookId column should help the join, and BookName covers this column, since it appears the select clause.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360