C
C
cryp242019-03-28 13:56:03
Laravel
cryp24, 2019-03-28 13:56:03

What is the best way to check records in an array for uniqueness?

Generally there is an array of 10 records, there is a table of 10000 records. It is necessary to determine which records in the array are not in the table and then insert them. Before this I had

$select_all_news = DB::select("select * from news WHERE author='автор'");

That is, in fact, he pulled out the floor of the table, and then scrolled through two cycles for and foreach, comparing the arrays. Thus, an array of unique news was formed. (one query to the database)
now remade like this
for ($x=0; $x<count($all_news['url']); $x++)
        {
            if(!DB::table('news')->where('source',$all_news['url'][$x])->where('author',$author)->first())
            {
                $unic_news[$x]['url']=$all_news['url'][$x]; 
                $unic_news[$x]['preview_img']=$all_news['preview_img'][$x];
                $unic_news[$x]['title']=$all_news['title'][$x];
                $unic_news[$x]['small_description']=$all_news['small_description'][$x];
            }
        }

In this case, 10 requests are obtained, since there is a reconciliation for each record.
Which option is more correct and why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2019-03-28
@cryp24

The second option has the right to exist. Additionally, I would advise you to add indexes to the bunch of fields "source" and "author" to speed up the search. And also not to do ->first()to check the existence of a record, for this there is a method exists.

A
Alexey Shmyrko, 2019-03-30
@shmyrkoalejsejj

Requests in cycles are very bad, it is always desirable to get rid of this.
You must have a unique key in the database so that there are no duplicates. When you do that, you can use insert ignore
Your code can be simplified by using the firstOrCreate method
To get rid of the queries in the loop you need to
1. Query the database whereIn
2. Loop through what is found and filter
3. Add the rest through one insert
This way you will only have two queries

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question