地址是http://topic.csdn.net/u/20091124/16/e45dde93-eae6-4a8a-9015-0c44419bfdaa.html你给出的语句mysql> select id,
    ->  (select col1 from t_fengchujun where id=(select max(id) from t_fengchujun where id<=a.id and col1 is not null)) as col1,
    ->  col2
    -> from t_fengchujun a;
功能是能实现的,不过速度太慢,表里面有50万条数据,,执行了12个小时,,都没有弄完。索引是有的所以,想请教一下你,还有不有其它的写法?

解决方案 »

  1.   


    无论是用 join 还是这种子查询都不会快。先看一下你的索引。可以建立两个索引以提高一下效率1. (id)
    2. (col1)
      

  2.   

    存储过程,或者你一次提交两个语句如下set @col1=null;
    select id,@col1:=ifnull(col1,@col1) as col1,col2 from t_fengchujun;mysql> select * from t_fengchujun;
    +------+------+-------+
    | id   | col1 | col2  |
    +------+------+-------+
    |    1 | a    | NULL  |
    |    2 | NULL | fsf   |
    |    3 | NULL | fe    |
    |    4 | NULL | ggvbc |
    |    5 | NULL | fsdf  |
    |    6 | NULL | fd    |
    |    7 | NULL | fsdf  |
    |    8 | b    | NULL  |
    |    9 | NULL | kjf   |
    |   10 | NULL | jty   |
    |   11 | NULL | jty   |
    |   12 | NULL | ty    |
    |   13 | c    | NULL  |
    |   14 | NULL | kyutj |
    |   15 | NULL | jty   |
    |   16 | d    | NULL  |
    |   17 | NULL | hfsh  |
    |   18 | NULL | bere  |
    +------+------+-------+
    18 rows in set (0.00 sec)mysql> set @col1=null;
    Query OK, 0 rows affected (0.00 sec)mysql> select id,@col1:=ifnull(col1,@col1) as col1,col2
        -> from t_fengchujun;
    +------+------+-------+
    | id   | col1 | col2  |
    +------+------+-------+
    |    1 | a    | NULL  |
    |    2 | a    | fsf   |
    |    3 | a    | fe    |
    |    4 | a    | ggvbc |
    |    5 | a    | fsdf  |
    |    6 | a    | fd    |
    |    7 | a    | fsdf  |
    |    8 | b    | NULL  |
    |    9 | b    | kjf   |
    |   10 | b    | jty   |
    |   11 | b    | jty   |
    |   12 | b    | ty    |
    |   13 | c    | NULL  |
    |   14 | c    | kyutj |
    |   15 | c    | jty   |
    |   16 | d    | NULL  |
    |   17 | d    | hfsh  |
    |   18 | d    | bere  |
    +------+------+-------+
    18 rows in set (0.00 sec)mysql>
      

  3.   

    (563387 row(s) affected)
    Execution Time : 00:00:01:250
    Transfer Time  : 00:00:01:109
    Total Time     : 00:00:02:359
    一秒就搞定了。
    你太牛了。。
      

  4.   

    存储过程与sql语句是否 , 一般的sql语句 都能用   存储过程 来实现呢?
    有没有  存储过程  学习的资料,,,比如在 csdn的贴子什么的。想去学习一下。。能介绍一下吗?
      

  5.   

    先读三遍《数据库系统概论》 (掌握基础知识和概念) 然后再粗略浏览一遍MYSQL的官方手册。(方便以后查找,避免类似于考试的时候,给你本政治书也不知道答案在第几章,第几页)MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html