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条数据。

解决方案 »

  1.   

    恩 mysql里执行的时候时间差不多
    具体运行的时候暂时没有这个环境,第二条语句是因为页面需要好几秒的时间,所以就想到按照时间来筛选再查询不知道会不会快点,也就是第一种方法。DESC 语句。也看不出什么名堂(请原谅我的菜)
      

  2.   

    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) 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;
    具体的执行计划看一下不就知道了。
      

  3.   

    恩,explain和desc 效果一样。感觉一样第二条
    *************************** 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)
      

  4.   

    ************************** 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 ********
    这个好
      

  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)
    此效率高。
      

  6.   

    你好,这个和效率关系不大吧?rows是影响的行数。如果查询出是0那不是效率飞快?
      

  7.   

    看mysql文档说效率从高到低依次为:
    system > const > eq_ref > ref > range > index > All第一条是使用 type:range,第二条是使用 type:index
    效率是哪条高?