Performance is critical in web applications, affecting user experience and server efficiency. Yii2 provides several built-in mechanisms to optimize performance, including caching, database query optimization, asset management, and background processing. This guide explores best practices for improving Yii2 application speed.
Caching reduces redundant processing by storing frequently accessed data, fragments, or views.
Reduce repeated database queries by caching query results.
$users = User::getDb()->cache(function ($db) {
return User::find()->where(['status' => 1])->all();
}, 3600); // Cache for 1 hourOr use cache(3600) directly in the query:
$users = User::find()->where(['status' => 1])->cache(3600)->all();Best for: Repeated queries that don’t change often.
Use dynamic cache keys to cache search results:
$cacheKey = 'search-results-' . md5(json_encode(Yii::$app->request->queryParams));
if ($this->beginCache($cacheKey, ['duration' => 3600])) {
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel]);
$this->endCache();
}Prevents static cache issues and ensures updated search results.
For pages that don’t change often, store the entire page output:
if ($this->beginCache('full-page-cache', ['duration' => 3600])) {
echo $this->render('index');
$this->endCache();
}Avoid SELECT *, fetch only required columns:
User::find()->select(['id', 'name', 'email'])->where(['status' => 1])->all();Ensure indexes exist for frequently queried columns:
ALTER TABLE user ADD INDEX idx_status (status);Use EXPLAIN to analyze slow queries.
Avoid N+1 query problem by eager loading:
$users = User::find()->with('profile')->all();Instead of:
foreach (User::find()->all() as $user) {
echo $user->profile->address; // Causes multiple queries
}Reduces queries from N+1 to 1+1.
Combine and minify CSS/JS files to reduce HTTP requests:
'components' => [
'assetManager' => [
'linkAssets' => true,
'bundles' => [
'yii\web\JqueryAsset' => [
'js' => ['jquery.min.js']
],
],
],
]Reduces file size and speeds up page load time.
Load JavaScript after the page renders:
<script src="script.js" defer></script>Helps improve First Contentful Paint (FCP).
For tasks like sending emails or generating reports, use background jobs instead of handling them in requests.
Install Yii2 Queue:
composer require yiisoft/yii2-queueConfigure queue (for Redis example):
'components' => [
'queue' => [
'class' => \yii\queue\redis\Queue::class,
'redis' => 'redis',
'channel' => 'queue',
],
]Send an email using a background job:
Yii::$app->queue->push(new SendEmailJob(['email' => 'test@example.com']));Frees up main application threads, reducing response times.
Compress responses to reduce bandwidth usage:
'components' => [
'response' => [
'on beforeSend' => function ($event) {
$event->sender->headers->set('Content-Encoding', 'gzip');
},
],
],Reduces page size by ~70%.
Set cache headers in config/web.php:
'components' => [
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'headers' => [
'Cache-Control' => 'max-age=31536000, public',
],
],
],Caches static assets like images, CSS, and JS for longer periods.
Avoid loading related models unnecessarily.
$users = User::find()->all();
foreach ($users as $user) {
echo $user->profile->address; // Causes multiple queries
}Causes N+1 problem (multiple queries).
$users = User::find()->with('profile')->all();Loads related models in a single query, improving performance.
batch() for Large Data ProcessingInstead of loading 100K records into memory, use batch():
foreach (User::find()->batch(1000) as $users) {
foreach ($users as $user) {
processUser($user);
}
}Processes 1,000 users at a time, reducing memory usage.
Debug mode slows down performance. Disable it in index.php:
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');Improves response times by avoiding debug overhead.
By implementing these Yii2 performance optimizations, you can significantly enhance speed, reduce server load, and improve user experience.
Key Takeaways:
Sign in to join the discussion and post comments.
Sign in