加快内循环查询?

时间:2015-08-28 11:00:11

标签: php mysql loops

我正在获取xml提要,解析并存储到数据库。

有时会有一些欧洲饲料团队,例如哪个国家是欧洲,而不是英格兰,德国,塞尔维亚等。只是欧洲,所以我创建了包含所有国家/地区列的所有国家和地图表的数组。

所以我想查看table map里面的country = $ countryfromarray的团队,当我和一个团队一起尝试时,这可以工作,但是我从feed获得团队,在feed中有大约5000个团队。

if((strtolower($country) == 'southamerica') or (strtolower($country) == 'conmebol')){
        $countries = $this->conmebolarray;
        $isregion = true;
    }
    elseif((strtolower($country) == 'europe') or (strtolower($country) == 'uefa')){
        $countries = $this->euroarray;
        $isregion = true;
    }
    elseif((strtolower($country) == 'asia') or (strtolower($country) == 'afc')){
        $countries = $this->afcarray;
        $isregion = true;
    }
    elseif((strtolower($country) == 'africa') or (strtolower($country) == 'caf')){
        $countries = $this->cafarray;
        $isregion = true;
    }
    elseif((strtolower($country) == 'northandcentralamerica') or (strtolower($country) == 'concacaf')){
        $countries = $this->conarray;
        $isregion = true;
    }
    else{
        $countries = $country;
        $isregion = false;
    }
    //$res = '';
    if($isregion){
      $query =  $PDO->prepare($sql);
      $newcountry = '';
      foreach($countries as $loopcountry){
          $query->bindValue(':data', $data);
          $query->bindValue(':country', $loopcountry);
          //
          if($query->execute())
          {
            if($query->rowCount()>0){
              $res = $query->fetchColumn();
              $newcountry = $loopcountry;   
              break; 
            }
            $query->closeCursor(); 
          }
      }

和一系列国家:

    //"southamerica" & "conmebol" & "CONMEBOL";
public $conmebolarray = array('argentina','brazil','chile','colombia','paraguay','uruguay','venezuela','peru','bolivia','ecuador','newzealand');

//"europe" & "uefa" & "Europe" & "UEFA";
public $euroarray = array('england','france','germany','italy','spain','scotland','albania','andorra','armenia','austria','azerbaijan','belarus','belgium','bosnia','bulgaria','croatia','cyprus','czech','denmark','estonia','faroeislands','georgia','gibraltar','greece','hungary','iceland','israel','kazakhstan','latvia','liechtenstein','luxembourg','malta','moldova','montenegro','netherlands','northernireland','norway','poland','portugal','ireland','romania','russia','sanmarino','serbia','slovakia','slovenia','sweden','switzerland','turkey','ukraine','wales','europe');

//"asia" & "afc" & "Asia" & "AFC";
public $afcarray = array('australia','japan','korea','singapore','china','qatar','saudiarabia','vietnam','bahrain',
'bangladesh','bhutan','brunei','cambodia','chinesetaipei','guam','hongkong','india','indonesia','iran','iraq','japan','korea',
'northkorea','kuwait','kyrgyzstan','laos','lebanon','macau','malaysia','maldives','mongolia','myanmar','nepal',
'northernmarianaislands','oman','pakistan','palestine','philippines','srilanka','syria','tajikistan','thailand','timorleste','turkmenistan','uae','uzbekistan','yemen','newzealand','asia','afc');

//"africa" & "caf" & "Africa" & "CAF";
public $cafarray = array('nigeria','southafrica','egypt','tunisia','algeria','morocco','cameroon','drcongo','ivorycoast','ghana','djibouti','eritrea','ethiopia','kenya','rwanda','somalia','southsudan','sudan','tanzania','uganda','zanzibar','angola',
'botswana','comoros','lesotho','madagascar','malawi','mauritius','mozambique','namibia','seychelles','swaziland','zambia',
'zimbabwe','reunion','burundi','saotomeandprincipe','gabon','equatorialguinea','congo','chad','centralafricanrepublic','togo',
'sierraleone','senegal','niger','mauritania','mali','liberia','guineabissau','guinea','gambia','capeverde','burkinafaso',
'benin','libya','africa','caf');

//"northandcentralamerica" & "concacaf" & "CONCACAF";
public $conarray = array('usa','mexico','canada','costarica','elsalvador','guatemala','honduras','trinidadandtobago','jamaica',
'panama','honduras','haiti','cuba','belize','dominicanrepublic','bermuda','aruba','barbados','grenada','surinam',
'guadeloupe', 'antigua','antiguaandbarbuda','saintvincent','saintvincentandthegrenadines','saintkittsandnevis',
'saintlucia','nicaragua','curacao','puertorico','guyana','dominica','usvirginislands','montserrat','caymanislands',
'turksandcaicos','turksandcaicosislands','britishvirginislands','bahamas','anguilla','frenchguiana','martinique',
'saintmartin','sintmarteen','bonaire','concacaf','northandcentralamerica');

和GetMap程序

CREATE PROCEDURE GetMap(p_team VARCHAR(100), p_country VARCHAR(100))
BEGIN
SELECT Base FROM map WHERE 
(map.Base = p_team OR
Ver1 = p_team OR
Ver2 = p_team OR
Ver3 = p_team OR
Ver4 = p_team OR
Ver5 = p_team OR
Ver6 = p_team OR
Ver7 = p_team OR
Ver8 = p_team OR
Ver9 = p_team OR
Ver10 = p_team OR
Ver11 = p_team OR
Ver12 = p_team) AND Country = p_country;
end //
DELIMITER ;

1 个答案:

答案 0 :(得分:1)

不完全确定您的查询有什么问题。考虑到map表有适当的索引;你可以修改你的查询,如下所示。看起来像覆盖索引会加快这个查询。

SELECT Base 
FROM map 
WHERE 
p_team IN (Ver1, Ver2, Ver3, Ver4, Ver5, Ver6, Ver7, Ver8, Ver9, Ver10, Ver11, Ver12, Base)
AND Country = p_country;