Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect parent row only if it has no children
    primarykey
    data
    text
    <p>I have a MySQL database in which table A has a one-to-many relation to table B, and I would like to select all rows in table B that have no children in table A. I have tried using </p> <pre><code>SELECT id FROM A WHERE NOT EXISTS (SELECT * FROM B WHERE B.id=A.id) </code></pre> <p>and</p> <pre><code>SELECT id FROM A LEFT JOIN B ON A.id=B.id WHERE B.id IS NULL </code></pre> <p>Both of these seem slow. Is there a faster query to achieve the same thing?</p> <p>In case this is relevant, in my database table A has about 500,000 rows and table B has about 3 to 4 million rows.</p> <p><strong>Edit:</strong> For the actual tables in my database, explain gives me:</p> <pre><code>+----+--------------------+------------------+-------+---------------+---------------------------+---------+------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------------+-------+---------------+---------------------------+---------+------+---------+--------------------------+ | 1 | PRIMARY | frontend_form471 | index | NULL | frontend_form471_61a633e8 | 32 | NULL | 671927 | Using where; Using index | | 2 | DEPENDENT SUBQUERY | SchoolData | index | PRIMARY | PRIMARY | 49 | NULL | 3121110 | Using where; Using index | +----+--------------------+------------------+-------+---------------+---------------------------+---------+------+---------+--------------------------+ </code></pre> <p>for</p> <pre><code>select number from frontend_form471 where not exists (select * from SchoolData where SchoolData.`f471 Application Number`=frontend_form471.number) </code></pre> <p>and</p> <pre><code>+----+-------------+------------------+-------+---------------+---------------------------+---------+------+---------+------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------------+-------+---------------+---------------------------+---------+------+---------+------------------------------------------------+ | 1 | SIMPLE | frontend_form471 | index | NULL | frontend_form471_61a633e8 | 32 | NULL | 671927 | Using index; Using temporary | | 1 | SIMPLE | SchoolData | index | PRIMARY | PRIMARY | 49 | NULL | 3121110 | Using where; Using index; Not exists; Distinct | +----+-------------+------------------+-------+---------------+---------------------------+---------+------+---------+------------------------------------------------+ </code></pre> <p>for</p> <pre><code>select distinct number from frontend_form471 left join SchoolData on frontend_form471.number=SchoolData.`f471 Application Number` where SchoolData.`f471 Application Number` is NULL </code></pre> <p>where in my case frontend_form471 is table A and SchoolData is table B</p> <p><strong>Edit2:</strong> In table B (SchoolData) in my database, id is the first part of a two part primary key, so it is indexed and there are still multiple entries in B with the same id.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload