Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果我们两次调用Mysql查询,首先调用Get count,然后调用listing,则会花费更多的时间_Php_Mysql - Fatal编程技术网

Php 如果我们两次调用Mysql查询,首先调用Get count,然后调用listing,则会花费更多的时间

Php 如果我们两次调用Mysql查询,首先调用Get count,然后调用listing,则会花费更多的时间,php,mysql,Php,Mysql,我面临mysql查询的问题,我需要先得到不同级别的记录数,然后显示它的列表,例如: select MAX(Orders) from priority where priority_type='Top_Priority'; select MAX(Orders) from priority where priority_type='Priority'; select MAX(Orders) from priority where priority_type='Regular'; 最高优先级(12

我面临mysql查询的问题,我需要先得到不同级别的记录数,然后显示它的列表,例如:

select MAX(Orders) from priority where priority_type='Top_Priority';
select MAX(Orders) from priority where priority_type='Priority';
select MAX(Orders) from priority where priority_type='Regular';
  • 最高优先级(120个订单)
  • 优先权(100个订单)
  • 常规(50份订单)
在此之后,我得到了相同的记录,如:

select * from priority where priority_type='Top_Priority';
select * from priority where priority_type='Priority';
select * from priority where priority_type='Regular';

执行起来要花很多时间。是否有其他方法可以提高执行性能。

您可以使用每种类型的最大值加入记录

SELECT t1.*, t2.OrderMax
FROM priority AS t1
JOIN (SELECT priority_type, MAX(Orders) AS OrderMax
      FROM priority
      GROUP BY priority_type) AS t2 ON t1.priority_ttpe = t2.priority_type

为了加速此查询,最好在表中的
(优先级类型,订单)
上有一个多列索引。

是否在
订单
优先级类型
上有索引?
选择最大值(列)
将返回值,而不是记录数。这两个查询不会返回相同的记录。第一个查询只为每个优先级类型返回一条记录,其中包含列的最大值。第二个查询返回所有记录。顺便说一句,您可以在单个查询中执行第一个查询<代码>按优先级类型从优先级组中选择优先级类型,最大(订单数)您唯一要做的是
索引
订单
优先级类型
列,它将加快您的执行时间,如果您想获得总记录,则使用
计数
而不是
MAX
max
将返回该特定类型的最大订单数。