php - Laravel best practice method for querying using an index key in tableGet the solution ↓↓↓
I have the following database table:
id - PK (Auto Increment) hash - a unique index the value here is filled using the function "uniqid()" title - string..
I want to query from table using a hash value not the id. Is this practice will load the server, as I know is the best way to get some row from the database table especially the one who contain high number of rows is searching using a primary key:
$row = Book::find(1);
Or I can use the following eloquent builder without worry of making unnecessary load to database server because he hash is set as a unique key:
$row = Book::where('hash',$hashFromAPiRequest)->first();
There is a package named laravel Scout, I am not sure if I really need to use it or not.
You don't need to use Laravel Scout for this. A simple where condition will suffice. As long as the hash column is indexed, your query will not load up the server.
However, since you mentioned unnecessary load, you can still optimized a tiny bit here by using select(). Please use only those columns that you really need otherwise you are sending/consuming additional bytes of data.
$row = Book::select('id', 'title')->where('hash',$hashFromAPiRequest)->first();
You can also check out Laravel use mysql indexing if you want to learn more about optimizing.
Share solution ↓
Link To Answer People are also looking for solutions of the problem: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.