编辑:(添加了说明)
我有一个存储一些数据的表,它具有以下所示的结构。
id tparti_id orde desc
1 1 10 One thing
2 1 20 Another thing
3 1 30 Last task of the month
4 2 10 First of second month
5 2 20 Second and last of second month
6 3 10 First of third month
orde字段是具有相同tparti_id的行的序列,此值在我们的应用中用于对给定tparti_id的内容进行排序,用户可以对其进行重新排序以更改值。
存储的值来自文本文件,并由CakePHP应用程序解析。
当插入新行时,通过在orde中搜索当前值并添加10给定id来计算orde序列中的下一个值,如果未找到orde则返回10
public function nextOrden($tpid){
$sql = "select orde from tdpies where tparti_id =".$tpid." order by orde desc limit 1;";
$r = $this->query($sql,$cachequeries = false);
if ($r){
$res = $r[0][0]['orde'] + 10;
} else {
$res = 10;
}
return $res;
}
插入少量记录时可以正常工作,但是插入数千条记录时很费时间。
如何提高性能:
谢谢
您似乎拥有一个自动递增的id,因此可以orde
在查询表时进行计算,而在创建表时可以进行计算:
select p.*, 10 * (row_number() over (partition by tparti_id order by id)) as orde
from tdpies p;
如果要在上进行处理insert
,则在上添加索引tdpies(tparti_id, orde)
。在触发器内,您将看到以下内容:
new.orde := (select coalesce(max(orde), 0) + 10
from tdpies p
where p.tparti_id = new.tparti_id
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句