dongsong73032 2013-09-21 19:31
浏览 7
已采纳

如何加快或优化数据库查询?

I'm somewhat new to mySQL and PHP. I have a little program that looks up a text's words in an etymological dictionary. -- Source here on github. It can only look up 1-3 words per second, which is a real limitation, especially when I'm trying to analyze a text that's bigger than a thousand words. Is there a way I can better structure my queries or database so that I can speed up this process?

Function that looks up a word:

function lookup($word) { 
    //connect to database
    $query="SELECT parent_lang FROM etym_dict WHERE word=\"$word\" and word_lang=\"eng\""; //making this English-only for now
    //debug_print("<p>Query is: $query</p>"); 
    $result=dbquery($query) 
    or die("Failed to look up words in database."); 
    $parent_lang=mysqli_fetch_array($result); 
    $parent_lang=$parent_lang[0]; 
    return $parent_lang; 
} 

Thing that calls that function:

foreach (array_keys($results) as $word) { 
    $parent_lang=lookup($word); 
    if (!empty($parent_lang)) {  
        $parent_langs[]=array($word,$parent_lang,$results[$word]); 
        debug_print("$word, "); 
    } else { 
        $derivation=lookup_derivation($word); 
        $has_derivation= (strlen($derivation)>0) ? TRUE : FALSE; 
        if ($has_derivation) { 
            $parent_lang=lookup($derivation); 
        } 
        if(!empty($parent_lang) && $has_derivation) { 
            debug_print("<span class=\"blue\">$word ($derivation)</span>, "); 
        } else if(!empty($parent_lang)) { 
            $parent_langs[]=array($word,$parent_lang,$results[$word]); 
            debug_print("<span class=\"blue\">$word</span>, "); 
        } else { 
            $not_in_dict[]=$word; 
            debug_print("<span class=\"red\">$word"); 
            if ($has_derivation) { 
                debug_print("/$derivation</span>, "); 
            } else { 
                debug_print("</span>, "); 
            } 
        } 
    } 
} 

Db query function:

 function dbquery($sql) {
                GLOBAL $dbc;
                $result=mysqli_query($dbc,$sql);
                return $result;
        }

Db connect function:

function dbconnect() {
            $dbc=mysqli_connect(
                  ... // redacted 
            ) or die ('Error connecting to database.');
            return $dbc;
    }

Sample etym_dict dataDefinitions

  • 写回答

1条回答 默认 最新

  • duanmuybrtg1231 2013-09-21 20:33
    关注

    My first guess is that you don't have indexes on etym_dict, but that's only a guess because you haven't shown us the table definitions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)