【发布时间】:2017-08-01 14:20:29
【问题描述】:
在数据表中,我使用两个查询,一个通过服务器端分页获取特定日期范围内的记录,另一个获取该特定日期范围内的记录总数。我正在使用 Postgresql 数据库。
select * from daily_txns where created_date <='2017-08-01' and created_date>='2017-07-01' and mid='0446721M0008690' order by created_date desc limit 10;
select count(mid) from daily_txns where created_date <='2017-08-01' and created_date>='2017-07-01' and mid='0446721M0008690';
这是正确的做法还是有任何最好的方法。 如果第一个查询需要 20 秒,那么第二个查询需要 40 秒,显示结果的总时间超过 60 秒。如何克服这个问题。
【问题讨论】:
-
第二个查询可以使用
count(1) -
在不将任何内容加载到 Datatables 的情况下运行数据库中的每个查询需要多长时间?
-
在第一次查询中,您可以获得前 10 条记录,然后您可以给
order by条件 -
我的意思是你可以使用内部查询
-
对于第二个查询,根据 SQL 标准,您无法使用
count、order by条件减少执行时间,因为它会检查表中的所有行
标签: php database laravel datatables