开发者

MySql: make this query faster… is there a way ? PART TWO

This is part two of the question: MySql: make this query faster... is there a way?

this query still run slowly:

SELECT b.id,
       b.name,
       c.name
FROM bookcorr as a JOIN books as b on b.id = a.books_id =
JOIN Library as c on c.id = a.library_id 
WHERE a.category_id = '2521' 
AND a.library_id = '4983' 
ORDER BY b.name ASC LIMIT 0,15

Any suggest ?

CREATE TABLE `bookcorr` (
  `category_id` smallint(4) unsigned NOT NULL,
  `book_id` mediumint(7) unsigned NOT NULL,
  `library_id` smallint(5) unsigned NOT NULL,
  UNIQUE KEY `cat_id_3` (`category_id`,`book_id`,`library_id`),
  KEY `category_id` (`category_id`),
  KEY `book_id` (`book_id`),
  KEY `library_id` (`library_id`),
  KEY `cat_id_2` (`cat_id`,`com_id`))

CREATE TABLE `books` (
  `id` mediumint(7) unsigned NOT NULL AUTO_INCREMENT,
  `com_id` smallint(5) unsigned NOT NULL,
  `name` varchar(256) NOT NULL,
    ....
  PRIMARY KEY (`id`),
  KEY `name` (`name`),
  FULLTEXT KEY `search` (`name`,`author`)
)

 CREATE TABLE `Library` (
  `id` smallint(4) unsigned NOT NULL A开发者_开发技巧UTO_INCREMENT,
  `name` varchar(256) NOT NULL,
  ...
  UNIQUE KEY `id` (`id`),
  KEY `library_name` (`name`)
) 

CREATE TABLE `Category` (
  `id` smallint(4) unsigned NOT NULL,
  `name` varchar(100) NOT NULL,
  UNIQUE KEY `id` (`id`),
  KEY `name` (`name`),
)

Here:

+----+-------------+-------+--------+----------------------------------------+----------+---------+----------------------------+------+-------------+
| id | select_type | table | type   | possible_keys                          | key      | key_len | ref                        | rows | Extra       |
+----+-------------+-------+--------+----------------------------------------+----------+---------+----------------------------+------+-------------+
|  1 | SIMPLE      | b     | index  | NULL                                   | name     | 258     | NULL                       |   15 |             |
|  1 | SIMPLE      | a     | eq_ref | cat_id_3,cat_id,book_id,library_id     | cat_id_3 | 7       | const,b.id,const           |    1 | Using index |
|  1 | SIMPLE      | c     | const  | PRIMARY                                | PRIMARY  | 2       | const                      |    1 | Using where |
+----+-------------+-------+--------+----------------------------------------+----------+---------+----------------------------+------+-------------+


Looks like you need to study how to use "explain select".

It will make all your queries faster! :)


Add a books_id index in bookcorr table.If the query stills slow, try to use numerical ids for the books(Integer).


SELECT  b.id,
        b.name,
        c.name
FROM    books as b
JOIN    bookcorr as a
ON      a.books_id = b.id
        AND a.category_id = '2521' 
        AND a.library_id = '4983' 
JOIN    Library as c
ON      c.id = a.library_id 
ORDER BY
        b.name ASC
LIMIT 15

Create the following indexes:

books (name)
bookcorr (category_id, library_id, books_id)

Update:

If you need the total record count, it would be better doing it in two queries.

Assuming that books_id and library_id always reference a record in books and library, use this query:

SELECT  COUNT(*)
FROM    bookcorr a
WHERE   a.category_id = '2521'
        AND a.library_id = '4983' 

to calculate the count.

Update 2:

SELECT  b.id,
        b.name,
        c.name
FROM    books b USE INDEX (name)
STRAIGHT_JOIN
        bookcorr a
ON      a.books_id = b.id
        AND a.category_id = '2521' 
        AND a.library_id = '4983' 
STRAIGHT_JOIN
        Library as c
ON      c.id = a.library_id 
ORDER BY
        b.name ASC
LIMIT 15


Is it a typo or why in the original query the SQL like that (?):

JOIN books as b on b.id = a.books_id = JOIN Library as c on c.id = a.library_id


mysql> explain SELECT b.id,b.name,c.name FROM bookcorr as a JOIN books as b on a.book_id = b.id JOIN library as c on b.library_id=c.id WHERE a.category_id = '2302' AND a.library_id = '4983' order by b.name asc LIMIT 0,15;

+----+-------------+-------+--------+----------------------------------------+----------+---------+--------------------+------+----------------------------------------------+
| id | select_type | table | type   | possible_keys                          | key      | key_len | ref                | rows | Extra                                        |
+----+-------------+-------+--------+----------------------------------------+----------+---------+--------------------+------+----------------------------------------------+
|  1 | SIMPLE      | a     | ref    | cat_id_3,cat_id,book_id,library_id     | cat_id_3 | 4       | const,const        | 1788 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | b     | eq_ref | PRIMARY,library_id                     | PRIMARY  | 3       | dbname.a.book_id   |    1 |                                              |
|  1 | SIMPLE      | c     | eq_ref | PRIMARY                                | PRIMARY  | 2       | dbname.b.library_id|    1 |                                              |
+----+-------------+-------+--------+----------------------------------------+----------+---------+--------------------+------+----------------------------------------------+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜