我在我的PHP代码中有一个命中MySQL 5.5数据库的查询。此查询的目的是生成下一个发票编号,因此我需要确保获得现有发票编号的最大值并将其加1。
诀窍在于这个引用(invoice #)列是一个varchar,因此用户可以输入字母数字字符,因此我的查询只需要查看数值。
select max(cast(t.reference as unsigned)) reference
from transactions t, families f
where t.familyid in (select id from families where companyid = 1415)
and t.familyid = f.id
and transactiontype = 'C'
and t.reference REGEXP '^[0-9]+$';正如您所看到的,我使用了一个正则表达式,因为我要做的是获取reference列中当前存在的最大数字,以便生成下一个发票编号。
当前查询需要2到8秒的时间才能缓存。有没有什么方法可以加速这个过程,或者这就是它得到的最好的结果了吗?
发布于 2018-02-07 08:45:14
请尝试此操作,在子句开销中删除:
select max(cast(t.reference as unsigned)) reference
from transactions t, families f
where
t.familyid = f.id
and f.companyid = 1415
and t.transactiontype = 'C'
and t.reference REGEXP '^[0-9]+$';https://stackoverflow.com/questions/48573435
复制相似问题