是否可以缩短这些学说查询?

发布于 2024-11-24 01:31:50 字数 610 浏览 0 评论 0原文

    $q_auctions = Doctrine_Query::create()
       ->select('COUNT(t.id) AS num_items')
       ->from('Auctions t')
       ->where('t.ends_at > NOW()');

    $q_auctions_upcoming = Doctrine_Query::create()
       ->select('COUNT(t.id) AS num_upcoming')
       ->from('Auctions t')
       ->where('t.starts_at > NOW()');

    $q_auctions_closed = Doctrine_Query::create()
       ->select('COUNT(t.id) AS num_closed')
       ->from('Auctions t')
       ->where('t.ends_at < NOW()');

我在《教义》中有这 3 个非常相似的拍卖,但我不确定是否可以以某种方式缩短它。我不喜欢重复太多,所以也许有人可以给我提示。

    $q_auctions = Doctrine_Query::create()
       ->select('COUNT(t.id) AS num_items')
       ->from('Auctions t')
       ->where('t.ends_at > NOW()');

    $q_auctions_upcoming = Doctrine_Query::create()
       ->select('COUNT(t.id) AS num_upcoming')
       ->from('Auctions t')
       ->where('t.starts_at > NOW()');

    $q_auctions_closed = Doctrine_Query::create()
       ->select('COUNT(t.id) AS num_closed')
       ->from('Auctions t')
       ->where('t.ends_at < NOW()');

I have these 3 very similar auctions in Doctrine, but I'm not sure wether I can shorten it somehow. I don't like so much repeating so maybe if someone could give me a tip.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2024-12-01 01:31:50

首先,我不会担心这三个查询太多,除非您真的想减少此页面的查询总数。 starts_at 和ends_at 上的索引可能有助于加快速度,但取决于这些表中的记录数。

现在想到的唯一一件事是执行单个查询,返回开始日期和结束日期,然后将 PHP foreach 循环中的日期解析为您需要的三组。这会将查询次数从 3 个减少到 1 个,但会增加一些 PHP 开销。如果您在那里有数百或数千场拍卖,这可能不是一个好主意。

First of all, I wouldn't worry about having the three queries too much unless you're really trying to cut down the total number of queries for this page. Indexes on starts_at and ends_at might help speed things up but depends on the number of records in those tables.

The only thing that comes to mind right now would be to do a single query, return the start and end dates, and then parse those inside a PHP foreach loop into the three groups you need. This would drop the queries from 3 to 1 but add some PHP overhead. If you've got hundreds or thousands of auctions there, this might not be a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文