pre_threads 主题表(几千万的数据)
pre_forums 版块表
SELECT t.tid,t.fid,t.subject,t.dateline FROM pre_threads t,pre_forums f WHERE f.fid=t.fid AND f.status IN(1,2) AND t.dateline>=unix_timestamp()-3600*24*3 LIMIT 5;SELECT t.tid,t.fid,t.subject,t.dateline FROM pre_threads t,pre_forums f WHERE f.fid=t.fid AND f.status IN(1,2) ORDER BY t.dateline DESC LIMIT 5;
需求就是
第一条语句查询最近3天时间的5条数据;
第二条语句查询最新5条数据。
pre_forums 版块表
SELECT t.tid,t.fid,t.subject,t.dateline FROM pre_threads t,pre_forums f WHERE f.fid=t.fid AND f.status IN(1,2) AND t.dateline>=unix_timestamp()-3600*24*3 LIMIT 5;SELECT t.tid,t.fid,t.subject,t.dateline FROM pre_threads t,pre_forums f WHERE f.fid=t.fid AND f.status IN(1,2) ORDER BY t.dateline DESC LIMIT 5;
需求就是
第一条语句查询最近3天时间的5条数据;
第二条语句查询最新5条数据。
具体运行的时候暂时没有这个环境,第二条语句是因为页面需要好几秒的时间,所以就想到按照时间来筛选再查询不知道会不会快点,也就是第一种方法。DESC 语句。也看不出什么名堂(请原谅我的菜)
FROM pre_threads t,pre_forums f
WHERE f.fid=t.fid AND f.status
IN(1,2) AND t.dateline>=unix_timestamp()-3600*24*3 LIMIT 5;explain SELECT t.tid,t.fid,t.subject,t.dateline FROM pre_threads t,pre_forums f WHERE f.fid=t.fid AND f.status IN(1,2) ORDER BY t.dateline DESC LIMIT 5;
具体的执行计划看一下不就知道了。
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
type: index
possible_keys: displayorder,typeid
key: pre_threads_index1
key_len: 4
ref: NULL
rows: 5
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: f
type: eq_ref
possible_keys: PRIMARY,forum
key: PRIMARY
key_len: 2
ref: tbl.t.fid
rows: 1
Extra: Using where
2 rows in set (0.00 sec)第一条
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
type: range
possible_keys: displayorder,typeid,pre_threads_index1
key: pre_threads_index1
key_len: 4
ref: NULL
rows: 3530
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: f
type: eq_ref
possible_keys: PRIMARY,forum
key: PRIMARY
key_len: 2
ref: tbl.t.fid
rows: 1
Extra: Using where
2 rows in set (0.00 sec)
id: 1
select_type: SIMPLE
table: t
type: index
possible_keys: displayorder,typeid
key: pre_threads_index1
key_len: 4
ref: NULL
rows: 5
Extra:
*************************** 2. row ********
这个好
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
type: index
possible_keys: displayorder,typeid
key: pre_threads_index1
key_len: 4
ref: NULL
rows: 5
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: f
type: eq_ref
possible_keys: PRIMARY,forum
key: PRIMARY
key_len: 2
ref: tbl.t.fid
rows: 1
Extra: Using where
2 rows in set (0.00 sec)
此效率高。
system > const > eq_ref > ref > range > index > All第一条是使用 type:range,第二条是使用 type:index
效率是哪条高?