开发者

mysql query running slow

开发者 https://www.devze.com 2023-01-28 20:29 出处:网络
Is there a way to speed up this query. Its loading slow and its about 279 records. I\'ve indexed the Language & id field in the data. Is there anything else I can do?

Is there a way to speed up this query. Its loading slow and its about 279 records. I've indexed the Language & id field in the data. Is there anything else I can do?

echo "<select name=language style='width: 136px;'>";
echo "<option value=$language selected=selected>-- Select --</option>";
$group1 = '<optgroup label=Common>';
$group2 = '<optgroup label=Alphabetically>';
$group = mysql_query("SELECT language, grouping, p_order FROM lang_list 
            WHERE grouping IN ('1','2') ORDER BY p_order");
while($row = mysql_fetch_array($group))
{
  if ($row['grouping'] == '1')
  {
       $group1 .= '<option value="'.$row['language'].'">'.
            $row['language'].'</option>';
  }
  else
  {
       $group2 .= '<option value="'.$row['language'].'">'.
            $row['language'].'</option>';
  }
    $group1 .= '</otpgroup>';
    $group2 .= '</otpgroup>';

    echo $group1;
    echo $group2;
    echo "</select>";
} 

Table

lang_list (
  id int(8) NOT NULL auto_increment,
  language varchar(75) NOT开发者_StackOverflow NULL,
  grouping varchar(15) NOT NULL,
  p_order varchar(15) NOT NULL,
  PRIMARY KEY  (id, language)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=280 ;


You need to index what you are matching against.

Just like in a book, page number of all chapters are given in the index because readers search by chapter, similarly for Mysql to process your queries faster, you need to index the fields which will be matched in the where condition. Index the fields which others have suggested here.


You need to index also grouping and p_order. You can drop index on language if not used by any where condition.

Please post the table schema for further investigation, and some sample data.


create index I_grouping_order on lang_list (grouping, p_order);

If you want to know the details have a look at http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

Note that adding two separate indexes (one for grouping and one for p_order) will not completelly do the trick because you are using both fields in your query at the same time.


Try a compossed index on groupping and p_order. Read more about group by optimization

0

精彩评论

暂无评论...
验证码 换一张
取 消