doudaochu1699 2014-10-10 10:05
浏览 211
已采纳

MySQL Union查询需要花费太多时间来执行

I'm trying to get a single MySQL query result from different queries working on different table and databases, this is my table structure with database names:

database ads1- tables: clicks_214,requests

database ads2- tables: clicks_255,requests

database ads3- tables: clicks_256,requests

and this is the query which I'm trying to execute

$query="";
for($i=1;$i<4;++$i)
{
    $this->db_num=$i;
    $this->selectDB($i);
    $table=$this->getTableName();
    $db=$this->getDatabaseName();
    $query.="(SELECT r.kwd, count(c.rid) as cnum, count(r.kwd) as rnum
            FROM $db.requests r LEFT JOIN $db.$table c ON r.id=c.rid
            WHERE hid='$hid' 
            AND r.time BETWEEN '$date1 $hour1:00:00' AND '$date2 $hour2:59:59'
            GROUP BY r.kwd
            ORDER BY cnum DESC
            LIMIT $limit,50)";
    if($i<3)
    {
        $query.=" UNION ";
    }
}

I'm certainly sure that this isn't the best way to do it, just because I have to wait about 5 minutes to get results. Is there any way to do this much faster? I've already set indexes on all of the 3 DBs

this is an EXPLAIN result: enter image description here

  • 写回答

1条回答 默认 最新

  • drdr123456 2014-10-10 13:48
    关注

    a) You SHOULD NOT make such crossdatabase queries. With your DB structure it would be more appropriate to run 3 independent queries and combine the result using some simple script Or to make a temporary table in one of the databases where you can combine data from every one of those

    b) On large databases put some indexes to improve the speed. It is not the solution though...

    You can try to play with Temp tables:

    $this->selectDB('temptableDB');
    $maketemp = "
    CREATE TEMPORARY TABLE temp_table_1 (
      `kwd` int ,
      `rid` int,
    )
    "; 
    
    mysql_query($maketemp, $connection) or die ("Sql error : ".mysql_error());
    
    for($i=1;$i<4;++$i)
    {
       $this->db_num=$i;
       $this->selectDB($i);
       $table=$this->getTableName();
       $db=$this->getDatabaseName();
    
       $inserttemp = "
        INSERT INTO temptableDB.temp_table_1
         (`kwd`, `rid`)
            SELECT r.kwd, c.rid
            FROM $db.requests r LEFT JOIN $db.$table c ON r.id=c.rid
            WHERE hid='$hid' 
            AND r.time BETWEEN '$date1 $hour1:00:00' AND '$date2 $hour2:59:59'
      ";
    
      mysql_query($inserttemp, $connection) or die ("Sql error : ".mysql_error());
    
     }
    
      $select = "
        SELECT kwd, count(rid) as cnum, count(kwd) as rnum
        FROM temp_table_1
        GROUP BY r.kwd
        ORDER BY cnum DESC
      ";
      $export = mysql_query($select, $connection) or die ("Sql error : ".mysql_error());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败