简体   繁体   English

我只想要桌子上的一行。 进行JOIN还是只进行两个选择调用比较好

[英]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.

相关问题 从两个表中选择数据。 一张桌子是父母/孩子,我需要让父母没有关系 - Select data from two tables. one table is parent/child and I need to get the parents without a relation 连接两个表。 使用连接列作为 Group By 然后 select 基于时间戳的最新行 - Join two tables. Use a joined column as Group By to then select the latest row based on timestamp 我有三张桌子。 我想要一张桌子。 重新设计我的桌子 - I have three tables. I want to have one table. ReDesign my table MYSQL:我有两个表。 我想在表 1 中使用 FK 显示表 1 和表 2 中的数据 - MYSQL: I have two tables. I want to show data from table 1 and table 2 using FK in table 1 如何联接这3个表并使Select查询工作? - How do I join these 3 tables and make my Select query work? 我想联接数据库中的两个表 - I want to join two tables in my database 一个从两个MySQL表中选择数据的语句。 显示名称而不是ID - One statement to select data from two MySQL tables. Show name instead of/by ID MySQL查询根据来自两个不同表的值选择一个表。 - MySQL query to select one table based on values from two different tables. 连接 MySQL 中的两个表,结果集中只有一行,从第二个表中选择了几列 - Join two tables in MySQL, have just one row in the result set with few columns selected from second table 在 MySQL 中连接两个表,从第二个表中只返回一行 - Join two tables in MySQL, returning just one row from the second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM