[英]I only want one row from my tables. Is it better to do a JOIN or just make two select calls
I got two tables I need one row containing all columns of table1 and only 1 column of table2. 我有两个表,我需要一行包含table1的所有列和table2的仅一列。 I am getting the row by using order by rand() limit 0,1; 我通过使用rand()limit 0,1;命令来获取行;
I am wondering if it more efficient to select all columns of table1 then make another call to get that extra column from table 2. since i only need one record, i am worried it is doing extra work in the background to join the tables when I only need one row. 我想知道选择表1的所有列是否更有效,然后再进行一次调用以从表2中获取该额外的列。由于我只需要一条记录,因此我担心在后台连接表时会做额外的工作只需要一行。
I am still developing locally and my computer is fast so I can't tell the difference, but when it goes live there will be many calls per second on paid servers so resource is precious 我仍在本地开发,我的计算机运行很快,所以我无法分辨出其中的区别,但是当它投入使用时,付费服务器上每秒会有很多呼叫,因此资源非常宝贵
There is no efficiency in order by rand limit 0,1; 按兰特限制0,1排序没有效率; it will not scale well. 它无法很好地扩展。 It is a design flaw out of the gate. 这是一个设计缺陷。 If you insist on this then you would certainly not want to do a join since this will iterated over every row in the table. 如果您坚持这样做,那么您肯定不希望进行联接,因为这将遍历表中的每一行。 Minimally, you should pick a small sample of rows to pull a random one from: 至少,您应该选择一小部分行样本,以从以下项中随机抽取一条:
$row=SELECT table_rows*rand() FROM INFORMATION_SCHEMA.TABLES WHERE foo and bar
SELECT * FROM foobar LIMIT $row, 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.