简体   繁体   中英

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. I am getting the row by using order by 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.

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; 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM