简体   繁体   English

mysql查询运行缓慢

[英]mysql query running slow

Is there a way to speed up this query. 有没有办法加快这个查询。 Its loading slow and its about 279 records. 它的加载速度慢,约有279条记录。 I've indexed the Language & id field in the data. 我已将数据中的Language&id字段编入索引。 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 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. 就像书中一样,所有章节的页码都在索引中给出,因为读者按章节搜索,类似于Mysql更快地处理你的查询,你需要索引将在where条件中匹配的字段。 Index the fields which others have suggested here. 索引其他人在此建议的字段。

You need to index also grouping and p_order . 您还需要索引groupingp_order You can drop index on language if not used by any where condition. 如果没有任何where条件使用,您可以删除language索引。

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 如果您想了解详细信息,请查看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. 请注意,添加两个单独的索引(一个用于grouping ,一个用于p_order )将无法完成这一操作,因为您在查询中同时使用这两个字段。

Try a compossed index on groupping and p_order. 在灌浆和p_order上尝试一个合成索引。 Read more about group by optimization 通过优化了解更多关于组的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM