0

我们无法让这个简单的查询工作:

SELECT * FROM
(SELECT  h.*, h.NewValue AS STATUS,
         (SELECT h2.CreatedDate
          FROM siebel_service_request_history h2
          WHERE h2.Field = 'Status' AND h2.CreatedDate > h.CreatedDate AND
                h2.SiebelID = h.SiebelID
          ORDER BY h2.CreatedDate
          LIMIT 1
         ) AS nextCreatedDate
  FROM siebel_service_request_history h WHERE h.Field ='Status') h

  GROUP BY h.SiebelID, h.Status

它适用于 grouping by SiebelID(但速度很慢),并且如果我们添加 grouping by ,它会永远挂起Status。但是我们需要在分组中包含两个字段才能获得我们想要的数据。

对于 salesforce_case_history 表,我们运行和工作的查询完全相同(它运行得非常快,准确地说,我们在两个表中的记录数大致相同(salesforce_case_history - -- 1159870 条记录 VS siebel_service_request_history - 1202865 条记录)):

SELECT * FROM (SELECT h.*, h.NewValue AS STATUS,
         (SELECT h2.CreatedDate
          FROM salesforce_case_history h2
          WHERE h2.Field = 'Status' AND h2.CreatedDate > h.CreatedDate AND
                h2.CaseId = h.CaseId
          ORDER BY h2.CreatedDate
          LIMIT 1
         ) AS nextCreatedDate
  FROM salesforce_case_history h WHERE h.Field ='Status'
 ) h
GROUP BY h.CaseId, h.Status

siebel_service_request_history我们还为内部子选择搜索创建了相同的复合索引,包括我们在 salesforce_case_history 上创建的三个字段 ( SiebelID, CreatedDate, ) 表。Field我们尝试了几乎所有我们知道的东西,但不幸的是没有找到这个问题的根本问题。

4

1 回答 1

0

尝试:

SELECT  h.*, h.NewValue AS STATUS,
         (SELECT h2.CreatedDate
          FROM siebel_service_request_history h2
          WHERE h2.Field = 'Status' AND h2.CreatedDate > h.CreatedDate AND
                h2.SiebelID = h.SiebelID
          ORDER BY h2.CreatedDate
          LIMIT 1
         ) AS nextCreatedDate
  FROM siebel_service_request_history h WHERE h.Field ='Status'

  GROUP BY h.SiebelID, h.Status

你有一个额外的不必要的子查询步骤。

还可以尝试从子查询创建一个表,在分组依据上添加索引,然后对 select * from table 进行分组。会快得多,我在所有例程中都使用带有索引的临时表。

于 2013-09-27T08:43:28.303 回答