Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to search single record from 6000k+ record DB in laravel

Tags:

sql

mysql

laravel

I want to know how to search a single record from a 6000k+ record database. I have written a query for it but I am facing 15s load time it's too high for the efficient system so please help me to search in an efficient way to take lower response time in searching.

$users = DB::select('select * from tablefour where Provider_Business_Mailing_Address_Fax_Number = ?', array($request['id'])); 
return $users;
like image 268
hassan raza Avatar asked Oct 30 '25 11:10

hassan raza


2 Answers

enter image description here

Make the column index then search will work fast on that column.This help me.

For Laravel:- Do this in Schema

    final class AddIndexesTableFour extends Migration
{
    public function up(): void
    {
        Schema::table('tablefour', function (Blueprint $table) {
            $table->index('Provider_Business_Mailing_Address_Telephone_Number');
        });
    }

    public function down(): void
    {
        Schema::table('tablefour', function (Blueprint $table) {
            $table->dropIndex(['Provider_Business_Mailing_Address_Telephone_Number']);
        });
    }
}
like image 194
Rana Nadeem Avatar answered Nov 02 '25 00:11

Rana Nadeem


1st Only select those columns which you needed.

$users = \DB::table('tablefour')->select(['columnone','columntwo'])->whereIn('Provider_Business_Mailing_Address_Fax_Number',array($request['id']))->get();
return $users;

2nd

Make indexing on those columns in which columns are you selecting.

Check @Rana Nadeem answer for how to make indexing.

using migration

Schema::table('tablefour', function (Blueprint $table) {
    $table->index('user_id');
});
like image 28
Dilip Hirapara Avatar answered Nov 02 '25 01:11

Dilip Hirapara