# Query_time: 11.041339 Lock_time: 0.000074 Rows_sent: 1 Rows_examined: 4033514
use mytable;
SET timestamp=1441090564;
select c.fetching_method,cc.book_chapter_id,cc.book_episode_name,cc.book_episode_number,cc.book_id,
ccc.book_image_path,ccc.book_page_sequence,cccc.book_name,cccc.book_permalink
from book_source c, book_chapter cc,book_page ccc,book cccc
where c.book_source_id = cc.book_source_id AND cc.book_chapter_id = ccc.book_chapter_id AND cccc.book_id = cc.book_id AND
cccc.book_permalink='Dancing_Cow' AND cc.book_episode_number > '42' ORDER BY book_episode_number ASC LIMIT 1;
mysql> desc book;
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| book_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| description | text | YES | | NULL | |
| book_picture | text | YES | | NULL | |
| book_permalink | text | YES | | NULL | |
| total_view_count | int(16) | NO | | 0 | |
| thumbnail | int(2) | YES | | NULL | |
| book_rating | double | YES | | NULL | |
| fail_retry | int(2) | NO | | 0 | |
+--------------------+------------------+------+-----+---------+----------------+
18 rows in set (0.01 sec)
mysql> desc book_page;
+---------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+---------+----------------+
| book_page_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| book_chapter_id | int(10) | YES | | NULL | |
| book_image_path | text | YES | | NULL | |
| book_page_sequence | double | YES | | NULL | |
| date_time_added | text | YES | | NULL | |
| date_time_modified | text | YES | | NULL | |
| watermark | int(2) | YES | | NULL | |
| book_related | text | YES | | NULL | |
+---------------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> desc book_source;
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| book_source_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| book_id | int(10) | YES | | NULL | |
| book_source_url | varchar(150) | YES | UNI | NULL | |
| fetching_method | text | YES | | NULL | |
| source_book_name | text | YES | | NULL | |
| process_id | int(10) | YES | | NULL | |
| disable | int(1) | YES | | NULL | |
| source_group | int(2) | YES | | NULL | |
| fail_retry | int(2) | YES | | 0 | |
+--------------------+------------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
mysql> desc book_chapter;
+----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------------+------+-----+---------+----------------+
| book_chapter_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| book_view_count | int(10) | YES | | NULL | |
| book_episode_number | double | YES | | NULL | |
| book_episode_name | text | YES | | NULL | |
| book_source_id | int(10) | YES | | NULL | |
| book_id | int(20) | YES | | NULL | |
| book_complete | text | YES | | NULL | |
| total_pages | double | YES | | NULL | |
| chapter_view_count | int(16) | NO | | 0 | |
| book_fix | int(2) | YES | | NULL | |
| original_url | text | YES | | NULL | |
+----------------------+------------------+------+-----+---------+----------------+
13 rows in set (0.00 sec)
我有一个非常糟糕的连接语句,这会导致我的服务器负载过重。 有没有办法可以改进这个连接语句以使用较小的行。
我的这个表book_page获得了大约100万条记录。如何优化实际能够绘制数据而不使用那么多记录(4m),因为我想要的数据实际上非常少。只是那么少的价值
答案 0 :(得分:1)
您应该始终做的第一件事是使用scrollView
来查看查询运行状况以及优化程序如何处理查询。这将为您提供一些基本信息,例如,如果您在表上缺少索引,从而导致全表扫描等。
现在,我将使用显式连接语法而不是隐式编写查询,您当前正在使用它,查询看起来像
explain select..
现在你需要检查表是否有正确的索引,注意主键总是被索引,因此你不需要再次添加它们,但是从上面的查询中你可以在表中有以下索引
select
c.fetching_method,
cc.book_chapter_id,
cc.book_episode_name,
cc.book_episode_number,
cc.book_id,
ccc.book_image_path,
ccc.book_page_sequence,
cccc.book_name,
cccc.book_permalink
from book_source c
join book_chapter cc on cc.book_source_id = c.book_source_id
join book_page ccc on ccc.book_chapter_id = cc.book_chapter_id
join book cccc on cccc.book_id = cc.book_id
where
cccc.book_permalink='Dancing_Cow'
AND cc.book_episode_number > '42'
ORDER BY cc.book_episode_number ASC LIMIT 1;
请注意,如果alter table book_chapter add index src_eps_idx(book_source_id,book_episode_number);
alter table book_page add index book_chapter_id_idx(book_chapter_id);
alter table book add index id_permalink_idx(book_id,book_permalink);
已经在book_chapter_id
表上编入索引,则可以跳过此表,更重要的是 - 在应用索引之前备份表。
答案 1 :(得分:0)