PHP MySql - 非常慢的数据加载

时间:2017-02-26 07:40:34

标签: php mysql

我有一个只有30 000行的MySql数据表。它不是一个大数据表,但加载时间明显更高(超过60秒)。我有3个表名为Category,Products and Companies。我需要根据所选类别加载产品和公司数据。这是我的代码。它有效,但加载速度很慢..

"
SELECT p1.CompanyID
     , p1.NameE as ProductName
     , p1.CategoryID
     , c1.CompanyID as CompanyID
     , c1.NameE as CompanyName 
  FROM Products as p1 
  JOIN Companies as c1 
    ON p1.CategoryID = ".$row['CategoryID']." 
   AND p1.CompanyID = c1.CompanyID 
 GROUP 
    BY ProductName 
 ORDER 
    BY CompanyName ASC LIMIT 0, 15;
";

1 个答案:

答案 0 :(得分:-1)

确保在连接的ON子句中使用的列上有索引。然后尝试更改您的查询:

$queryList = $db->query(
    "SELECT p1.CompanyID, p1.NameE as ProductName, p1.CategoryID, c1.CompanyID as CompanyID, c1.NameE as CompanyName 
    FROM Products as p1 
    INNER JOIN Companies as c1 
    ON p1.CompanyID=c1.CompanyID 
    WHERE p1.CategoryID=".$row['CategoryID']." 
    GROUP BY ProductName 
    ORDER BY CompanyName ASC 
    LIMIT 0, 15");