我有一个名为广告的表格,其中包含~441.000行和21列。
我正在尝试运行以下查询:
SELECT Ads.* FROM Ads WHERE Ads.countries_CountryId = 'FR'
我将一个索引放入countries_CountryId字段,该字段的类型为char(2)
,但是当我运行上述查询时,大约需要5-8秒才能完成。对于我来说,对于这样一个中等大小的表,以及如此简单的SQL查询,这段时间看起来非常高。我应该在哪里寻找问题?或者这样的查询花了这么长时间才正常?
我也试过EXPLAIN
上面的查询,并得到以下结果,但我不知道如何解读这个:(
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE Ads ref countries_CountryId countries_CountryId 2 const 24368 Using where
EDIT1:(对Seth McClaine的回应)
我尝试过您的建议,按照您的建议,返回错误,但如果我运行SELECT count(*) FROM Ads WHERE Ads.countries_CountryId = 'FR'
,则运行速度要快得多:0.0052490234375
但问题是,我没有打印任何东西,我使用php来运行查询,并计算他们的运行时,最后输出查询运行,以及他们采取的时间:
Array
(
[0] => Array
(
[query] => SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'
[duration] => 0.00481009483337
)
[1] => Array
(
[query] => SELECT * FROM Countries WHERE NameFormatted LIKE '%FRANCE%'
[duration] => 0.00234889984131
)
[2] => Array
(
[query] => SELECT Ads.* FROM Ads WHERE Ads.countries_CountryId = 'FR'
[duration] => 4.71820402145
)
[3] => Array
(
[query] => SELECT COUNT(*) FROM Ads WHERE Ads.countries_CountryId = 'FR'
[duration] => 0.0052490234375
)
)
这里还有运行查询的代码片段:
public function query($query, $cacheit=true) {
if (!$this->isConnected()) {
$this->throwError("Querying <b>".$query."</b> failed, because MySQL is not connected!", self::MYSQL_NOT_CONNECTED, false);
return false;
}
$qstart=microtime(true);
$result=@mysql_query($query, $this->conn_resource);
$qduration=microtime(true) - $qstart;
if ($result===FALSE) $this->throwError("Querying <b>".$query."</b> failed!", self::MYSQL_QUERY_ERR);
$this->numQueries++;
$this->executedQueries[] = array(
'query' => $query,
'duration' => $qduration
);
return $result;
}