【发布时间】:2010-12-18 19:41:21
【问题描述】:
我正在建立一个电子图书馆,我编写了一个查询,它可能由于索引不佳而扫描更多行。这是我的表结构。
-
book_categories(cat_id, parent_id DEFAULT NULL, cat_name, parent_cat_name DEFAULT NULL) -
authors(author_id, author_name, active) -
books(book_id, cat_id, author_id, book_title, book_contents, book_status ENUM (published,pending,deleted) publish_date)
这里是 SQL 代码
SELECT T.author_id,
T.author_name,
C.parent_id,
C.cat_id,
C.cat_name,
B.book_id,
B.book_title,
B.book_contents,
B.publish_date
FROM books B,
authors T,
book_categories C
WHERE B.author_id = T.author_id
AND C.cat_id = B.cat_id
AND book_status = 'published'
ORDER BY published_date DESC
上述查询的解释
id select_type table type possible_keys key key_len ref rows Extra
-----------------------------------------------------------------------------------------------------
1 SIMPLE C ALL PRIMARY NULL NULL NULL 449 Using where; Using temporary; Using filesort
1 SIMPLE B ref author_id,cat_id cat_id 4 bookdb.C.cat_id 214 Using where
1 SIMPLE T eq_ref PRIMARY PRIMARY 4 bookdb.B.author_id 1
索引和键
Alter table books
add Foreign Key (author_id) references authors (author_id) on delete restrict on update restrict;
Alter table books
add Foreign Key (cat_id) references book_categories (cat_id) on delete restrict on update restrict;
Create Index books_INX ON books(cat_id,book_status,published_date);
【问题讨论】:
-
@Juha Syrjälä 我添加了它们,但有些缺少它们。 author_id是authors表的主键,cat_id是book_categories的主键,book_id是books表的主键
-
你说这个查询扫描了太多行?但是查询速度够快吗?每个表有多少行?
-
我在这个查询中使用了 LIMIT 10,它扫描了 650 多行。超过 110K 已出版的书籍和 50K 尚未出版。如果我从此查询中删除 order by,则扫描的行数减少到 250。