1

我的问题是我们做了一个选择,然后,对于每一行,我们运行 4 个不同的请求 SQL(很疯狂),你可以猜到我们发出了很多请求,并且使用它的系统非常慢。

SELECT 
    deal_source.id, 
    deal_source.source_name, 
    deal_source.spider_status, 
    spider.last_success_date
FROM deal_source 
JOIN spider 
ON deal_source.id = spider.deal_source_id

然后对于这个查询的每一行,我们进行:

$total_query = "SELECT count(id) as total
                FROM spider_log 
                WHERE deal_source_id = '$deal_source_id' 
                AND date_format(date_created, '%Y-%m-%d') = '$lastdate' ";

$added_query = "SELECT count(id) as added
                FROM spider_log 
                WHERE deal_source_id = '$deal_source_id' 
                AND action = 'added'  
                AND date_format(date_created, '%Y-%m-%d') = '$lastdate' ";

$extended_query = "SELECT count(id) as extended 
                   FROM spider_log 
                   WHERE deal_source_id = '$deal_source_id' 
                   AND action = 'extended'  
                   AND date_format(date_created, '%Y-%m-%d') = '$lastdate' ";

$duplicate_query = "SELECT count(id) as duplicate 
                    FROM spider_log 
                    WHERE deal_source_id = '$deal_source_id' 
                    AND action = 'duplicate'  
                    AND date_format(date_created, '%Y-%m-%d') = '$lastdate' ";
4

2 回答 2

3
SELECT   d.id,
         d.source_name,
         d.spider_status,
         s.last_success_date,
         COUNT(l.id) AS total,
         SUM(l.id IS NOT NULL AND l.action='added'    ) AS added,
         SUM(l.id IS NOT NULL AND l.action='extended' ) AS extended,
         SUM(l.id IS NOT NULL AND l.action='duplicate') AS duplicate
FROM     deal_source d
    JOIN spider      s
      ON s.deal_source_id  = d.id
    JOIN spider_log  l
      ON l.deal_source_id  = d.id
      ON l.date_created   >= s.last_success_date
     AND l.date_created   <  s.last_success_date + INTERVAL 1 DAY
GROUP BY d.id
于 2013-01-21T21:00:00.893 回答
3

几点:

  • 您可以优化每个查询的性能,使用EXPLAIN和仔细添加索引。

  • 您可以将所有查询组合成一个大查询,因此您不必使用大量查询来访问数据库。

  • 除了大量查询之外,Thedate_format(date_created, '%Y-%m-%d') = '$lastdate'还是一个性能杀手,因为它将函数 ( DATE_FORMAT()) 应用于列 ( date_created),因此无法使用索引,并且该函数被调用数千次或数百万次(因为检查了许多行)。将此类条件(无论它们在您的代码中的何处)更改为:

     (  date_created >= DATE('$lastdate') 
    AND date_created < DATE('$lastdate') + INTERVAL 1 DAY
     )
    

    甚至更好,如果那$lastdate是一个日期,到:

     (  date_created >= '$lastdate' 
    AND date_created < '$lastdate' + INTERVAL 1 DAY
     )
    

    date_created如果是一DATE列,则更好的是:

        date_created = '$lastdate' 
    
于 2013-01-21T21:04:50.527 回答