Laravel MySQL Best Pratice : Laravel Tips And Tricks

Blogs ❯❯ Laravel

Image could not load

MySQL Image by Rubaitul Azad on Unsplash

जैसा कि हम जानते है , कि Laravel कई सारे features provide करता है। लेकिन कई बार application बड़ा होने के कारण या extra queries लिखने की वजह से process slow हो जाती है , इस blog में कुछ ऐसी tips और tricks के बारे में बात करेंगे जिससे Laravel application process fast हो सके।

Use chunk for large datasets

normally जब हम database से large data fetch करके उस data पर किसी loop के through extra operation perform करते हैं , तो कुछ इस तरह से code लिखते हैं।

$blogs = Blog::all();       // when using eloquent
$blogs = DB::table('blogs')->get();  // when using query builder

foreach ($blogs as $blog){
 // perform other operation on blog
}

यहां हम एक बार में table से complete data fetch कर रहे हैं, हालाँकि इस code में कोई कमी नहीं है , लेकिन क्या हो अगर हमरी table में 40 या 50 Lakhs records हैं। No doubt data fetch होने पर ज्यादा load बढ़ेगा और process slow होगी।

इसके लिए Laravel हमें chunk functionality provide कराता है , जब table से large data fetch करना हो तो आप वहां पर chunk use कर सकते हैं। chunk का simply मतलब है result को subset में process करना।

// when using eloquent
$blogs = Blog::chunk(100, function($blogs){
  foreach ($blogs as $blog){
    // Process blogs
  }
});

// when using query builder
$blogs = DB::table('blogs')->chunk(100, function ($blogs){
  foreach ($blogs as $blog){
    // Process blogs
  }
});

ऊपर दिए गए code से हम database से 100 rows fetch करके operation perform कर रहे हैं। हालाँकि need के according आप ये number कम या ज्यादा कर सकते हैं।

Option 2 : Use chunkById()

इसके अलावा आप , chunkById() method का use भी कर सकते हैं।

// when using eloquent
$blogs = Blog::chunkById(100, function($blogs){
  foreach ($blogs as $blog){
   // Process blogs
  }
});

// when using query builder
$blogs = DB::table('blogs')->chunkById(100, function ($blogs){
  foreach ($blogs as $blog){
   // Process blogs
  }
});

chunk() और chunkById() दोनों ही database से result को subsets में fetch करते हैं , तो दोनों में difference क्या है। well chunk() records को offset and limit के according rows को retrieve करता है। जिसकी query कुछ इस तरह से prepare होती है -

select * from blogs offset 0 limit 100
select * from blogs offset 101 limit 100

chunkById()

chunkById , primary id के bases पर work करता है , अगर आपके table में primary key integer और auto increment है तो , उसके लिए query कुछ इस तरह से prepare होगी।

select * from blogs order by id asc limit 100
select * from blogs where id > 100 order by id asc limit 100

Select only necessary columns

normally , हम किसी table से single या multiple records retrieve करने के लिए सभी columns को get करते है , लेकिन ऐसा नहीं करना चाहिए।

$blogs = Blog::find(1); //When using eloquent
$blogs = DB::table('blogs')->where('id','=',1)->first(); //When using query builder

// The above code makes below query
select * from blogs where id = 1 limit 1

सभी columns को select करने की जगह हमें सिर्फ वही columns select करने चाहिए , जिनकी जरूरत हो।

$blogs = Blog::select('id','title', 'slug')->find(1); //When using eloquent
$blogs = DB::table('blogs')->where('id','=',1)->select('id','title', 'slug')->first(); //When using query builder

// The above code makes below query
select id,title,slug from blogs where id = 1 limit 1

Use pluck() for single or two columns

अक्सर , सिर्फ एक या दो column की जरूरत करने पड़ने पर , हम सभी columns select करते हैं ।

$blogs = Blog::select('title','slug')->get(); //When using eloquent
$blogs = DB::table('blogs')->select('title','slug')->get(); //When using query builder

ऐसा करने पर Laravel behind the scene , कुछ इस तरह से result process करता है।

  • सबसे पहले select title, slug from posts query execute करता है।

  • फिर हर एक row के लिए Blog model object create होता है।

  • इसके बाद Blog models के साथ collection बनता है।

  • then , result return होता है।

जिसे कुछ इस तरह से access / use करते हैं।

foreach ($blogs as $blog){
  // $blog is a blog model or php standard object
  $blog->title;
  $blog->slug;
}

pluck()

इसकी जगह आप Laravel का pluck method का use करके , process को fast और smooth बना सकते हैं।

$blogs = Blog::pluck('title', 'slug'); //When using eloquent
$blogs = DB::table('blogs')->pluck('title','slug'); //When using query builder

ऐसा करने पर Laravel behind the scene , कुछ इस तरह से result process करता है।

  • सबसे पहले select title, slug from posts query execute करता है।

  • इसके बाद result में एक Associative Array create होता है जिसमे title as array value और slug as array key होता है।

  • then , result return होता है। Array Format : [ slug1 => title1, slug2 => title2 ]

जिसे कुछ इस तरह से access / use कर सकते हैं।

foreach ($blogs as $slug => $title){
  // $title is the title of a blog
  // $slug is the slug of a blog
}
Important

ध्यान रहे ऐसा सिर्फ तभी होगा जब आप , दो columns pluck करते हैं , single column pluck करने पर आपको Indexed Array मिलेगा जिसमे सभी selected column की values होगी।
For Example :

$blogs = Blog::pluck('id');

//it will return

[1,2,3,4,5,6,7,8.. and so on]

Count rows using query instead of collection

normally हम, count करने के लिए सभी records को get करके कुछ इस तरह से count करते हैं।

$blogs = Blog::all()->count(); //When using eloquent
$blogs = DB::table('blogs')->get()->count(); //When using query builder

//it generates below query
select * from blogs

यहां पर , हम सभी result को retrieve करके उसे count कर रहे हैं तो obviously ज्यादा records होने पर process slow होगी। इसलिए इस approach को follow न करके कुछ् इस तरह से manage कर सकते हैं।

$blogs = Blog::count(); //When using eloquent
$blogs = DB::table('blogs')->count(); //When using query builder

// it generates below query
select count(*) from posts

Separate columns with large data

ज्यादातर हम , सभी type के columns को एक ही table में रख देते हैं जो कि कुछ हद तक सही है। लेकिन अगर कोई column large data को contain करता है। जैसे TEXT या LARGE TEXT data type के columns को separate table में ही manage करें।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook