简体   繁体   English

Mysql查询很慢(group by)

[英]Mysql query very slow (group by)

mysql query very slow but i use "group by" function... mysql 查询速度很慢,但我使用"group by"功能...

i remove group by query and query very fast.我按查询删除组,查询速度非常快。

How can I solve this problem?我怎么解决这个问题?

my query code:我的查询代码:

$myquery1 = mysql_query("SELECT * FROM konucuklar 
                         WHERE status='' 
                           and category='football' 
                         GROUP BY matchhour 
                         ORDER BY id asc");
while($myquery1record = mysql_fetch_array($myquery1)){

    $myquery2 = mysql_query("SELECT * FROM konucuklar 
                             WHERE mactarihi='$bugunt' 
                               and statu='' 
                               and kategori='futbol' 
                               and macsaati='$myquery1record[matchhour]' 
                             ORDER BY id asc");

     $toplams=@mysql_num_rows($myquery2);


     while ($myquery2record=mysql_fetch_array($myquery2)) {     
// code
     } 
}
}
  1. Your first query does not comply with SQL standards and will be processed by mysql only if strict sql mode is not enabled.您的第一个查询不符合 SQL 标准,只有在未启用严格 sql 模式时才会由 mysql 处理。
  2. You are issuing the 2nd query in a loop based on the results returned by the 1st query.您将根据第一个查询返回的结果在循环中发出第二个查询。 So, if the 1st query returns 10 rows, then you will execute the 2nd query 10 times.因此,如果第一个查询返回 10 行,那么您将执行第二个查询 10 次。 This is very slow.这是非常缓慢的。 You should rewrite the 2 queries as one, since both queries query the same table and have almost the same where criteria.您应该将 2 个查询重写为一个,因为这两个查询查询同一个表并且具有几乎相同的 where 条件。
  3. No idea what the 2nd while loop does, as I can't see where $listele is defined.不知道第二个 while 循环做了什么,因为我看不到 $listele 的定义位置。

The slow down might not be related to the GROUP BY clause.速度变慢可能与 GROUP BY 子句无关。 Try adding an index on columns you need to .尝试在您需要的列上添加索引。

Link to understand index : http://www.tutorialspoint.com/sql/sql-indexes.htm链接了解索引: http : //www.tutorialspoint.com/sql/sql-indexes.htm

MySQL Profiling might also help you in your endeavour MySQL 分析也可能对您有所帮助

Your queries are not optimized and probably could be done better in other way incluiding using only one composed query (JOIN) to fetch all data at once.您的查询未优化,可能可以通过其他方式做得更好,包括仅使用一个组合查询 (JOIN) 一次获取所有数据。

Also if your tables have lots of items is good practice to create INDEXES to the fields uses in the common queries for the filter to make the search faster.此外,如果您的表有很多项目,那么为过滤器的常用查询中使用的字段创建索引以加快搜索速度是一种很好的做法。

Example, your firs select has this complexity (and probably is not well formed)例如,您的第一个选择具有这种复杂性(并且可能格式不正确)

SELECT * FROM konucuklar WHERE status='' 
                           and category='football' 
                         GROUP BY matchhour 
                         ORDER BY id asc

But is used only to get the matchhour for the second query.但仅用于获取第二个查询的比赛时间。 The minimal optimization is to use a query to fetch only the required field.最小的优化是使用查询来仅获取所需的字段。

SELECT DISTINCT matchhour FROM konucuklar WHERE status='' and category='football' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM