Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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报表查询_Php_Mysql - Fatal编程技术网

Php 优化MySQL报表查询

Php 优化MySQL报表查询,php,mysql,Php,Mysql,我有一些查询需要30分钟才能执行,我不是数据库专家,所以我真的不知道该做什么,所以在这里,我需要有人建议更好的查询: select count(*),substring(tdate,1,7) from bills where amt='30' group by substring(tdate,1,7) order by substring(tdate,1,7) desc SELECT count(*) FROM `bills` where amt='30' and date(td

我有一些查询需要30分钟才能执行,我不是数据库专家,所以我真的不知道该做什么,所以在这里,我需要有人建议更好的查询:

select count(*),substring(tdate,1,7) 
from bills 
where amt='30' 
group by substring(tdate,1,7) 
order by substring(tdate,1,7) desc

SELECT count(*) 
FROM `bills` 
where amt='30' 
and date(tdate)=date('$date') 
and stat='RENEW' 
and x1 in (select `id` from sub); 
在这里,我以以下格式传递$date的值'Y-m-d 00:00:00'

select count(*),substring(tdate,1,7) 
from bills 
where amt='30' 
group by substring(tdate,1,7) 
order by substring(tdate,1,7) desc
表结构:

MariaDB [talksport]> desc bills;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| bid   | int(11)      | NO   | PRI | NULL    | auto_increment |
| num   | varchar(500) | NO   |     | NULL    |                |
| stat  | varchar(500) | NO   |     | NULL    |                |
| tdate | varchar(500) | NO   |     | NULL    |                |
| x1    | varchar(500) | NO   |     | NULL    |                |
| amt   | varchar(500) | NO   |     | 30      |                |
+-------+--------------+------+-----+---------+----------------+
欢迎任何和所有的帮助


Michael

您的三个查询实际上是两个(第一个和第三个是相同的)。以下是您的三个查询(已重新格式化以便可读):

首先,对于第一次查询,您需要票据(amt,tdate)上的索引。第二个问题更大。在MySQL的某些版本中,中的
可能是一个问题。此外,日期算法也有问题。因此,如果您将
tdate
存储为YYYY-MM-DD,则以相同的格式传入
$date
(最好使用参数,最好使用正确的类型)。因此,我将这样写:

select count(*)
from `bills` b
where amt = '30' and tdate = '$date' and stat = 'RENEW' and
      exists (select 1 from sub s where b.x1 = s.id);
然后你需要一个关于
票据的索引(amt,stat,tdate,id)


正确的索引应该可以加快查询速度。

除了上面的答案之外,还可以进行另一个优化,将
COUNT(*)
替换为
COUNT(id)

当您对所有行进行计数时,并且每行都有一个唯一的标识符(
id
是已编制索引的
主键),如果您只对id进行计数,那么您最好得到相同的
COUNT
。查询必须查找较少的列,并且它将筛选的列已经被索引,以使搜索和聚合更快

SELECT
查询中,尝试使用特定的列名而不是
*
,总是很好的。同样,务必查看
SELECT
中使用的列以及参与
WHERE
groupby
子句的列,以确定潜在的索引候选项


请注意:

创建多个索引不应被认为是优化的唯一方法,因为它可能会减慢批量
INSERT
s/
UPDATE
s,同时尝试显著加快
SELECT
。此外,您可能最终创建的索引可能是多余的或冗余的。因此,必须考虑应用程序用途的整体视图,以实现最佳平衡-这取决于更多的用户操作是集中在
INSERT
/
UPDATE
还是
SELECT

为什么要将名为
tdate
的内容存储在字符串中?类似地,对于名为
amt
?即交易日期,记录插入表格的日期和时间插入票据(num,stat,tdate,x1,amt)值('1234','RENEW',now(),'080777','10mt'参见关于数据类型
select count(*)
from `bills` b
where amt = '30' and tdate = '$date' and stat = 'RENEW' and
      exists (select 1 from sub s where b.x1 = s.id);