这是问题的第二部分:MySql: make this query faster... is there a way?
此查询仍然运行缓慢:
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
有什么建议吗?
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 AUTO_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`),
)
这里:
+----+-------------+-------+--------+----------------------------------------+----------+---------+----------------------------+------+-------------+
| 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 |
+----+-------------+-------+--------+----------------------------------------+----------+---------+----------------------------+------+-------------+
答案 0 :(得分:1)
看起来你需要研究如何使用“解释选择”。
它会让您的所有查询更快! :)
答案 1 :(得分:0)
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
创建以下索引:
books (name)
bookcorr (category_id, library_id, books_id)
<强>更新强>
如果您需要总记录数,最好在两个查询中进行。
假设books_id
和library_id
始终引用books
和library
中的记录,请使用此查询:
SELECT COUNT(*)
FROM bookcorr a
WHERE a.category_id = '2521'
AND a.library_id = '4983'
计算数量。
更新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
答案 2 :(得分:0)
在bookcorr表中添加books_id索引。如果查询仍然很慢,请尝试使用书籍的数字ID(整数)。
答案 3 :(得分:0)
的MySQL&GT;解释SELECT b.id,b.name,c.name FROM bookcorr as a JOIN book 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'顺序由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 | |
+----+-------------+-------+--------+----------------------------------------+----------+---------+--------------------+------+----------------------------------------------+
答案 4 :(得分:0)
这是一个错字还是为什么在原始查询中SQL就像那样(?):
JOIN books as b on b.id = a.books_id = JOIN Library as c on c.id = a.library_id