1

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 NULL,
  grouping varchar(15) NOT NULL,
  p_order varchar(15) NOT NULL,
  PRIMARY KEY  (id, language)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=280 ;
3
  • Can you post the query execution plan for the above query? Commented Dec 6, 2010 at 7:35
  • post the output of the EXPLAIN of the query, just add EXPLAIN to the beginning Commented Dec 6, 2010 at 7:55
  • How much time is the query taking? There are very few records and no joins at all. It should get fixed with the proper indexes. Commented Dec 6, 2010 at 8:00

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

i've added it to my original post
Ok cool, now add the indexes I mentioned, and see how performs
1
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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.