【发布时间】:2017-05-11 10:51:08
【问题描述】:
我有一个场景,我希望有更有效的方法来优化它的代码,我们开始吧。
假设有一个名为 ticket_thread 的表,其中包含以下字段
- 线程ID
- ticketID
- threadType - 可以是 c2s、s2s、s2c
- postTime - 日期时间
- 留言
所有数据都按照ticketID排序,后跟postTime
我的工作是确定每个 c2s 到 s2c 所需的时间,也就是响应时间。
我目前的方法是将过滤后的表转储到两个列表中 - c2s 和 s2c
while (!isempty($c2s) || !isempty($s2c)) {
// popping first record from c2s
$c2sRecord = array_shift($c2s);
if (!$c2sRecord['ticketID'] == $s2c[0]['ticketID']) {
// cannot find a response to the ticket
echo $c2sRecord['ticketID'] . "<br>";
} else {
echo $c2sRecord['ticketID'];
// popping first response from s2c
$s2cRecord = array_shift($s2c);
// print out the response time
echo " " . date_diff($s2cRecord['postTime'], $c2sRecord['postTime']);
$filter = true;
while ($filter) {
// checking the next record in c2s, if it is a different ticket
// OR the new post is placed AFTER service has responded.
if (($c2s[0]['ticketID'] <> $s2cRecord['ticketID'])
or ($c2s[0]['postTime'] > $s2cRecord['postTime'])) {
// stops the filter
$filter = false;
} else {
// pop out unneeded records (supplementary questions)
$c2sRecord = array_shift($c2s);
}
}
}
我的问题是,这需要的时间太长,有没有更快的方法可以使用 SQL 来生成我需要的东西?
table generated from SQL
ticket_id | c2sTime | s2cTime | timeTaken | rank
0012 | 12:20:20 | 12:30:20 | 00:10:00 | 1
0012 | 12:40:00 | 12:55:30 | 00:15:30 | 2
0012 | 13:10:20 | null | null | 3
0013 | 12:20:20 | null | null | 1
编辑:根据要求提供示例表
threadID | ticketID | threadType | postTime | message
3012 | 0012 | c2s | 12:20:20 | customer A's 1st post
3014 | 0012 | c2s | 12:20:30 | Added info to A's 1st post, should not be included
3015 | 0012 | s2c | 12:30:20 | Support responding to A's 1st post
3016 | 0012 | s2s | 12:30:30 | internal chat, should not be included
3017 | 0012 | s2s | 12:30:40 | internal chat, should not be included
3018 | 0012 | c2s | 12:40:00 | A's 2nd post
3019 | 0012 | s2c | 12:55:30 | Support responding to A's 2nd post
3020 | 0012 | s2c | 13:00:00 | Added info to Support's 2nd response, should not be included
3021 | 0012 | c2s | 13:10:00 | A's 3nd post
3013 | 0013 | c2s | 12:20:20 | customer B's 1st post
【问题讨论】:
-
编辑问题并添加表格中的示例数据以获得所需的输出。
-
您的输出中的
rank是什么?它只是一些编号,基于排序吗? (又名。row_number()/rank()/dense_rank()那种东西?) -
@pozs 是的,它已被使用,因此我可以轻松判断客户和支持成员之间来回回复了多少次
标签: php sql postgresql