查询执行时间问题

时间:2016-06-01 09:03:01

标签: php mysql phpmyadmin

我对脚本的执行时间有些麻烦。查询需要花费大量时间。这是查询:

select avg(price) from voiture where duration<30 AND make="Audi" AND model="A4"

+---+---+---+---+---+---+---+---+---+---+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+---+---+---+---+---+---+---+---+---+---+
| 1 | SIMPLE | voiture | ALL | NULL | NULL | NULL | NULL | 1376949 | Using where |
+---+---+---+---+---+---+---+---+---+---+

select price from voiture where duration<30 AND make="Audi" AND model="A4"
+---+---+---+---+---+---+---+---+---+---+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+---+---+---+---+---+---+---+---+---+---+
| 1 | SIMPLE | voiture | ALL | NULL | NULL | NULL | NULL | 1376949 | Using where |
+---+---+---+---+---+---+---+---+---+---+

此查询大约需要2秒钟才能在phpMyAdmin界面上执行。我试图看看问题是什么,并删除avg函数使查询持续约0.0080秒 我问自己在php脚本中计算平均值需要多长时间,但是有或没有平均值的查询大约需要2秒钟。
所以我决定采用我的表的所有值并在脚本中进行处理,所以我使用这个查询:

select * from voiture where duration<30 AND make="Audi" AND model="A4"

+---+---+---+---+---+---+---+---+---+---+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+---+---+---+---+---+---+---+---+---+---+
| 1 | SIMPLE | voiture | ALL | NULL | NULL | NULL | NULL | 1376949 | Using where |
+---+---+---+---+---+---+---+---+---+---+

在phpMyAdmin界面上,需要0.0112秒。但在我的PHP脚本中,需要25秒!

$timestart2=microtime(true);
$querysec='select * from voiture where duration<30 AND make="Audi" AND model="A4"';
$requestsec=mysql_query($querysec) or die(mysql_error());
$timeend2=microtime(true);
$time2=$timeend2-$timestart2;
$page_load_time2 = number_format($time2, 9);

这是表结构:

CREATE TABLE `voiture` (
  `carid` bigint(9) NOT NULL,
  `serviceid` bigint(9) NOT NULL,
  `service` varchar(256) NOT NULL,
  `model` varchar(256) DEFAULT NULL,
  `gearingType` varchar(256) DEFAULT NULL,
  `displacement` int(5) DEFAULT NULL,
  `cylinders` int(2) DEFAULT NULL,
  `fuel` varchar(32) DEFAULT NULL,
  `mileage` int(7) DEFAULT NULL,
  `existFlag` tinyint(1) DEFAULT NULL,
  `lastUpdate` date DEFAULT NULL,
  `version` varchar(256) DEFAULT NULL,
  `bodyType` varchar(256) DEFAULT NULL,
  `firstRegistration` date DEFAULT NULL,
  `powerHp` int(4) DEFAULT NULL,
  `powerKw` int(4) DEFAULT NULL,
  `vat` varchar(256) DEFAULT NULL,
  `price` decimal(12,2) DEFAULT NULL,
  `duration` int(3) DEFAULT NULL,
  `pageUrl` varchar(256) DEFAULT NULL,
  `carImg` varchar(256) DEFAULT NULL,
  `color` varchar(256) DEFAULT NULL,
  `doors` int(1) DEFAULT NULL,
  `seats` int(1) DEFAULT NULL,
  `prevOwner` int(1) DEFAULT NULL,
  `co2` varchar(256) DEFAULT NULL,
  `consumption` varchar(256) DEFAULT NULL,
  `gears` int(1) DEFAULT NULL,
  `equipment` varchar(1024) NOT NULL,
  `make` varchar(256) NOT NULL,
  `country` varchar(3) NOT NULL
)

有关于carid和serviceid的索引 为什么我的查询执行时间太长?有没有办法可以改进?
为什么执行时间与phpMyAdmin和我的php脚本不同?

3 个答案:

答案 0 :(得分:1)

  

在phpMyAdmin界面上,需要0.0112秒。但在我的PHP   脚本,需要25秒!

phpMyAdmin接口为每个查询添加LIMIT。默认情况下,它是LIMIT 30

要减少聚合查询的时间,您需要为您使用的每个条件创建索引(或者可能是一个复合索引)。

因此,请尝试为modelmakeduration字段创建索引。

此外,您的表格太正规化了。您可以创建一对表来对其进行标准化 例如:Vendors(id, name)Models(id, name)并修改您的voiture以包含vendor_id / model_id个字段,而不是文字make / model

然后您的初始查询将如下所示:

select avg(t.price) from voiture t
INNER JOIN Models m ON m.id = t.model_id
INNER JOIN Vendors v ON v.id = t.vendor_id
where t.duration<30 AND v.name="Audi" AND m.name="A4"

它将扫描光查找表以查找文本,并使用带有索引ID的重表进行操作。

答案 1 :(得分:0)

有许多可能的解决方案,首先您可以在数据库级别执行is create index,这也可以缩短您的执行时间。

这个检查服务器,可能会有一些进程占用你的服务器资源,并使你的服务器变慢。

答案 2 :(得分:0)

你可以使用GROUP BY来加快速度,如

select AVG(price) from voiture where duration<30 AND make="Audi" AND model="A4" GROUP BY make

您还可以为make列添加索引

ALTER TABLE `voiture` ADD INDEX `make` (`make`)
相关问题