为什么学说使用 WHERE IN 而不是 LIMIT?
为什么学说(1.2)使用WHERE IN
而不是LIMIT
?
此代码:
Doctrine_Query::create()
->from('Table t')
->limit(10)
->getSqlQuery();
返回类似以下内容:
SELECT t.id_table AS t__id_table FROM table AS t WHERE t__id_table IN (1,2,3,4,10,12,18,20,21,25);
而不是:
SELECT t.id_table AS t__id_table FROM table AS t LIMIT 10;
此行为对于任何 LIMIT
值都是相同的。这会针对高 LIMIT
值生成非常长的查询。
附加问题:Doctrine 如何知道要使用什么 id? (通过向数据库发送另一个查询??)
Why does doctrine (1.2) use WHERE IN
instead of LIMIT
?
This code:
Doctrine_Query::create()
->from('Table t')
->limit(10)
->getSqlQuery();
Returns something like this:
SELECT t.id_table AS t__id_table FROM table AS t WHERE t__id_table IN (1,2,3,4,10,12,18,20,21,25);
Instead of this:
SELECT t.id_table AS t__id_table FROM table AS t LIMIT 10;
This behaivor is same for any LIMIT
value. This generates a very long queries for high LIMIT
values.
Bonus question: How does Doctrine know, what ids to use? (By sending another query to DB??)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为
LIMIT
对数据库行而不是“对象”进行操作。当您输入$q->limit(10)
时,您希望获取十个对象,而不是数据库中的十行。考虑以下查询(产品和类别具有多对多关系):
要获取 10 个产品(对象),您的查询必须获取至少 20 行。您不能使用
LIMIT 10
,因为(仅作为示例)只会退回 3 件产品。这就是为什么您需要找出应该获取哪些产品(限制适用于产品),然后获取实际数据。这将导致以下查询:
第二个查询可能返回 20、423 或 31 行。正如您所看到的,这不是来自
limit()
的值。附言。在这种情况下,Doctrine2 更加清晰,因为它使用
setMaxResults()
方法而不是limit()
,这样更不容易混淆。That's because
LIMIT
operates on database rows not "objects". When you type$q->limit(10)
you want to get ten objects, not ten rows from database.Consider following query (products and categories have many-to-many relationship):
To fetch 10 products (objects) your query will have to fetch at least 20 rows. You cannot use
LIMIT 10
cause (just for example) only 3 products would be returned. That's why you need to find out which products should be fetched (limit applies to products), and later fetch the actual data.That will result in following queries:
Second query might return 20, 423 or 31 rows. As you can see that's not a value from
limit()
.PS. Doctrine2 is much more clearer in that case as it's using
setMaxResults()
method instead oflimit()
which is less confusing.使用 Doctrine 1.2.3:
我得到的结果是:
你的代码中是否还有其他事情发生?
Using Doctrine 1.2.3:
I get the result:
Is there maybe something else going on in your code?