假设我有16个不同的条件,下面两种方案1, 执行16次查询,每次传入一个条件
for (i = 0; i < 16, i ++) {
执行sql查询根据每个传入的条件
}2. 写一个procedure, 把这16个条件连接起来一次传入procedure的参数,procedure根据这16个
条件查得所需结果,然后把16个结果用字……补充:
上面的16次说错了,总共是256次查询。
这256次查询里,所有的查询条件都一样,只是表的名字不同,即256个表。

解决方案 »

  1.   

    用存储过程一次查询比较好,所得结果可以用 union all 联合起来作为一个结果集传到调用过程.
      

  2.   

    谢谢大家的回复。
    〉〉〉用存储过程一次查询比较好,所得结果可以用 union all 联合起来作为一个结果集传到调用过程.
    比如说我的查询是 
    第1次 select ccc from A where 条件
    第2次 select ccc from A where 条件
    第3次 select ccc from A where 条件
    第4次 select ccc from A where 条件
    第5次 select ccc from A where 条件

    总计256个,所有的查询条件都一样,只是表名不同。
    上面所说的union all如何用? 是不是比较高效率的?
      

  3.   

    谢谢大家的回复。
    〉〉〉用存储过程一次查询比较好,所得结果可以用 union all 联合起来作为一个结果集传到调用过程.
    比如说我的查询是 
    第1次 select ccc from A where 条件
    第2次 select ccc from B where 条件
    第3次 select ccc from C where 条件
    第4次 select ccc from D where 条件

    总计256个,所有的查询条件都一样,只是表名不同。
    上面所说的union all如何用? 是不是比较高效率的?
      

  4.   

    如果表名都是已知的,条件都能与表名和列名对上号,那可以这样:
    create procedure getvaluefrom256table
    (@where1 nvarchar(20),@where2 nvarchar(20),@where3 nvarchar(20))
    as
    begin
    select * from tb1 where col1=@where1
    union all
    select * from tb2 where col1=@where2
    union all
    select * from tb3 where col1=@where3
    end

    否则的话,要用动态查询,但你 256 个表太多了, nvarchar 类型只能有 4000字,查一个表10多个字肯定不够.即使用 varchar 也不够.
      

  5.   

    create proc procname
    (@查询条件 varchar(100))
    as
    select ccc from A where 条件 union
    select ccc from B where 条件 union
    select ccc from C where 条件 union
    select ccc from D where 条件 union
    ......
      

  6.   

    下面是我写的procedure, 这个procedure被调用20次,
    并且每执行procedure一次,procedure里的cursor1有15次循环。
    总的来说,所需时间太长。
    请大家帮我优化优化。
    DELIMITER $$CREATE PROCEDURE `gete`(
    IN location VARCHAR(24),
    IN idlen INT(2),
    IN swhere VARCHAR(3000),
    OUT sSize VARCHAR(3000),
    OUT sTid VARCHAR(3000))
    BEGINdeclare done int;
    declare tid VARCHAR(24);
    declare sid VARCHAR(30);declare cursor1 CURSOR FOR SELECT did FROM didtable where lvl = idlen
     and ind = location;declare exit handler for not found set done = 0;set done = 1;
    set sZize = '';
    set sTid = '';open cursor1;
      while done do
        fetch cursor1 into tid;    set sid = concat('h', idlen, '.', tid);        set @stmt = concat(' SELECT size into @avgP FROM ', sid, swhere);
        prepare s1 from @stmt;
        execute s1;    #SELECT @avgP INTO `sSize`;
        set sSize= concat(sSize, ',', @avgP);
        set sTid = concat(sTid, ',', tid);
      end while;
    close cursor1;
      

  7.   

    还是关于这个问题。
    查询的次数太多,导致检索时间过长。有哪位高手能否介绍一下有关mysql的并行检索? 
    比如说,我有2000个查询,让两台机器同时执行,每台机器负责1000个查询。
    或者有什么别的建议?
      

  8.   

    用MYSql的Cluster能否做高效的并行检索?