Answer the question
In order to leave comments, you need to log in
How to quickly check if a record exists in a table?
There are two tables, table A with a huge amount of data and an empty table B. The task is to write the transferred data to table B, provided that one field does not exist in table A. Now a search has been made and a request for each element in the loop SELECT 1 FROM (`А`) WHERE `поле` = '123' LIMIT 1
. I made the `field` field a unique index, removed the data acquisition by replacing * with 1. Tell me how else can I speed up this process?
Answer the question
In order to leave comments, you need to log in
Well, for example, COUNT() is much faster. If you do not choose, but check, then it will be faster
I didn't really understand, but maybe you need to get everything that is not in A with one request? For example, a link from
create temporary table temptable...
insert into temptable values ()()()...
select * from temptable where not exists (select * from a where a.field = temptable.field)
drop temptable
"on the forehead" we get records that are missing in another table and shove them into b
insert into b (.....)
select * from temp where temp.field not in (select distinct field from a)
insert into b (.....)
select * from temp
left join a on temp.field=a.field
where a.field is null
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question